Struct malloced::Malloced[][src]

#[repr(transparent)]
pub struct Malloced<T: ?Sized> { /* fields omitted */ }
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

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.

Consumes the instance, returning a wrapped raw pointer.

The pointer will be properly aligned and non-null.

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.

Returns an immutable raw pointer to the data.

Returns a mutable raw pointer to the data.

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.

Attempt to downcast the instance to a concrete type.

Attempt to downcast the instance to a concrete type.

Trait Implementations

Performs the conversion.

Performs the conversion.

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Performs the conversion.

Feeds this value into the given Hasher. Read more

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

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

Writes some data into this Hasher. Read more

Writes a single u8 into this hasher.

Writes a single u16 into this hasher.

Writes a single u32 into this hasher.

Writes a single u64 into this hasher.

Writes a single u128 into this hasher.

Writes a single usize into this hasher.

Writes a single i8 into this hasher.

Writes a single i16 into this hasher.

Writes a single i32 into this hasher.

Writes a single i64 into this hasher.

Writes a single i128 into this hasher.

Writes a single isize into this hasher.

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

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

This method tests for !=.

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

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

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

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

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

Formats the value using the given formatter.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.