pub struct MBox<T: ?Sized + Free>(/* private fields */);
Expand description
A malloc-backed box. This structure allows Rust to exchange objects with C without cloning.
Implementations§
source§impl<T: ?Sized + Free> MBox<T>
impl<T: ?Sized + Free> MBox<T>
sourcepub unsafe fn from_raw(ptr: *mut T) -> Self
pub unsafe fn from_raw(ptr: *mut T) -> Self
Constructs a new malloc-backed box from a pointer allocated by malloc
.
§Safety
The ptr
must be allocated via malloc()
, calloc()
or similar C functions that is
expected to be deallocated using free()
. It must be aligned and not null. The content of the pointer
must be already initialized. The pointer’s ownership is passed into the box, and thus should
not be used after this function returns.
Note that even when T
is zero-sized, the input ptr
is still expected to be released using
free()
. Therefore, you must not use a conceived dangling pointer such as NonNull::dangling()
here. Consider using malloc(1)
in case of ZSTs.
sourcepub unsafe fn from_non_null_raw(ptr: NonNull<T>) -> Self
pub unsafe fn from_non_null_raw(ptr: NonNull<T>) -> Self
Constructs a new malloc-backed box from a non-null pointer allocated by malloc
.
§Safety
The ptr
must be allocated via malloc()
, calloc()
or similar C functions that is
expected to be deallocated using free()
. The content of the pointer must be already
initialized. The pointer’s ownership is passed into the box, and thus should not be used
after this function returns.
Note that even when T
is zero-sized, the input ptr
is still expected to be released using
free()
. Therefore, you must not use a conceived dangling pointer such as NonNull::dangling()
here. Consider using malloc(1)
in case of ZSTs.
sourcepub fn as_mut_ptr(boxed: &mut Self) -> *mut T
pub fn as_mut_ptr(boxed: &mut Self) -> *mut T
Obtains the mutable pointer owned by the box.
sourcepub fn into_raw(boxed: Self) -> *mut T
pub fn into_raw(boxed: Self) -> *mut T
Consumes the box and returns the original pointer.
The caller is responsible for free
ing the pointer after this.
sourcepub fn into_non_null_raw(boxed: Self) -> NonNull<T>
pub fn into_non_null_raw(boxed: Self) -> NonNull<T>
Consumes the box and returns the original non-null pointer.
The caller is responsible for free
ing the pointer after this.
source§impl<T> MBox<T>
impl<T> MBox<T>
sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Constructs a new malloc-backed box, and move an initialized value into it.
sourcepub fn new_uninit() -> MBox<MaybeUninit<T>>
pub fn new_uninit() -> MBox<MaybeUninit<T>>
Constructs a new malloc-backed box with uninitialized content.
sourcepub fn pin(value: T) -> Pin<Self>
pub fn pin(value: T) -> Pin<Self>
Constructs a new Pin<MBox<T>>
. If T
does not implement Unpin
, then value
will be
pinned in memory and cannot be moved.
sourcepub fn into_boxed_slice(boxed: Self) -> MBox<[T]>
pub fn into_boxed_slice(boxed: Self) -> MBox<[T]>
Converts an MBox<T>
into a single-item MBox<[T]>
.
This conversion does not allocate on the heap and happens in place.
sourcepub fn into_inner(boxed: Self) -> T
pub fn into_inner(boxed: Self) -> T
Consumes the MBox
, returning the wrapped value.
source§impl<T> MBox<MaybeUninit<T>>
impl<T> MBox<MaybeUninit<T>>
sourcepub unsafe fn assume_init(self) -> MBox<T>
pub unsafe fn assume_init(self) -> MBox<T>
source§impl<T> MBox<[T]>
impl<T> MBox<[T]>
sourcepub unsafe fn from_raw_parts(ptr: *mut T, len: usize) -> Self
pub unsafe fn from_raw_parts(ptr: *mut T, len: usize) -> Self
Constructs a new malloc-backed slice from the pointer and the length (number of items).
§Safety
ptr
must be allocated via malloc()
or similar C functions. It must be aligned and not null.
The malloc
ed size of the pointer must be at least len * size_of::<T>()
. The content
must already been initialized.
sourcepub fn new_uninit_slice(len: usize) -> MBox<[MaybeUninit<T>]>
pub fn new_uninit_slice(len: usize) -> MBox<[MaybeUninit<T>]>
Constructs a new boxed slice with uninitialized contents.
sourcepub fn into_raw_parts(self) -> (*mut T, usize)
pub fn into_raw_parts(self) -> (*mut T, usize)
Decomposes the boxed slice into a pointer to the first element and the slice length.
source§impl<T> MBox<[MaybeUninit<T>]>
impl<T> MBox<[MaybeUninit<T>]>
sourcepub unsafe fn assume_init(self) -> MBox<[T]>
pub unsafe fn assume_init(self) -> MBox<[T]>
Converts into an initialized boxed slice.
§Safety
The caller should guarantee *self
is indeed initialized.
source§impl<T: Clone> MBox<[T]>
impl<T: Clone> MBox<[T]>
sourcepub fn from_slice(slice: &[T]) -> MBox<[T]>
pub fn from_slice(slice: &[T]) -> MBox<[T]>
Creates a new malloc
-boxed slice by cloning the content of an existing slice.
source§impl MBox<str>
impl MBox<str>
sourcepub unsafe fn from_raw_utf8_parts_unchecked(
value: *mut u8,
len: usize
) -> MBox<str>
pub unsafe fn from_raw_utf8_parts_unchecked( value: *mut u8, len: usize ) -> MBox<str>
Constructs a new malloc-backed string from the pointer and the length (number of UTF-8 code units).
§Safety
The malloc
ed size of the pointer must be at least len
. The content must already been
initialized and be valid UTF-8.
sourcepub unsafe fn from_raw_utf8_parts(
value: *mut u8,
len: usize
) -> Result<MBox<str>, Utf8Error>
pub unsafe fn from_raw_utf8_parts( value: *mut u8, len: usize ) -> Result<MBox<str>, Utf8Error>
Constructs a new malloc-backed string from the pointer and the length (number of UTF-8 code
units). If the content does not contain valid UTF-8, this method returns an Err
.
§Safety
The malloc
ed size of the pointer must be at least len
.
The content must already been initialized.
sourcepub fn into_bytes(self) -> MBox<[u8]>
pub fn into_bytes(self) -> MBox<[u8]>
Converts the string into raw bytes.
Trait Implementations§
source§impl<T: ?Sized + Free> BorrowMut<T> for MBox<T>
impl<T: ?Sized + Free> BorrowMut<T> for MBox<T>
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FromIterator<T> for MBox<[T]>
impl<T> FromIterator<T> for MBox<[T]>
source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
source§impl<'a, T> IntoIterator for &'a MBox<[T]>
impl<'a, T> IntoIterator for &'a MBox<[T]>
source§impl<'a, T> IntoIterator for &'a mut MBox<[T]>
impl<'a, T> IntoIterator for &'a mut MBox<[T]>
source§impl<T> IntoIterator for MBox<[T]>
impl<T> IntoIterator for MBox<[T]>
source§impl<T: ?Sized + Free + Ord> Ord for MBox<T>
impl<T: ?Sized + Free + Ord> Ord for MBox<T>
source§impl<U: ?Sized + Free, T: ?Sized + Free + PartialEq<U>> PartialEq<MBox<U>> for MBox<T>
impl<U: ?Sized + Free, T: ?Sized + Free + PartialEq<U>> PartialEq<MBox<U>> for MBox<T>
source§impl<U: ?Sized + Free, T: ?Sized + Free + PartialOrd<U>> PartialOrd<MBox<U>> for MBox<T>
impl<U: ?Sized + Free, T: ?Sized + Free + PartialOrd<U>> PartialOrd<MBox<U>> for MBox<T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more