pub struct TaggedPtr<T>(/* private fields */);
Expand description
A tagged pointer with the maximum tag size for the given type.
This type behaves like crate::TaggedPtr
but doesn’t have a BITS
parameter that determines how many tag bits to store. Instead, this type
uses the largest possible tag size for an aligned pointer to T
; see
Self::BITS
for the exact calculation.
Unlike crate::TaggedPtr
, this type always requires pointers to be
properly aligned, even if you don’t need all the available tag bits;
see Self::new
.
Implementations§
Source§impl<T> TaggedPtr<T>
impl<T> TaggedPtr<T>
Sourcepub const BITS: u32
pub const BITS: u32
The number of tag bits that this tagged pointer can store. Equal to
align_of::<T>().trailing_zeros()
(because alignment
is always a power of 2, this is the base-2 logarithm of the alignment
of T
).
Sourcepub const MAX_TAG: usize
pub const MAX_TAG: usize
The maximum tag (inclusive) that this tagged pointer can store. Equal
to align_of::<T>() - 1
.
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 Self::BITS
bits of
tag
are stored.
§Panics
This function may panic if ptr
is not properly aligned (i.e., aligned
to at least align_of::<T>()
).
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
ptr
must be properly aligned (i.e., aligned to at leastalign_of::<T>()
).tag
cannot 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 ptr
must be “dereferenceable” in the sense defined by
core::ptr
.
Source§impl<T> TaggedPtr<T>
impl<T> TaggedPtr<T>
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
.