Struct malloced::Malloced

source ·
#[repr(transparent)]
pub struct Malloced<T: ?Sized> { /* private fields */ }
Expand description

A pointer type for malloc-ed heap allocation.

Memory layout

So long as T: Sized, a Malloced<T> is guaranteed to be represented as a single pointer and is also ABI-compatible with C pointers (i.e. the C type T*). This means that if you have extern “C” Rust functions that will be called from C, you can define those Rust functions using Malloced<T> types, and use T* as corresponding type on the C side.

Regardless if T: Sized, a Malloced<T> is guaranteed to be ABI-compatible with NonNull<T>.

Implementations§

source§

impl<T: ?Sized> Malloced<T>

source

pub unsafe fn from_raw(ptr: *mut T) -> Self

Constructs an instance from a raw malloc-ed pointer.

Safety

The data referenced by ptr must be valid and must have been allocated by malloc so that it can be free-d on Drop.

source

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

Consumes the instance, returning a wrapped raw pointer.

The pointer will be properly aligned and non-null.

source

pub fn into_pin(this: Self) -> Pin<Malloced<T>>

Converts a Malloced<T> into a Pin<Malloced<T>>

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

This is also available via From.

source

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

Consumes and leaks the instance, 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 Malloced::from_raw function producing a Malloced. This Malloced can then be dropped which will properly destroy T and free the allocated memory.

Note: this is an associated function, which means that you have to call it as Malloced::leak(this) instead of this.leak(). This is so that there is no conflict with a method on the inner type.

source

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

Returns an immutable raw pointer to the data.

source

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

Returns a mutable raw pointer to the data.

source

pub fn into_any(this: Self) -> Malloced<dyn Any>where T: Sized + Any,

Erases the static type T.

source

pub fn into_any_send(this: Self) -> Malloced<dyn Any + Send + Sync>where T: Sized + Any + Send + Sync,

Erases the static type T.

source§

impl<T> Malloced<[T]>

source

pub unsafe fn slice_from_raw_parts(data: *mut T, len: usize) -> Self

Constructs an instance for a slice from a pointer and a length.

Safety

Behavior is undefined if any of the following conditions are violated:

  • data must have been allocated by malloc so that it can be free-d on Drop.

  • data must be valid for both reads and writes for len * mem::size_of::<T>() many bytes, and it must be properly aligned. This means in particular:

    • The entire memory range of this slice must be contained within a single allocated object! Slices can never span across multiple allocated objects.

    • data must be non-null and aligned even for zero-length slices. One reason for this is that enum layout optimizations may rely on references (including slices of any length) being aligned and non-null to distinguish them from other data. You can obtain a pointer that is usable as data for zero-length slices using NonNull::dangling().

  • data must point to len consecutive properly initialized values of type T.

  • The total size len * mem::size_of::<T>() of the slice must be no larger than isize::MAX. See the safety documentation of pointer::offset.

See slice::from_raw_parts_mut for details.

source§

impl Malloced<CStr>

source

pub unsafe fn from_ptr(ptr: *mut c_char) -> Self

Wraps a raw malloced C string with a safe owned C string wrapper.

Safety

See CStr::from_ptr safety docs.

source§

impl Malloced<dyn Any>

source

pub fn downcast<T: Any>(self) -> Result<Malloced<T>, Self>

Attempt to downcast the instance to a concrete type.

source§

impl Malloced<dyn Any + Send>

source

pub fn downcast<T: Any>(self) -> Result<Malloced<T>, Self>

Attempt to downcast the instance to a concrete type.

source§

impl Malloced<dyn Any + Send + Sync>

source

pub fn downcast<T: Any>(self) -> Result<Malloced<T>, Self>

Attempt to downcast the instance to a concrete type.

Trait Implementations§

source§

impl<T: ?Sized> AsMut<T> for Malloced<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> AsRef<T> for Malloced<T>

source§

fn as_ref(&self) -> &T

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

impl<T: ?Sized> Borrow<T> for Malloced<T>

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T: ?Sized> BorrowMut<T> for Malloced<T>

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T: ?Sized + Debug> Debug for Malloced<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl<T: ?Sized> Deref for Malloced<T>

§

type Target = T

The resulting type after dereferencing.
source§

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

Dereferences the value.
source§

impl<T: ?Sized> DerefMut for Malloced<T>

source§

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

Mutably dereferences the value.
source§

impl<T: ?Sized + Display> Display for Malloced<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

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

impl<T: ?Sized> Drop for Malloced<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: ?Sized> From<Malloced<T>> for Pin<Malloced<T>>

source§

fn from(m: Malloced<T>) -> Self

Converts to this type from the input type.
source§

impl From<Malloced<str>> for Malloced<[u8]>

source§

fn from(m: Malloced<str>) -> Self

Converts to this type from the input type.
source§

impl<T: ?Sized + Hash> Hash for Malloced<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<T: ?Sized + Hasher> Hasher for Malloced<T>

source§

fn finish(&self) -> u64

Returns the hash value for the values written so far. Read more
source§

fn write(&mut self, bytes: &[u8])

Writes some data into this Hasher. Read more
source§

fn write_u8(&mut self, i: u8)

Writes a single u8 into this hasher.
source§

fn write_u16(&mut self, i: u16)

Writes a single u16 into this hasher.
source§

fn write_u32(&mut self, i: u32)

Writes a single u32 into this hasher.
source§

fn write_u64(&mut self, i: u64)

Writes a single u64 into this hasher.
source§

fn write_u128(&mut self, i: u128)

Writes a single u128 into this hasher.
source§

fn write_usize(&mut self, i: usize)

Writes a single usize into this hasher.
source§

fn write_i8(&mut self, i: i8)

Writes a single i8 into this hasher.
source§

fn write_i16(&mut self, i: i16)

Writes a single i16 into this hasher.
source§

fn write_i32(&mut self, i: i32)

Writes a single i32 into this hasher.
source§

fn write_i64(&mut self, i: i64)

Writes a single i64 into this hasher.
source§

fn write_i128(&mut self, i: i128)

Writes a single i128 into this hasher.
source§

fn write_isize(&mut self, i: isize)

Writes a single isize into this hasher.
source§

fn write_length_prefix(&mut self, len: usize)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a length prefix into this hasher, as part of being prefix-free. Read more
source§

fn write_str(&mut self, s: &str)

🔬This is a nightly-only experimental API. (hasher_prefixfree_extras)
Writes a single str into this hasher. Read more
source§

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

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = SliceIter<T>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: ?Sized + Ord> Ord for Malloced<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) -> Selfwhere Self: Sized,

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

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

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

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

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

impl<T: ?Sized + PartialEq> PartialEq<Malloced<T>> for Malloced<T>

source§

fn eq(&self, other: &Self) -> bool

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

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

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

impl<T: ?Sized + PartialOrd> PartialOrd<Malloced<T>> for Malloced<T>

source§

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

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

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

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

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

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

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

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

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

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

impl<T: ?Sized> Pointer for Malloced<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter.
source§

impl<T: ?Sized + Eq> Eq for Malloced<T>

source§

impl<T: ?Sized + Send> Send for Malloced<T>

source§

impl<T: ?Sized + Sync> Sync for Malloced<T>

Auto Trait Implementations§

§

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

§

impl<T: ?Sized> Unpin for Malloced<T>where T: Unpin,

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> ToString for Twhere 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.