#[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§
impl<T: ?Sized> Malloced<T>
Testing helpers.
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 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:
-
data
must have been allocated bymalloc
so that it can befree
-d onDrop
. -
data
must 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.
-
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 asdata
for zero-length slices usingNonNull::dangling()
.
-
-
data
must point tolen
consecutive 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
)source§impl<T> IntoIterator for Malloced<[T]>
impl<T> IntoIterator for Malloced<[T]>
source§impl<T: ?Sized + Ord> Ord for Malloced<T>
impl<T: ?Sized + Ord> Ord for Malloced<T>
source§impl<T: ?Sized + PartialOrd> PartialOrd<Malloced<T>> for Malloced<T>
impl<T: ?Sized + PartialOrd> PartialOrd<Malloced<T>> for Malloced<T>
source§fn le(&self, other: &Self) -> bool
fn le(&self, other: &Self) -> bool
self
and other
) and is used by the <=
operator. Read more