use super::{Pointer, Tag}; use crate::stable_hasher::{HashStable, StableHasher}; use std::fmt; use std::marker::PhantomData; use std::num::NonZeroUsize; /// A `Copy` TaggedPtr. /// /// You should use this instead of the `TaggedPtr` type in all cases where /// `P: Copy`. /// /// If `COMPARE_PACKED` is true, then the pointers will be compared and hashed without /// unpacking. Otherwise we don't implement PartialEq/Eq/Hash; if you want that, /// wrap the TaggedPtr. pub struct CopyTaggedPtr
where P: Pointer, T: Tag, { packed: NonZeroUsize, data: PhantomData<(P, T)>, } impl
Copy for CopyTaggedPtr
where P: Pointer, T: Tag, P: Copy, { } impl
Clone for CopyTaggedPtr
where P: Pointer, T: Tag, P: Copy, { fn clone(&self) -> Self { *self } } // We pack the tag into the *upper* bits of the pointer to ease retrieval of the // value; a left shift is a multiplication and those are embeddable in // instruction encoding. impl
CopyTaggedPtr
where
P: Pointer,
T: Tag,
{
const TAG_BIT_SHIFT: usize = usize::BITS as usize - T::BITS;
const ASSERTION: () = {
assert!(T::BITS <= P::BITS);
// Used for the transmute_copy's below
assert!(std::mem::size_of::<&P::Target>() == std::mem::size_of:: std::ops::Deref for CopyTaggedPtr
where
P: Pointer,
T: Tag,
{
type Target = P::Target;
fn deref(&self) -> &Self::Target {
self.pointer_ref()
}
}
impl std::ops::DerefMut for CopyTaggedPtr
where
P: Pointer + std::ops::DerefMut,
T: Tag,
{
fn deref_mut(&mut self) -> &mut Self::Target {
self.pointer_mut()
}
}
impl fmt::Debug for CopyTaggedPtr
where
P: Pointer,
P::Target: fmt::Debug,
T: Tag + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CopyTaggedPtr")
.field("pointer", &self.pointer_ref())
.field("tag", &self.tag())
.finish()
}
}
impl PartialEq for CopyTaggedPtr
where
P: Pointer,
T: Tag,
{
fn eq(&self, other: &Self) -> bool {
self.packed == other.packed
}
}
impl Eq for CopyTaggedPtr
where
P: Pointer,
T: Tag,
{
}
impl std::hash::Hash for CopyTaggedPtr
where
P: Pointer,
T: Tag,
{
fn hash HashStable
where
P: Pointer + HashStable