rust-lang / libs-team

The home of the library team
Apache License 2.0
123 stars 19 forks source link

Box/Vec/slice convenience NonNull methods #418

Closed theemathas closed 1 month ago

theemathas commented 2 months ago

Proposal

Problem statement

NonNull is the correct type for implementing unsafe data structures. However, using them requires extra conversions from Box and Vec, meaning that the path of least resistance is using *mut/*const raw pointers. Thus, with similar motivation to the ACP for NonNull convenience methods, I'm proposing some convenience methods on Box and Vec.

Motivating examples or use cases

The linked list book has an example of how someone might want to allocate some memory with Box, convert it into NonNull for storage, and later convert it back to Box for deallocation.

Solution sketch

I would like to proposing the following API additions:

impl<T: ?Sized, A: Allocator> Box<T, A> {
    pub fn into_non_null(b: Self) -> NonNull<T> { .... }
    pub fn into_non_null_with_allocator(b: Self) -> (NonNull<T>, A) { .... }
    pub const unsafe fn from_non_null_in(ptr: NonNull<T>, alloc: A) -> Self { .... }
}
impl<T: ?Sized> Box<T> {
    pub const unsafe fn from_non_null(ptr: NonNull<T>) -> Self { .... }
}

impl<T, A: Allocator> Vec<T, A> {
    pub fn into_non_null_parts(self) -> (NonNull<T>, usize, usize) { .... }
    pub fn into_non_null_parts_with_alloc(self) -> (NonNull<T>, usize, usize, A) { .... }
    pub fn as_non_null(&mut self) -> NonNull<T> { .... }
    pub unsafe fn from_non_null_parts_in(ptr: NonNull<T>, length: usize, capacity: usize, alloc: A) -> Self { .... }
}
impl<T> Vec<T> {
    pub unsafe fn from_non_null_parts(ptr: NonNull<T>, length: usize, capacity: usize) -> Self { .... }
}

impl<T> [T] {
    pub const fn as_non_null(&mut self) -> NonNull<T> { .... }
    pub const fn as_non_null_range(&mut self) -> Range<NonNull<T>> { .... }
}

Alternatives

The status quo: users need to do an extra layer of conversion to/from NonNull.

Vec::as_non_null might take &self instead of &mut self.

We might also want convenience methods for slices.

Links and related work

There are already some methods on NonNull<[T]>.

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

Second, if there's a concrete solution:

joshtriplett commented 1 month ago

We discussed this in today's @rust-lang/libs-api meeting. We'd like to add most of these, with some tweaks:

We're accepting that subset of the ACP, and we'd ask you to open a new ACP for the remaining methods with guidance from T-opsem.