Help with unsound code
⚓ Rust 📅 2025-07-04 👤 surdeus 👁️ 17Hi,
I'm trying to create an API that looks like this:
pub fn boxed(count: usize) -> AllocationMut<T> where T: Unpin {
let boxed_array = vec![T::default(); count].into_boxed_slice();
let mut pinned = Box::into_pin(boxed_array);
// My guess is that this slice doesn't live as long as it needs to?
let slice_mut = Pin::get_mut(pinned.as_mut());
let memory= NonNull::from(slice_mut);
AllocationMut {
memory,
context: Some(AllocationContext::Boxed(pinned)),
}
}
but I can't figure out what's the right way to create a reference that stays valid for as long as the object is alive (not sure if I worded it in an understandable way). Miri rightfully complains that the pointer is trying to make a reference that doesn't exist.
I've set up a playground that shows the issue.
What's the right way to make this work?
3 posts - 2 participants
🏷️ rust_feed