pub struct Box<T: ?Sized> { /* private fields */ }Expand description
A heap-allocated memory block.
Implementations§
Source§impl<T> Box<[T]>
impl<T> Box<[T]>
Sourcepub fn new_slice_zeroed(len: usize) -> Result<Self, KernelError>
pub fn new_slice_zeroed(len: usize) -> Result<Self, KernelError>
Create a new zeroed heap-allocated slice with the given length.
len - The length of the slice.
Returns a new heap-allocated slice with the given length or an error if the allocation failed.
Sourcepub const fn new_slice_empty() -> Self
pub const fn new_slice_empty() -> Self
Create a new empty slice.
Returns a new empty slice.
Sourcepub fn new_slice_uninit(
len: usize,
) -> Result<Box<[MaybeUninit<T>]>, KernelError>
pub fn new_slice_uninit( len: usize, ) -> Result<Box<[MaybeUninit<T>]>, KernelError>
Create a new uninit heap-allocated slice with the given length.
len - The length of the slice.
Returns a new heap-allocated slice with the given length or an error if the allocation failed.
Source§impl<T> Box<T>
impl<T> Box<T>
Sourcepub fn new(value: T) -> Option<Self>
pub fn new(value: T) -> Option<Self>
Create a new heap-allocated value.
value - The value to store on the heap.
Returns a new heap-allocated value or None if the allocation failed.
Sourcepub fn into_raw(self) -> NonNull<T>
pub fn into_raw(self) -> NonNull<T>
Consumes the Box, returning a pointer to the heap-allocated value.
The caller is responsible for freeing the memory with the global free function.
A pointer created with this function can be converted back into a Box with the from_raw function.
Sourcepub unsafe fn from_raw(ptr: NonNull<T>) -> Self
pub unsafe fn from_raw(ptr: NonNull<T>) -> Self
Moves a pointer to a heap-allocated value into a Box.
ptr - The pointer to the heap-allocated value.
Returns a new Box managing the given pointer.
§Safety
The caller must ensure that the pointer is valid and that the memory is not freed while the Box is alive.
The caller must ensure that the following conditions are met:
- The pointer must be allocated with the global
mallocfunction. - The pointer must be unique and not aliased.
- The pointer must be properly aligned.
- The pointer must point to a valid
T.
The Box takes ownership of the memory and will free it with the global allocator when dropped.