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>
impl<T: ?Sized> Malloced<T>
Sourcepub fn into_raw(this: Self) -> *mut T
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.
Sourcepub fn into_pin(this: Self) -> Pin<Malloced<T>>
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.
Sourcepub fn leak<'a>(this: Self) -> &'a mut Twhere
T: 'a,
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.
Sourcepub fn as_mut_ptr(this: &mut Self) -> *mut T
pub fn as_mut_ptr(this: &mut Self) -> *mut T
Returns a mutable raw pointer to the data.
Source§impl<T> Malloced<[T]>
impl<T> Malloced<[T]>
Sourcepub unsafe fn slice_from_raw_parts(data: *mut T, len: usize) -> Self
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:
-
datamust have been allocated bymallocso that it can befree-d onDrop. -
datamust be valid for both reads and writes forlen * 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.
-
datamust 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 asdatafor zero-length slices usingNonNull::dangling().
-
-
datamust point tolenconsecutive properly initialized values of typeT. -
The total size
len * mem::size_of::<T>()of the slice must be no larger thanisize::MAX. See the safety documentation ofpointer::offset.
See
slice::from_raw_parts_mut
for details.
Trait Implementations§
Source§impl<T: ?Sized> BorrowMut<T> for Malloced<T>
impl<T: ?Sized> BorrowMut<T> for Malloced<T>
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T: ?Sized + Hasher> Hasher for Malloced<T>
impl<T: ?Sized + Hasher> Hasher for Malloced<T>
Source§fn write_u128(&mut self, i: u128)
fn write_u128(&mut self, i: u128)
u128 into this hasher.Source§fn write_usize(&mut self, i: usize)
fn write_usize(&mut self, i: usize)
usize into this hasher.Source§fn write_i128(&mut self, i: i128)
fn write_i128(&mut self, i: i128)
i128 into this hasher.Source§fn write_isize(&mut self, i: isize)
fn write_isize(&mut self, i: isize)
isize into this hasher.Source§fn write_length_prefix(&mut self, len: usize)
fn write_length_prefix(&mut self, len: usize)
hasher_prefixfree_extras)