Skip to content

Can the trait bound on P be relaxed to PartialOrd? #84

@Zabot

Description

@Zabot

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions