I'm trying to use std::collections::BinaryHeap
with a custom struct. In order to do so, I have to have my struct implement the std::cmp::Ord
trait, but what I need is 2 BinaryHeap
s of the same structs but ordered differently.
Is there a way to define 2 Ord implementations & pick which Ord
gets used, or some other way to specify an alternate ordering?
I think I could define 2 different wrapping structs which keep a reference to the original custom struct and have an implementation of Ord
for each of them, but it seems quite wasteful to have to construct potentially a lot of instances of such wrapping structs.
In Pyhton/Java I'd provide a sorting function/Comparator, but there seems no facility like that. In Scala, I can define a compile-time only type to select the right implicit Ordering implementation; it feels like Rust supports something similar, but I haven't been able to work it out.
There's no way to have two different implementations of the same trait for a single type, e.g. this hypothetical scheme doesn't work
struct MyType { ... }
mod foo {
impl Ord for MyType { ... } // A
// everything in here uses the A implementation
}
mod bar {
impl Ord for MyType { ... } // B
// everything in here uses the B implementation
}
If you want different behaviours with BinaryHeap
you do have to just use wrapper types, however, wrapper types are not wasteful, since there is no additional indirection or memory use, struct Foo { data: T }
and T
are same, other than the nominal type name (no matter what type T
is).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With