Help with unsound code

⚓ Rust    📅 2025-07-04    👤 surdeus    👁️ 17      

surdeus

Warning

This post was published 126 days ago. The information described in this article may have changed.

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Help with unsound code

Hi,

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

Read full topic

🏷️ rust_feed