I am trying to wrap my head around the requirement of PartialOrd to be consistent with PartialEq and would appreciate some help to clear that
On the documentation of PartialOrd, the following is the first condition for consistency:
-
a == b
if and only ifpartial_cmp(a, b) == Some(Equal)
.
I am unable to understand why should partial_cmp(a, b) == Some(Equal)
imply that a == b
.
For instance, I can have a struct Person(name, age)
, where the equality (to check duplicates) of the Persons could be on the name
, whereas ordering of these could be on age
.
To test this, I wrote a toy example to see how an inconsistent Ord vs Eq impacts things, and it does result in unexpected behavior:
The following code inserts two different Persons (different names) having the same age into a BtreeSet. But the set deduplicates these due to having the same age:
Prints:
Person { name: "John", age: 30 }
use std::cmp::Ordering;
#[derive(Eq, Debug)]
struct Person {
name: String,
age: u8,
}
impl PartialEq<Self> for Person {
fn eq(&self, other: &Self) -> bool {
self.name.eq(&other.name)
}
}
impl PartialOrd<Self> for Person {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Person {
fn cmp(&self, other: &Self) -> Ordering {
self.age.cmp(&other.age)
}
}
#[test]
fn testBtreeSet() {
use std::collections::BTreeSet;
let mut set = BTreeSet::new();
set.insert(Person {
name: "John".to_string(),
age: 30,
});
set.insert(Person {
name: "Jack".to_string(),
age: 30,
});
set.iter().for_each(|person| {
println!("{:?}", person);
})
}