Skip to main content

PackedPointer

Struct PackedPointer 

Source
pub struct PackedPointer<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool = true> { /* private fields */ }
Expand description

A pointer wrapper that allows storing a small amount of data in the alignment bits of the pointer.

The number of bits available for packing (DATA_BITS) must be less than or equal to the number of trailing zero bits in the alignment of T.

PackedPointer enforces compile-time validation of the alignment constraints using const assertions, and runtime debug assertions to verify pointer alignment and data limits.

If CHECK_ALIGNMENT is set to false, the compile-time alignment check is bypassed. This can be useful when working with types whose alignment cannot be validated at compile-time.

This implementation preserves pointer provenance by using Rust’s strict provenance methods (addr, with_addr, map_addr).

Implementations§

Source§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Source

pub fn new(ptr: *mut T, data: usize) -> Self

Creates a new PackedPointer from a pointer and data.

§Panics

Panics in debug builds if the pointer is not aligned to the required boundary, or if the data exceeds the allowed number of bits.

Source

pub const fn null() -> Self

Creates a new, empty packed pointer.

Source

pub fn from_ptr(ptr: *mut T) -> Self

Creates a PackedPointer from a pointer with zeroed data bits.

Source

pub const fn from_data(data: usize) -> Self

Creates a PackedPointer with a null pointer and specified data.

§Panics

Panics if the data exceeds the allowed number of bits.

Source

pub fn ptr(&self) -> *mut T

Returns the unpacked pointer, preserving its original provenance.

Source

pub fn data(&self) -> usize

Returns the unpacked data.

Source

pub fn set_ptr(&mut self, ptr: *mut T)

Sets the pointer, preserving the currently packed data.

Source

pub fn set_data(&mut self, data: usize)

Sets the data, preserving the currently packed pointer and its provenance.

Source

pub fn reset(&mut self)

Resets the packed pointer to null with zero data.

Source

pub fn is_null(&self) -> bool

Returns true if the unpacked pointer is null.

Trait Implementations§

Source§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> Clone for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> Debug for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Source§

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

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

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> Default for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> From<*mut T> for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Source§

fn from(ptr: *mut T) -> Self

Converts to this type from the input type.
Source§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> PartialEq<*const T> for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Source§

fn eq(&self, other: &*const T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> PartialEq<*mut T> for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Source§

fn eq(&self, other: &*mut T) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> PartialEq for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> Copy for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Source§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> Eq for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

Auto Trait Implementations§

§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> Freeze for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> RefUnwindSafe for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>
where T: RefUnwindSafe,

§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool = true> !Send for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool = true> !Sync for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> Unpin for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> UnsafeUnpin for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>

§

impl<T, const DATA_BITS: usize, const CHECK_ALIGNMENT: bool> UnwindSafe for PackedPointer<T, DATA_BITS, CHECK_ALIGNMENT>
where T: RefUnwindSafe,

Blanket Implementations§

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> Init<T> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
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> PinInit<T> for T

Source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
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.