Struct TaggedRef

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

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

This type behaves like TaggedPtr but with a reference instead of a raw pointer.

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<'a, T, const BITS: usize> TaggedRef<'a, T, BITS>

Source

pub fn new(reference: &'a T, tag: usize) -> Self

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

Source§

impl<T, const BITS: usize> TaggedRef<'_, T, BITS>

Source

pub const BITS: u32

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

Source

pub const MAX_TAG: usize

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

Source§

impl<'a, T, const BITS: usize> TaggedRef<'a, T, BITS>

Source

pub unsafe fn new_unchecked(reference: &'a T, tag: usize) -> Self

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

§Safety

tag cannot be greater than Self::MAX_TAG.

Source

pub fn get(self) -> (&'a T, usize)

Gets the reference and tag stored by the tagged reference.

Source

pub fn get_ref(self) -> &'a T

Gets the reference stored by the tagged reference, without the tag.

Equivalent to self.get().0.

Source

pub fn set_ref(&mut self, reference: &'a T)

Sets the reference without modifying the tag.

This method is equivalent to:

*self = self.with_ref(reference);

Because this method mutates the tagged reference in-place, the new reference must have the exact same lifetime, 'a. As a consequence, any data currently borrowed for 'a by the old reference will remain borrowed even once the reference is updated.

Self::with_ref may be more flexible in some situations, as it returns a new tagged reference that can have a different lifetime.

Source

pub fn with_ref(self, reference: &T) -> TaggedRef<'_, T, BITS>

Creates a new tagged reference with the same tag but a different reference.

This method is equivalent to:

TaggedRef::new(reference, self.tag())
Source

pub fn tag(self) -> usize

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

Source

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

Sets the tag without modifying the reference.

This method is equivalent to:

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

Trait Implementations§

Source§

impl<T, const BITS: usize> AsRef<T> for TaggedRef<'_, T, BITS>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T, const BITS: usize> Clone for TaggedRef<'_, 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 TaggedRef<'_, T, BITS>
where T: Debug,

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 TaggedRef<'_, T, BITS>
where T: Hash,

Source§

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

Hashes self.get().

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 TaggedRef<'_, T, BITS>
where T: Ord,

Source§

fn cmp(&self, other: &Self) -> Ordering

Returns self.get().cmp(&other.get()).

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 TaggedRef<'_, T, BITS>
where T: PartialEq,

Source§

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

Returns self.get() == other.get().

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 TaggedRef<'_, T, BITS>
where T: PartialOrd,

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

Returns self.get().partial_cmp(&other.get()).

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> Pointer for TaggedRef<'_, 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> Copy for TaggedRef<'_, T, BITS>

Source§

impl<T, const BITS: usize> Eq for TaggedRef<'_, T, BITS>
where T: Eq,

Source§

impl<T, const BITS: usize> Send for TaggedRef<'_, T, BITS>
where T: Sync,

Source§

impl<T, const BITS: usize> Sync for TaggedRef<'_, T, BITS>
where T: Sync,

Auto Trait Implementations§

§

impl<'a, T, const BITS: usize> Freeze for TaggedRef<'a, T, BITS>

§

impl<'a, T, const BITS: usize> RefUnwindSafe for TaggedRef<'a, T, BITS>
where T: RefUnwindSafe,

§

impl<'a, T, const BITS: usize> Unpin for TaggedRef<'a, T, BITS>

§

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