pub struct Box<T: ?Sized, A: Allocator = DefaultAllocator> { /* private fields */ }Expand description
A custom Box type appropriate for fallible allocation.
Implementations§
Source§impl<T: ?Sized, A: Allocator> Box<T, A>
impl<T: ?Sized, A: Allocator> Box<T, A>
Sourcepub const unsafe fn from_raw_in(ptr: *mut T, allocator: A) -> Self
pub const unsafe fn from_raw_in(ptr: *mut T, allocator: A) -> Self
Creates a Box from a raw pointer.
This does NOT allocate any memory. It simply takes ownership of the memory
pointed to by ptr.
§Safety
- For non-zero-sized types, the pointer must be valid and have been allocated by the same allocator.
- For zero-sized types (ZSTs), the pointer must be non-null and properly aligned.
Consider using
NonNull::dangling()to obtain such a pointer.
Sourcepub const unsafe fn from_non_null_in(ptr: NonNull<T>, allocator: A) -> Self
pub const unsafe fn from_non_null_in(ptr: NonNull<T>, allocator: A) -> Self
Constructs a box from a NonNull pointer.
§Safety
- For non-zero-sized types, the pointer must be valid and have been allocated by the same allocator.
- For zero-sized types (ZSTs), the pointer must be non-null and properly aligned.
Consider using
NonNull::dangling()to obtain such a pointer.
Sourcepub fn into_raw_with_allocator(this: Self) -> (*mut T, A)
pub fn into_raw_with_allocator(this: Self) -> (*mut T, A)
Consumes the Box, returning a mutable reference to T.
The memory will be leaked, and never deallocated.
§Note
The allocator A is also leaked. This is intentional and matches the
behavior of std::boxed::Box::leak, ensuring that a stateful allocator
remains valid as long as the leaked reference.
Consumes the Box, returning a raw pointer and the allocator.
The memory will be leaked, and never deallocated unless reconstructed.
Source§impl<T: ?Sized> Box<T, DefaultAllocator>
impl<T: ?Sized> Box<T, DefaultAllocator>
Sourcepub const unsafe fn from_raw(ptr: *mut T) -> Self
pub const unsafe fn from_raw(ptr: *mut T) -> Self
Creates a Box from a raw pointer using the default allocator.
§Safety
- For non-zero-sized types, the pointer must be valid and have been allocated by the default allocator.
- For zero-sized types (ZSTs), the pointer must be non-null and properly aligned.
Consider using
NonNull::dangling()to obtain such a pointer.
Sourcepub const unsafe fn from_non_null(ptr: NonNull<T>) -> Self
pub const unsafe fn from_non_null(ptr: NonNull<T>) -> Self
Constructs a box from a NonNull pointer using the default allocator.
§Safety
- For non-zero-sized types, the pointer must be valid and have been allocated by the default allocator.
- For zero-sized types (ZSTs), the pointer must be non-null and properly aligned.
Consider using
NonNull::dangling()to obtain such a pointer.
Source§impl<T, A: Allocator> Box<[T], A>
impl<T, A: Allocator> Box<[T], A>
Sourcepub const fn empty_slice_in(allocator: A) -> Self
pub const fn empty_slice_in(allocator: A) -> Self
Creates an empty slice Box.
Infallible because it doesn’t allocate memory.
Sourcepub fn try_new_uninit_slice_in(
len: usize,
allocator: A,
) -> Result<Box<[MaybeUninit<T>], A>, AllocError>
pub fn try_new_uninit_slice_in( len: usize, allocator: A, ) -> Result<Box<[MaybeUninit<T>], A>, AllocError>
Tries to allocate a new slice of the given length.
Sourcepub fn try_new_zeroed_slice_in(
len: usize,
allocator: A,
) -> Result<Box<[MaybeUninit<T>], A>, AllocError>
pub fn try_new_zeroed_slice_in( len: usize, allocator: A, ) -> Result<Box<[MaybeUninit<T>], A>, AllocError>
Tries to allocate a new zeroed slice of the given length.
Source§impl<T> Box<[T], DefaultAllocator>
impl<T> Box<[T], DefaultAllocator>
pub const fn empty_slice() -> Self
pub fn try_new_uninit_slice( len: usize, ) -> Result<Box<[MaybeUninit<T>], DefaultAllocator>, AllocError>
pub fn try_new_zeroed_slice( len: usize, ) -> Result<Box<[MaybeUninit<T>], DefaultAllocator>, AllocError>
Source§impl<T, A: Allocator> Box<[MaybeUninit<T>], A>
impl<T, A: Allocator> Box<[MaybeUninit<T>], A>
Sourcepub unsafe fn assume_init(self) -> Box<[T], A>
pub unsafe fn assume_init(self) -> Box<[T], A>
Sourcepub fn try_grow(this: &mut Self, new_len: usize) -> Result<(), AllocError>
pub fn try_grow(this: &mut Self, new_len: usize) -> Result<(), AllocError>
Tries to grow the slice to a new length. Returns Err if allocation fails, and the box is unchanged.
Sourcepub unsafe fn try_shrink(
this: &mut Self,
new_len: usize,
) -> Result<(), AllocError>
pub unsafe fn try_shrink( this: &mut Self, new_len: usize, ) -> Result<(), AllocError>
Tries to shrink the slice to a new length.
If new_len is 0, the box becomes empty and memory is freed.
Returns Err if allocation fails, and the box is unchanged.
§Safety
The caller must guarantee that any elements above new_len are either
uninitialized or have already been dropped. This method discards them
without running their destructors.
Source§impl<T, A: Allocator> Box<T, A>
impl<T, A: Allocator> Box<T, A>
Sourcepub fn try_new_in(value: T, allocator: A) -> Result<Self, AllocError>
pub fn try_new_in(value: T, allocator: A) -> Result<Self, AllocError>
Tries to allocate a new instance of T and move the value into it.
Sourcepub fn try_new_uninit_in(
allocator: A,
) -> Result<Box<MaybeUninit<T>, A>, AllocError>
pub fn try_new_uninit_in( allocator: A, ) -> Result<Box<MaybeUninit<T>, A>, AllocError>
Constructs a new box with uninitialized contents on the heap.
Sourcepub fn try_new_zeroed_in(
allocator: A,
) -> Result<Box<MaybeUninit<T>, A>, AllocError>
pub fn try_new_zeroed_in( allocator: A, ) -> Result<Box<MaybeUninit<T>, A>, AllocError>
Constructs a new box with uninitialized contents, filled with 0 bytes.