#[repr(transparent)]
pub struct TaggedPtr<T, const BITS: usize>(_);
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 (unless the fallback implementation is used; see the crate documentation).

The tagged pointer conceptually holds 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; see Self::new.

Implementations§

source§

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

A check is performed at compile time to ensure that the alignment of T is not less than 2BITS (1 << BITS). This ensures that all properly aligned pointers to T will be aligned enough to store the specified number of bits of the tag.

Panics

ptr should be “dereferenceable” in the sense defined by core::ptr.1 If it is not, this function or methods of TaggedPtr may panic.

It is recommended that ptr be properly aligned (i.e., aligned to at least mem::align_of::<T>()), but it may have a smaller alignment. However, if its alignment is not at least 2BITS, this function may panic.


  1. It is permissible for only the first 2BITS bytes of ptr to be dereferenceable. 

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.

Panics

If the pointer provided to Self::new wasn’t “dereferenceable”, this method may panic.

source

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

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

Panics

If the pointer provided to Self::new wasn’t “dereferenceable”, this method may panic.

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());
Panics

See Self::new.

source

pub fn tag(self) -> usize

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

Panics

If the pointer provided to Self::new wasn’t “dereferenceable”, this method may panic.

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);
Panics

See Self::new.

Trait Implementations§

source§

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

source§

fn clone(&self) -> Self

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, 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) -> Selfwhere Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

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

source§

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

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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 Twhere 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 Twhere 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.