Struct TaggedPtr

Source
pub struct TaggedPtr<T, const BITS: usize>(/* private fields */);
Expand description

A tagged pointer: a space-efficient representation of a pointer and integer tag.

This type stores a pointer and an integer tag without taking up more space than a normal pointer. Conceptually, this type behaves as if it contains a NonNull<T> and a certain number of bits of an integer tag.

BITS specifies how many bits are used for the tag. The alignment of T must be large enough to store this many bits: if BITS is larger than the base-2 logarithm of the alignment of T1, panics or compilation errors will occur.


  1. Because alignment is always a power of 2, this is equal to align_of::<T>().trailing_zeros()

Implementations§

Source§

impl<T, const BITS: usize> TaggedPtr<T, BITS>

Source

pub const BITS: u32

The number of tag bits that this tagged pointer can store.

Source

pub const MAX_TAG: usize

The maximum tag (inclusive) that this tagged pointer can store. Equal to (1 << BITS) - 1 (i.e., one less than 2 to the power of BITS).

Source

pub fn new(ptr: NonNull<T>, tag: usize) -> Self

Creates a new tagged pointer. Only the lower BITS bits of tag are stored.

§Panics

ptr must be aligned to at least 2BITS (i.e., 1 << BITS) or panics may occur. It is recommended that ptr be properly aligned for T, which automatically fulfills this requirement due to the restriction on BITS above.

Source

pub unsafe fn new_unchecked(ptr: NonNull<T>, tag: usize) -> Self

Equivalent to Self::new but without some runtime checks.

§Safety
  • ptr must be aligned to at least 2BITS (i.e., 1 << BITS).
  • tag cannot be greater than Self::MAX_TAG.
Source

pub unsafe fn new_unchecked_dereferenceable(ptr: NonNull<T>, tag: usize) -> Self

Like Self::new_unchecked, but the pointer must be dereferenceable, which allows better optimization.

§Safety

All conditions of Self::new_unchecked must be upheld, plus at least the first 2BITS (i.e., 1 << BITS) bytes of ptr must be “dereferenceable” in the sense defined by core::ptr.

Source§

impl<T, const BITS: usize> TaggedPtr<T, BITS>

Source

pub fn get(self) -> (NonNull<T>, usize)

Gets the pointer and tag stored by the tagged pointer.

If you need both the pointer and tag, this method may be more efficient than calling Self::ptr and Self::tag separately.

Source

pub fn ptr(self) -> NonNull<T>

Gets the pointer stored by the tagged pointer, without the tag. Equivalent to self.get().0.

Source

pub fn set_ptr(&mut self, ptr: NonNull<T>)

Sets the pointer without modifying the tag.

This method is simply equivalent to:

*self = Self::new(ptr, self.tag());

See Self::new for information on argument validity and panics.

Source

pub fn tag(self) -> usize

Gets the tag stored by the tagged pointer. Equivalent to self.get().1.

Source

pub fn set_tag(&mut self, tag: usize)

Sets the tag without modifying the pointer.

This method is simply equivalent to:

*self = Self::new(self.ptr(), tag);

See Self::new for more information.

Trait Implementations§

Source§

impl<T, const BITS: usize> Clone for TaggedPtr<T, BITS>

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, const BITS: usize> Debug for TaggedPtr<T, BITS>

Source§

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

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

impl<T, const BITS: usize> Hash for TaggedPtr<T, BITS>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T, const BITS: usize> Ord for TaggedPtr<T, BITS>

Source§

fn cmp(&self, other: &Self) -> 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, const BITS: usize> PartialEq for TaggedPtr<T, BITS>

Source§

fn eq(&self, other: &Self) -> 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, const BITS: usize> PartialOrd for TaggedPtr<T, BITS>

Source§

fn partial_cmp(&self, other: &Self) -> 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, const BITS: usize> Copy for TaggedPtr<T, BITS>

Source§

impl<T, const BITS: usize> Eq for TaggedPtr<T, BITS>

Auto Trait Implementations§

§

impl<T, const BITS: usize> Freeze for TaggedPtr<T, BITS>

§

impl<T, const BITS: usize> RefUnwindSafe for TaggedPtr<T, BITS>
where T: RefUnwindSafe,

§

impl<T, const BITS: usize> !Send for TaggedPtr<T, BITS>

§

impl<T, const BITS: usize> !Sync for TaggedPtr<T, BITS>

§

impl<T, const BITS: usize> Unpin for TaggedPtr<T, BITS>

§

impl<T, const BITS: usize> UnwindSafe for TaggedPtr<T, BITS>
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, 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, 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.