pub struct Box<T: ?Sized, A: Allocator = Global>(/* private fields */);Expand description
A pointer type for heap allocation.
Implementations§
Source§impl<T> Box<T>
impl<T> Box<T>
Sourcepub fn try_new(x: T) -> Result<Self, AllocError>
pub fn try_new(x: T) -> Result<Self, AllocError>
Allocates memory on the heap then places x into it,
returning an error if the allocation fails
This doesn’t actually allocate if T is zero-sized.
Source§impl<T: ?Sized> Box<T>
impl<T: ?Sized> Box<T>
Sourcepub unsafe fn from_raw(raw: *mut T) -> Self
pub unsafe fn from_raw(raw: *mut T) -> Self
Constructs a box from a raw pointer.
After calling this function, the raw pointer is owned by the
resulting Box. Specifically, the Box destructor will call
the destructor of T and free the allocated memory. For this
to be safe, the memory must have been allocated in accordance
with the [memory layout] used by Box .
§Safety
This function is unsafe because improper use may lead to memory problems. For example, a double-free may occur if the function is called twice on the same raw pointer.
Source§impl<T, A: Allocator> Box<T, A>
impl<T, A: Allocator> Box<T, A>
Sourcepub fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
pub fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
Allocates memory in the given allocator then places x into it,
returning an error if the allocation fails
This doesn’t actually allocate if T is zero-sized.
Source§impl<T: ?Sized, A: Allocator> Box<T, A>
impl<T: ?Sized, A: Allocator> Box<T, A>
Sourcepub unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self
pub unsafe fn from_raw_in(raw: *mut T, alloc: A) -> Self
Constructs a box from a raw pointer in the given allocator.
After calling this function, the raw pointer is owned by the
resulting Box. Specifically, the Box destructor will call
the destructor of T and free the allocated memory. For this
to be safe, the memory must have been allocated in accordance
with the [memory layout] used by Box .
§Safety
This function is unsafe because improper use may lead to memory problems. For example, a double-free may occur if the function is called twice on the same raw pointer.
Sourcepub fn into_raw(b: Self) -> *mut T
pub fn into_raw(b: Self) -> *mut T
Consumes the Box, returning a wrapped raw pointer.
The pointer will be properly aligned and non-null.
After calling this function, the caller is responsible for the
memory previously managed by the Box. In particular, the
caller should properly destroy T and release the memory, taking
into account the [memory layout] used by Box. The easiest way to
do this is to convert the raw pointer back into a Box with the
Box::from_raw function, allowing the Box destructor to perform
the cleanup.
Note: this is an associated function, which means that you have
to call it as Box::into_raw(b) instead of b.into_raw(). This
is so that there is no conflict with a method on the inner type.
Sourcepub fn into_raw_with_allocator(b: Self) -> (*mut T, A)
pub fn into_raw_with_allocator(b: Self) -> (*mut T, A)
Consumes the Box, returning a wrapped raw pointer and the allocator.
The pointer will be properly aligned and non-null.
After calling this function, the caller is responsible for the
memory previously managed by the Box. In particular, the
caller should properly destroy T and release the memory, taking
into account the [memory layout] used by Box. The easiest way to
do this is to convert the raw pointer back into a Box with the
Box::from_raw_in function, allowing the Box destructor to perform
the cleanup.
Note: this is an associated function, which means that you have
to call it as Box::into_raw_with_allocator(b) instead of b.into_raw_with_allocator(). This
is so that there is no conflict with a method on the inner type.
Sourcepub fn allocator(b: &Self) -> &A
pub fn allocator(b: &Self) -> &A
Returns a reference to the underlying allocator.
Note: this is an associated function, which means that you have
to call it as Box::allocator(&b) instead of b.allocator(). This
is so that there is no conflict with a method on the inner type.
Sourcepub fn leak<'a>(b: Self) -> &'a mut Twhere
A: 'a,
pub fn leak<'a>(b: Self) -> &'a mut Twhere
A: 'a,
Consumes and leaks the Box, returning a mutable reference,
&'a mut T. Note that the type T must outlive the chosen lifetime
'a. If the type has only static references, or none at all, then this
may be chosen to be 'static.
This function is mainly useful for data that lives for the remainder of
the program’s life. Dropping the returned reference will cause a memory
leak. If this is not acceptable, the reference should first be wrapped
with the Box::from_raw function producing a Box. This Box can
then be dropped which will properly destroy T and release the
allocated memory.
Note: this is an associated function, which means that you have
to call it as Box::leak(b) instead of b.leak(). This
is so that there is no conflict with a method on the inner type.
pub fn into_std(self) -> StdBox<T, A>
pub fn from_std(b: StdBox<T, A>) -> Self
Trait Implementations§
Source§impl<T: Ord + ?Sized, A: Ord + Allocator> Ord for Box<T, A>
impl<T: Ord + ?Sized, A: Ord + Allocator> Ord for Box<T, A>
Source§impl<T: PartialOrd + ?Sized, A: PartialOrd + Allocator> PartialOrd for Box<T, A>
impl<T: PartialOrd + ?Sized, A: PartialOrd + Allocator> PartialOrd for Box<T, A>
Source§impl<T: TryClone, A: Allocator + TryClone> TryClone for Box<T, A>
impl<T: TryClone, A: Allocator + TryClone> TryClone for Box<T, A>
fn try_clone(&self) -> Result<Self, AllocError>
Source§fn try_clone_from(&mut self, source: &Self) -> Result<(), AllocError>
fn try_clone_from(&mut self, source: &Self) -> Result<(), AllocError>
source. Read more