google / moveit

Apache License 2.0
166 stars 18 forks source link

A trait to patch pointers after moving #26

Open Tamschi opened 2 years ago

Tamschi commented 2 years ago

This isn't currently useful on stable, and likely won't be until https://github.com/rust-lang/rust/issues/32838 (more specifically Allocator::grow_zeroed) and https://github.com/rust-lang/rust/issues/31844 (at least #![feature(min_specialization)]) land, but an unsafe trait with a function like

/// Repairs an instance of `Self` located at `moved` back into a valid state.
///
/// # Safety
///
/// The data referenced by `moved` must have been
///
/// - previously located at `old_address`,
/// - must have been initialized as valid instance of `Self` there and
/// - must have been moved to its new location without modification since then.
unsafe fn repair_moved(old_address: NonNull<Self>, moved: Pin<&mut MaybeUninit<Self>>);

would be potentially helpful as optimisation for reallocating pinning collections.

As I said before, I don't expect this to be currently useful, and also would not currently have a use for it myself. I just came across it while writing a blog post about pinning and wanted to jot down the idea somewhere since it doesn't fit that text.

The traits in this crate will most likely see a huge boost in helpfulness once specialisation allows them to be default-implemented for T: Unpin. Feel free to ping me when that happens and I should have time to gladly turn this feature into a pull-request (with hopefully better naming-sense, as I'm not sure repair_moved checks out and is consistent with the other traits in the crate).