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.
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>
impl<T, const BITS: usize> TaggedPtr<T, BITS>
Sourcepub const MAX_TAG: usize
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).
Sourcepub fn new(ptr: NonNull<T>, tag: usize) -> Self
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.
Sourcepub unsafe fn new_unchecked(ptr: NonNull<T>, tag: usize) -> Self
pub unsafe fn new_unchecked(ptr: NonNull<T>, tag: usize) -> Self
Equivalent to Self::new but without some runtime checks.
§Safety
ptrmust be aligned to at least 2BITS(i.e.,1 << BITS).tagcannot be greater thanSelf::MAX_TAG.
Sourcepub unsafe fn new_unchecked_dereferenceable(ptr: NonNull<T>, tag: usize) -> Self
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>
impl<T, const BITS: usize> TaggedPtr<T, BITS>
Sourcepub fn ptr(self) -> NonNull<T>
pub fn ptr(self) -> NonNull<T>
Gets the pointer stored by the tagged pointer, without the tag.
Equivalent to self.get().0.
Sourcepub fn set_ptr(&mut self, ptr: NonNull<T>)
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.
Sourcepub fn tag(self) -> usize
pub fn tag(self) -> usize
Gets the tag stored by the tagged pointer. Equivalent to
self.get().1.