Struct mbox::mbox::MBox

source ·
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>

source

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.

source

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.

source

pub fn as_ptr(boxed: &Self) -> *const T

Obtains the pointer owned by the box.

source

pub fn as_mut_ptr(boxed: &mut Self) -> *mut T

Obtains the mutable pointer owned by the box.

source

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

Consumes the box and returns the original pointer.

The caller is responsible for freeing the pointer after this.

source

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 freeing the pointer after this.

source§

impl<T> MBox<T>

source

pub fn new(value: T) -> Self

Constructs a new malloc-backed box, and move an initialized value into it.

source

pub fn new_uninit() -> MBox<MaybeUninit<T>>

Constructs a new malloc-backed box with uninitialized content.

source

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.

source

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.

source

pub fn into_inner(boxed: Self) -> T

Consumes the MBox, returning the wrapped value.

source

pub fn into_pin(boxed: Self) -> Pin<Self>

Converts an MBox<T> into a Pin<MBox<T>>.

This conversion does not allocate on the heap and happens in place.

source

pub fn leak<'a>(boxed: Self) -> &'a mut T
where T: 'a,

Consumes and leaks the MBox, returning a mutable reference, &'a mut T.

source§

impl<T> MBox<MaybeUninit<T>>

source

pub unsafe fn assume_init(self) -> MBox<T>

Converts into an initialized box.

§Safety

The caller should guarantee *self is indeed initialized.

source§

impl<T> MBox<[T]>

source

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 malloced size of the pointer must be at least len * size_of::<T>(). The content must already been initialized.

source

pub fn new_uninit_slice(len: usize) -> MBox<[MaybeUninit<T>]>

Constructs a new boxed slice with uninitialized contents.

source

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

source

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

source

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>

source

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 malloced size of the pointer must be at least len. The content must already been initialized and be valid UTF-8.

source

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 malloced size of the pointer must be at least len. The content must already been initialized.

source

pub fn into_bytes(self) -> MBox<[u8]>

Converts the string into raw bytes.

source

pub unsafe fn from_utf8_unchecked(bytes: MBox<[u8]>) -> MBox<str>

Creates a string from raw bytes.

§Safety

The raw bytes must be valid UTF-8.

source

pub fn from_utf8(bytes: MBox<[u8]>) -> Result<MBox<str>, Utf8Error>

Creates a string from raw bytes. If the content does not contain valid UTF-8, this method returns an Err.

Trait Implementations§

source§

impl<T: ?Sized + Free> AsMut<T> for MBox<T>

source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<T: ?Sized + Free> AsRef<T> for MBox<T>

source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<T: ?Sized + Free> Borrow<T> for MBox<T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T: ?Sized + Free> BorrowMut<T> for MBox<T>

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T: Clone> Clone for MBox<[T]>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Clone> Clone for MBox<T>

source§

fn clone(&self) -> MBox<T>

Returns a copy of the value. Read more
source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Clone for MBox<str>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: ?Sized + Free + Debug> Debug for MBox<T>

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> FormatResult

Formats the value using the given formatter. Read more
source§

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

source§

fn default() -> Self

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

impl<T: Default> Default for MBox<T>

source§

fn default() -> MBox<T>

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

impl Default for MBox<str>

source§

fn default() -> Self

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

impl<T: ?Sized + Free> Deref for MBox<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &T

Dereferences the value.
source§

impl<T: ?Sized + Free> DerefMut for MBox<T>

source§

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

Mutably dereferences the value.
source§

impl<T: ?Sized + Free + Display> Display for MBox<T>

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> FormatResult

Formats the value using the given formatter. Read more
source§

impl<T: ?Sized + Free> Drop for MBox<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl From<&str> for MBox<str>

source§

fn from(string: &str) -> Self

Creates a new malloc-boxed string by cloning the content of an existing string slice.

source§

impl<T> From<T> for MBox<T>

source§

fn from(value: T) -> MBox<T>

Converts to this type from the input type.
source§

impl<T> FromIterator<T> for MBox<[T]>

source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
source§

impl<T: ?Sized + Free + Hash> Hash for MBox<T>

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a, T> IntoIterator for &'a MBox<[T]>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
source§

impl<'a, T> IntoIterator for &'a mut MBox<[T]>

§

type Item = &'a mut T

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IterMut<'a, T>

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for MBox<[T]>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = MSliceIntoIter<T>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> MSliceIntoIter<T>

Creates an iterator from a value. Read more
source§

impl<T: ?Sized + Free + Ord> Ord for MBox<T>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<U: ?Sized + Free, T: ?Sized + Free + PartialEq<U>> PartialEq<MBox<U>> for MBox<T>

source§

fn eq(&self, other: &MBox<U>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<U: ?Sized + Free, T: ?Sized + Free + PartialOrd<U>> PartialOrd<MBox<U>> for MBox<T>

source§

fn partial_cmp(&self, other: &MBox<U>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: ?Sized + Free> Pointer for MBox<T>

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> FormatResult

Formats the value using the given formatter.
source§

impl<T: ?Sized + Free + Eq> Eq for MBox<T>

source§

impl<T: ?Sized + Free> StableDeref for MBox<T>

source§

impl<T: ?Sized + Free> Unpin for MBox<T>

Auto Trait Implementations§

§

impl<T: ?Sized> RefUnwindSafe for MBox<T>
where T: RefUnwindSafe,

§

impl<T: ?Sized> Send for MBox<T>
where T: Send,

§

impl<T: ?Sized> Sync for MBox<T>
where T: Sync,

§

impl<T: ?Sized> UnwindSafe for MBox<T>

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> Free for T

source§

unsafe fn free(ptr_ref: NonNull<T>)

Drops the content pointed by this pointer and frees it. Read more
source§

impl<T> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
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<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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

§

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.