#[repr(C)]pub struct AllocPtr<T, Alloc> {
pub ptr: NonNull<T>,
pub marker: PhantomData<Alloc>,
}Expand description
A non-null pointer guaranteed to be preceded by a valid
AllocPrefix unless the pointer is dangling.
This means that unless T is a ZST, the pointer is guaranteed to be aligned to the maximum of T’s alignment and the alignment of the prefix, which itself is ptr-size aligned.
Fields§
§ptr: NonNull<T>The pointer to the data.
marker: PhantomData<Alloc>Remember the allocator’s type.
Implementations§
Source§impl<T, Alloc> AllocPtr<T, Alloc>where
Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>>: IStable,
PhantomData<Alloc>: IStable,
NonNull<T>: IStable,
impl<T, Alloc> AllocPtr<T, Alloc>where
Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>>: IStable,
PhantomData<Alloc>: IStable,
NonNull<T>: IStable,
Sourcepub const fn has_optimal_layout() -> bool
pub const fn has_optimal_layout() -> bool
Returns true if the layout for AllocPtr is smaller or equal to that Rust would have generated for it.
Source§impl<T, Alloc> AllocPtr<MaybeUninit<T>, Alloc>
impl<T, Alloc> AllocPtr<MaybeUninit<T>, Alloc>
Sourcepub const unsafe fn assume_init(self) -> AllocPtr<T, Alloc>
pub const unsafe fn assume_init(self) -> AllocPtr<T, Alloc>
Assumes the internals of the pointer have been initialized.
§Safety
The internals of the pointer must have been initialized.
Source§impl<T, Alloc> AllocPtr<T, Alloc>
impl<T, Alloc> AllocPtr<T, Alloc>
Sourcepub const unsafe fn prefix(&self) -> &AllocPrefix<Alloc>
pub const unsafe fn prefix(&self) -> &AllocPrefix<Alloc>
A reference to the prefix for this allocation.
§Safety
self must not be dangling, and have been properly allocated, using Self::alloc or Self::realloc for example.
Sourcepub const unsafe fn prefix_mut(&mut self) -> &mut AllocPrefix<Alloc>
pub const unsafe fn prefix_mut(&mut self) -> &mut AllocPrefix<Alloc>
A mutable reference to the prefix for this allocation.
§Safety
self must not be dangling, and have been properly allocated, using Self::alloc or Self::realloc for example.
Since this type is Copy, the &mut self is not a sufficient guarantee of uniqueness.
Sourcepub const unsafe fn split_mut(&mut self) -> (&mut AllocPrefix<Alloc>, &mut T)
pub const unsafe fn split_mut(&mut self) -> (&mut AllocPrefix<Alloc>, &mut T)
Returns mutable access to the prefix and the data.
§Safety
self must not be dangling, and have been properly allocated, using Self::alloc or Self::realloc for example.
Sourcepub unsafe fn init(ptr: NonNull<()>, capacity: usize) -> Self
pub unsafe fn init(ptr: NonNull<()>, capacity: usize) -> Self
Initializes any given pointer:
- The returned pointer is guaranteed to be correctly aligned for
T - It is guaranteed to preceded without padding by an
AllocPrefix<Alloc>
§Safety
ptr MUST be word-aligned, and MUST be valid for writes for at least the size of
#[repr(C)] struct { prefix: AllocPrefix<Alloc>, data: [T; capacity] }
Source§impl<T, Alloc: IAlloc> AllocPtr<T, Alloc>
impl<T, Alloc: IAlloc> AllocPtr<T, Alloc>
Sourcepub fn alloc(alloc: &mut Alloc) -> Option<Self>
pub fn alloc(alloc: &mut Alloc) -> Option<Self>
Allocates a pointer to a single element of T, prefixed by an AllocPrefix
Sourcepub fn alloc_array(alloc: &mut Alloc, capacity: usize) -> Option<Self>
pub fn alloc_array(alloc: &mut Alloc, capacity: usize) -> Option<Self>
Allocates a pointer to an array of capacity T, prefixed by an AllocPrefix
Sourcepub unsafe fn realloc(
self,
alloc: &mut Alloc,
prev_capacity: usize,
new_capacity: usize,
) -> Option<Self>
pub unsafe fn realloc( self, alloc: &mut Alloc, prev_capacity: usize, new_capacity: usize, ) -> Option<Self>
Reallocates a pointer to an array of capacity T, prefixed by an AllocPrefix.
In case of failure of the allocator, this will return None and self will not have been freed.
§Safety
self must not be dangling
Sourcepub unsafe fn free(self, alloc: &mut Alloc)
pub unsafe fn free(self, alloc: &mut Alloc)
Reallocates a pointer to an array of capacity T, prefixed by an AllocPrefix
§Safety
self must not be dangling, and is freed after this returns.
Methods from Deref<Target = NonNull<T>>§
1.25.0 · Sourcepub unsafe fn as_ref<'a>(&self) -> &'a T
pub unsafe fn as_ref<'a>(&self) -> &'a T
Returns a shared reference to the value. If the value may be uninitialized, as_uninit_ref
must be used instead.
For the mutable counterpart see as_mut.
§Safety
When calling this method, you have to ensure that the pointer is convertible to a reference.
§Examples
use std::ptr::NonNull;
let mut x = 0u32;
let ptr = NonNull::new(&mut x as *mut _).expect("ptr is null!");
let ref_x = unsafe { ptr.as_ref() };
println!("{ref_x}");1.25.0 · Sourcepub unsafe fn as_mut<'a>(&mut self) -> &'a mut T
pub unsafe fn as_mut<'a>(&mut self) -> &'a mut T
Returns a unique reference to the value. If the value may be uninitialized, as_uninit_mut
must be used instead.
For the shared counterpart see as_ref.
§Safety
When calling this method, you have to ensure that the pointer is convertible to a reference.
§Examples
use std::ptr::NonNull;
let mut x = 0u32;
let mut ptr = NonNull::new(&mut x).expect("null pointer");
let x_ref = unsafe { ptr.as_mut() };
assert_eq!(*x_ref, 0);
*x_ref += 2;
assert_eq!(*x_ref, 2);Trait Implementations§
Source§impl<T, Alloc> IStable for AllocPtr<T, Alloc>where
Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>>: IStable,
PhantomData<Alloc>: IStable,
NonNull<T>: IStable,
impl<T, Alloc> IStable for AllocPtr<T, Alloc>where
Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>>: IStable,
PhantomData<Alloc>: IStable,
NonNull<T>: IStable,
Source§const REPORT: &'static TypeReport
const REPORT: &'static TypeReport
Source§const ID: u64
const ID: u64
crate::report::gen_id, but can be manually set.Source§type ForbiddenValues = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::ForbiddenValues
type ForbiddenValues = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::ForbiddenValues
Source§type UnusedBits = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::UnusedBits
type UnusedBits = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::UnusedBits
Source§type Size = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::Size
type Size = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::Size
Source§type Align = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::Align
type Align = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::Align
Source§type HasExactlyOneNiche = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::HasExactlyOneNiche
type HasExactlyOneNiche = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::HasExactlyOneNiche
core::option::Options are stable: Read more