Struct AllocPtr

Source
#[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>

Source

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>

Source

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>

Source

pub const fn dangling() -> Self

Constructs a dangling pointer.

Source

pub const fn cast<U>(self) -> AllocPtr<U, Alloc>

Casts an allocated pointer.

Source

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.

Source

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.

Source

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.

Source

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>

Source

pub fn alloc(alloc: &mut Alloc) -> Option<Self>

Allocates a pointer to a single element of T, prefixed by an AllocPrefix

Source

pub fn alloc_array(alloc: &mut Alloc, capacity: usize) -> Option<Self>

Allocates a pointer to an array of capacity T, prefixed by an AllocPrefix

Source

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

Source

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 · Source

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 · Source

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> Clone for AllocPtr<T, Alloc>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, Alloc: Debug> Debug for AllocPtr<T, Alloc>

Source§

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

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

impl<T, Alloc> Deref for AllocPtr<T, Alloc>

Source§

type Target = NonNull<T>

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T, Alloc> DerefMut for AllocPtr<T, Alloc>

Source§

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

Mutably dereferences the value.
Source§

impl<T, Alloc> IStable for AllocPtr<T, Alloc>

Source§

const REPORT: &'static TypeReport

A compile-time generated report of the fields of the type, allowing for compatibility inspection.
Source§

const ID: u64

A stable (and ideally unique) identifier for the type. Often generated using crate::report::gen_id, but can be manually set.
Source§

type ForbiddenValues = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::ForbiddenValues

The values that the annotated type cannot occupy.
Source§

type UnusedBits = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::UnusedBits

The padding bits in the annotated types
Source§

type Size = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::Size

The size of the annotated type in bytes.
Source§

type Align = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::Align

The alignment of the annotated type in bytes.
Source§

type HasExactlyOneNiche = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::HasExactlyOneNiche

Allows the detection of whether or not core::option::Options are stable: Read more
Source§

type ContainsIndirections = <Struct<FieldPair<NonNull<T>, PhantomData<Alloc>>> as IStable>::ContainsIndirections

Whether or not the type contains indirections (pointers, indices in independent data-structures…)
Source§

fn size() -> usize

Returns the size of the type.
Source§

fn align() -> usize

Returns the alignment of the type.
Source§

unsafe fn is_invalid(ptr: *const u8) -> bool

Returns true if ptr points to memory that cannot be a valid value of Self. Read more
Source§

impl<T: Ord, Alloc: Ord> Ord for AllocPtr<T, Alloc>

Source§

fn cmp(&self, other: &AllocPtr<T, Alloc>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

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

impl<T: PartialEq, Alloc: PartialEq> PartialEq for AllocPtr<T, Alloc>

Source§

fn eq(&self, other: &AllocPtr<T, Alloc>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd, Alloc: PartialOrd> PartialOrd for AllocPtr<T, Alloc>

Source§

fn partial_cmp(&self, other: &AllocPtr<T, Alloc>) -> Option<Ordering>

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

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T, Alloc> Copy for AllocPtr<T, Alloc>

Source§

impl<T: Eq, Alloc: Eq> Eq for AllocPtr<T, Alloc>

Source§

impl<T, Alloc> StructuralPartialEq for AllocPtr<T, Alloc>

Auto Trait Implementations§

§

impl<T, Alloc> Freeze for AllocPtr<T, Alloc>

§

impl<T, Alloc> RefUnwindSafe for AllocPtr<T, Alloc>
where Alloc: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, Alloc> !Send for AllocPtr<T, Alloc>

§

impl<T, Alloc> !Sync for AllocPtr<T, Alloc>

§

impl<T, Alloc> Unpin for AllocPtr<T, Alloc>
where Alloc: Unpin,

§

impl<T, Alloc> UnwindSafe for AllocPtr<T, Alloc>
where T: RefUnwindSafe, Alloc: UnwindSafe,

Blanket Implementations§

Source§

impl<Source> AccessAs for Source

Source§

fn ref_as<T>(&self) -> <Source as IGuardRef<T>>::Guard<'_>
where Source: IGuardRef<T>, T: ?Sized,

Provides immutable access to a type as if it were its ABI-unstable equivalent.
Source§

fn mut_as<T>(&mut self) -> <Source as IGuardMut<T>>::GuardMut<'_>
where Source: IGuardMut<T>, T: ?Sized,

Provides mutable access to a type as if it were its ABI-unstable equivalent.
Source§

impl<T> Any for T
where T: IStable,

Source§

extern "C" fn report(&self) -> &'static TypeReport

The report of the type.
Source§

extern "C" fn id(&self) -> u64

The id of the type.
Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, As> IGuardMut<As> for T
where T: Into<As>, As: Into<T>,

Source§

type GuardMut<'a> = MutAs<'a, T, As> where T: 'a

The type of the guard which will clean up the temporary after applying its changes to the original.
Source§

fn guard_mut_inner(&mut self) -> <T as IGuardMut<As>>::GuardMut<'_>

Construct the temporary and guard it through a mutable reference.
Source§

impl<T, As> IGuardRef<As> for T
where T: Into<As>, As: Into<T>,

Source§

type Guard<'a> = RefAs<'a, T, As> where T: 'a

The type of the guard which will clean up the temporary.
Source§

fn guard_ref_inner(&self) -> <T as IGuardRef<As>>::Guard<'_>

Construct the temporary and guard it through an immutable reference.
Source§

impl<T> Includes<End> for T

Source§

type Output = End

The result
Source§

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

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.