Skip to main content

Box

Struct Box 

Source
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>

Source

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.
Source

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.
Source

pub fn as_ptr(this: &Self) -> *mut T

Returns the raw pointer.

Source

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>

Source

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.
Source

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

pub fn into_raw(this: Self) -> *mut T

Consumes the Box, returning a raw pointer.

The memory will be leaked, and never deallocated unless reconstructed.

Source§

impl<T, A: Allocator> Box<[T], A>

Source

pub const fn empty_slice_in(allocator: A) -> Self

Creates an empty slice Box.

Infallible because it doesn’t allocate memory.

Source

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.

Source

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>

Source§

impl<T, A: Allocator> Box<[MaybeUninit<T>], A>

Source

pub unsafe fn assume_init(self) -> Box<[T], A>

Converts to Box<[T], A>.

§Safety

The caller must guarantee that the values are initialized.

Source

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.

Source

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>

Source

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.

Source

pub fn try_new_uninit_in( allocator: A, ) -> Result<Box<MaybeUninit<T>, A>, AllocError>

Constructs a new box with uninitialized contents on the heap.

Source

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.

Source§

impl<T> Box<T, DefaultAllocator>

Source§

impl<T, A: Allocator> Box<MaybeUninit<T>, A>

Source

pub unsafe fn assume_init(self) -> Box<T, A>

Converts to Box<T, A>.

§Safety

The caller must guarantee that the value is initialized.

Trait Implementations§

Source§

impl<T> Default for Box<[T]>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: ?Sized, A: Allocator> Deref for Box<T, A>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: ?Sized, A: Allocator> DerefMut for Box<T, A>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T: ?Sized, A: Allocator> Drop for Box<T, A>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<T, A> Freeze for Box<T, A>
where A: Freeze, T: ?Sized,

§

impl<T, A> RefUnwindSafe for Box<T, A>

§

impl<T, A = DefaultAllocator> !Send for Box<T, A>

§

impl<T, A = DefaultAllocator> !Sync for Box<T, A>

§

impl<T, A> Unpin for Box<T, A>
where A: Unpin, T: ?Sized,

§

impl<T, A> UnsafeUnpin for Box<T, A>
where A: UnsafeUnpin, T: ?Sized,

§

impl<T, A> UnwindSafe for Box<T, A>
where A: UnwindSafe, T: RefUnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.