I would like to use a BinaryHeap with elements of type MyWrapperStruct(Weak<MyStruct>), where I implement Ord for MyWrapperStruct. In this implementation, I need to upgrade the weak references, which might fail. If they fail, I want the element to be removed from the heap.
How could I implement this in a way that doesn't require me to traverse the entire heap first to remove all the non-upgradable weak references? There will be a large amount of data in this heap, while only very few of these will become invalid. For the sake of performance, I would like to avoid traversal of all data.
You can't remove an item while you are comparing something.
Also, from my point of view, implementing
Ordwould be wrong for the same reason whyOrdis not implemented for floating-point values in Rust. If you have two non-upgradableWeakstructures, there is no order between them and that's the reason whyPartialOrdexists.If you really want to make your example work, implement
Ordfor your type and order non-upgradableWeaks at the very top (e.g. it is always the biggest element), then usepeekon yourBinaryHeapto check if it is upgradable; if not, remove it and take the next one.This is a nasty hack but probably your best solution if you want your thing to work.