I want to implement a KeyValuePair<K, V>
data structure that implements Ord
if K
implements Ord
. Currently, this is my implementation:
use std::cmp::Ordering;
struct KeyValuePair<K, V> {
key: K,
value: V,
}
impl<K: PartialEq, V> PartialEq for KeyValuePair<K, V> {
fn eq(&self, other: &Self) -> bool {
self.key.eq(&other.key)
}
}
impl<K: Eq, V> Eq for KeyValuePair<K, V> {}
impl<K: PartialOrd, V> PartialOrd for KeyValuePair<K, V> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.key.partial_cmp(&other.key)
}
}
impl<K: Ord, V> Ord for KeyValuePair<K, V> {
fn cmp(&self, other: &Self) -> Ordering {
self.key.cmp(&other.key)
}
}
Other than Ord
, I also have to implement PartialEq
, Eq
and PartialOrd
, which is a bit too much. I don’t want to use #[derive]
because that will compare value
as well.
So is there any way I can simplify my code?