How does one create a wide pointer from thin pointer?

I can't seem to find an API to create a wide pointer from thin pointer.

Wide pointer is created like this:

Box::into_non_null(Box::new_uninit_slice(size))

Since size is known from the context, I'd like to store NonNull<MaybeUninit<u8>> rather than NonNull<[MaybeUninit<u8>]>, however I can't seem to find a way to go back from thin pointer to the wide pointer and layout of wide pointer is unstable either.

I'm fine using Nighty and unstable features that are on track for stabilization.

The main reason for converting to wide pointer is to de-allocate it properly with Box::from_non_null(), but maybe alternative would be to de-allocate memory behind thin pointer, though allocator API seems to be unstable without a clear timeline when it might possibly be stabilized, so I thought re-creating Box might be easier somehow.

In the general case, the unstable feature for wide pointer manipulation is the Pointee trait, which is used in functions like NonNull::from_raw_parts().

However, specifically slice pointer manipulation is stable, usually as functions with slice in the name; in particular, NonNull::slice_from_raw_parts().

5 Likes

That seems like exactly what I need, somehow missed it before. Thank you!