I came across an interesting edge case in the PartialEq documentation: while it goes to great lengths to explain how it needs to be symmetric and transitive, it does not forbid this code:
pub struct Insane(AtomicU32);
impl PartialEq for Insane {
fn eq(&self, rhs: &Self) -> bool {
let left = self.0.fetch_add(1, Ordering::Relaxed);
let right = rhs.0.load(Ordering::Relaxed);
left == right
}
}
This is symmetric and transitive because the original value is compared, so by documentation, this is a valid implementation but I'm pretty sure if you actually used this it'd break a shitload of algorithms that implicitly rely on this not happening.
And frankly, I don't think anyone has ever written anything this insane, so I think it should be desirable to add something like this to the documentation:
The methods must be repeatable, that means, any number of calls to
eqornemust always produce the same result. This is similar to the function being pure and any pure function satisfies this requirement but the function does not have to be pure. It can mutate any state such as by logging the calls as long as this does not affect the result of the repeated call.
Same requirement for Ord and PartialOrd.