Implement placement-in protocol for BinaryHeap by martinhath · Pull Request #39062 · rust-lang/rust (original) (raw)

Implementation of this is going in the right direction but not quite there yet.

Probably the most notable issue here is that the Place that you create in the underlying vector gets immediatelly dropped, after which you are not allowed to put anything into the place in question anymore.

While this may work in current implementation, because PlaceBack for Vec has no Drop defined, consider what would happen if it had one:

// self.heap.data here is just big enough that a call to make_place would resize the vector. let place = self.heap.data.place_back().make_place(); // resizes vector drop(place); // resizes the vector back to the original size if was implemented weirdly // now the placement protocol asks for a pointer to put data in self.heap.data.place_back().pointer() // returns a pointer out of bounds!

This implementation therefore does not follow the placement protocol.

What ought be done instead is a new structure such as this (I think, feel free to explore alternative designs! generics omitted for brevity):

struct BinaryHeapPlace { heap: &mut BinaryHeap place: vec::PlaceBack }

and then use the place in this structure to produce the pointer and also call the finalize on.