Default Ord implementation for trait


#1

Hi all,

I need to implement a generic priority queue (basically a simple wrapper around BinaryHeap) which sorts elements by time. So I made HasTime trait which the queue elements need to implement. The problem is that I would like to avoid implementing Ord trait as well for each type because it is always the same for all types which implement HasTime. According to discussion here [1] it seems that we can’t define default cmp implementation for all types which implement HasTime. So, is there some better approach then writing a macro which implements Ord trait for all HasTime types?

Thanks in advance,
–alan

[1] https://github.com/rust-lang/rfcs/issues/1024


#2

The typical way to work around this is by defining your own wrapper type and implementing the required traits for it, e.g.:

#[derive(PartialEq, ...)]
struct Wrapper<T>(T);

impl<T: HasTime> HasTime for Wrapper<T> {
 ...
}

#3

Hi,

nice and smart solution, works like a charm :slight_smile:

Thanks a lot!
–alan