I was recently surprised by PriorityQueue popping items in a seemingly random order despite setting priorities when I inserted. I had implemented Ord::cmp by unwrapping PartialOrd::partial_cmp for some testing and since I didn't see any panics I thought things were working correctly, when in fact all of my comparisons were returning None.
As far as I can tell PriorityQueue doesn't actually call any function in Ord, just the comparison operators in bubble up, which always return false if partial_cmp is None. Would it make sense to relax the bound to P: PartialOrd, to make this behavior less surprising? Or alternatively use Ord::cmp explicitly instead of PartialOrd::lt? As it stands PriorityQueue is perfectly happy to operate on priorities that implement Ord by unconditionally panicking, despite the fact that Ord is explicitly required, which seems very surprising.
According to the docs for Ord if both Ord and PartialOrd are implemented, PartialOrd should always return Some(self.cmp(other)), which would also have solved my issue, but it relies on convention rather then the compiler to enforce that behavior.
Happy to follow up with a PR if there's a change warranted here!
I was recently surprised by
PriorityQueuepopping items in a seemingly random order despite setting priorities when I inserted. I had implementedOrd::cmpby unwrappingPartialOrd::partial_cmpfor some testing and since I didn't see any panics I thought things were working correctly, when in fact all of my comparisons were returningNone.As far as I can tell
PriorityQueuedoesn't actually call any function inOrd, just the comparison operators in bubble up, which always return false ifpartial_cmpisNone. Would it make sense to relax the bound toP: PartialOrd, to make this behavior less surprising? Or alternatively useOrd::cmpexplicitly instead ofPartialOrd::lt? As it standsPriorityQueueis perfectly happy to operate on priorities that implementOrdby unconditionally panicking, despite the fact thatOrdis explicitly required, which seems very surprising.According to the docs for
Ordif bothOrdandPartialOrdare implemented,PartialOrdshould always returnSome(self.cmp(other)), which would also have solved my issue, but it relies on convention rather then the compiler to enforce that behavior.Happy to follow up with a PR if there's a change warranted here!