Struct stabby::alloc::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<T, Alloc>

source

pub const fn dangling() -> AllocPtr<T, Alloc>

Constructs a dangling pointer.

source

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

Casts an allocated pointer.

source

pub const fn prefix_skip() -> usize

The offset between self.ptr and the prefix.

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 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§

impl<T, Alloc> AllocPtr<T, Alloc>
where Alloc: IAlloc,

source

pub fn alloc(alloc: &mut Alloc) -> Option<AllocPtr<T, Alloc>>

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

source

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

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

source

pub unsafe fn realloc( self, alloc: &mut Alloc, capacity: usize ) -> Option<AllocPtr<T, Alloc>>

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 all of the following is true:

  • The pointer must be properly aligned.

  • It must be “dereferenceable” in the sense defined in the module documentation.

  • The pointer must point to an initialized instance of T.

  • You must enforce Rust’s aliasing rules, since the returned lifetime 'a is arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. In particular, while this reference exists, the memory the pointer points to must not get mutated (except inside UnsafeCell).

This applies even if the result of this method is unused! (The part about being initialized is not yet fully decided, but until it is, the only safe approach is to ensure that they are indeed initialized.)

§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 all of the following is true:

  • The pointer must be properly aligned.

  • It must be “dereferenceable” in the sense defined in the module documentation.

  • The pointer must point to an initialized instance of T.

  • You must enforce Rust’s aliasing rules, since the returned lifetime 'a is arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. In particular, while this reference exists, the memory the pointer points to must not get accessed (read or written) through any other pointer.

This applies even if the result of this method is unused! (The part about being initialized is not yet fully decided, but until it is, the only safe approach is to ensure that they are indeed initialized.)

§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) -> AllocPtr<T, Alloc>

Returns a copy 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, Alloc> Debug for AllocPtr<T, Alloc>
where T: Debug, Alloc: Debug,

source§

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

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

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

§

type Target = NonNull<T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &<AllocPtr<T, Alloc> as Deref>::Target

Dereferences the value.
source§

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

source§

fn deref_mut(&mut self) -> &mut <AllocPtr<T, Alloc> as Deref>::Target

Mutably dereferences the value.
source§

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

§

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

The values that the annotated type cannot occupy.
§

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

The padding bits in the annotated types
§

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

The size of the annotated type in bytes.
§

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

The alignment of the annotated type in bytes.
§

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
§

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§

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§

fn size() -> usize

Returns the size of the type.
source§

fn align() -> usize

Returns the alignment of the type.
source§

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

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 + PartialOrd,

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

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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

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

This method 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

This method 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

This method 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

This method 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, Alloc> Eq for AllocPtr<T, Alloc>
where T: Eq, Alloc: Eq,

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: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
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> 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> 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>,

§

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>,

§

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

§

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<T> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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.