1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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<P, T, const COMPARE_PACKED: bool>
where
    P: Pointer,
    T: Tag,
{
    packed: NonZeroUsize,
    data: PhantomData<(P, T)>,
}

impl<P, T, const COMPARE_PACKED: bool> Copy for CopyTaggedPtr<P, T, COMPARE_PACKED>
where
    P: Pointer,
    T: Tag,
    P: Copy,
{
}

impl<P, T, const COMPARE_PACKED: bool> Clone for CopyTaggedPtr<P, T, COMPARE_PACKED>
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<P, T, const COMPARE_PACKED: bool> CopyTaggedPtr<P, T, COMPARE_PACKED>
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::<usize>());
    };

    pub fn new(pointer: P, tag: T) -> Self {
        // Trigger assert!
        let () = Self::ASSERTION;
        let packed_tag = tag.into_usize() << Self::TAG_BIT_SHIFT;

        Self {
            // SAFETY: We know that the pointer is non-null, as it must be
            // dereferenceable per `Pointer` safety contract.
            packed: unsafe {
                NonZeroUsize::new_unchecked((P::into_usize(pointer) >> T::BITS) | packed_tag)
            },
            data: PhantomData,
        }
    }

    pub(super) fn pointer_raw(&self) -> usize {
        self.packed.get() << T::BITS
    }
    pub fn pointer(self) -> P
    where
        P: Copy,
    {
        // SAFETY: pointer_raw returns the original pointer
        //
        // Note that this isn't going to double-drop or anything because we have
        // P: Copy
        unsafe { P::from_usize(self.pointer_raw()) }
    }
    pub fn pointer_ref(&self) -> &P::Target {
        // SAFETY: pointer_raw returns the original pointer
        unsafe { std::mem::transmute_copy(&self.pointer_raw()) }
    }
    pub fn pointer_mut(&mut self) -> &mut P::Target
    where
        P: std::ops::DerefMut,
    {
        // SAFETY: pointer_raw returns the original pointer
        unsafe { std::mem::transmute_copy(&self.pointer_raw()) }
    }
    pub fn tag(&self) -> T {
        unsafe { T::from_usize(self.packed.get() >> Self::TAG_BIT_SHIFT) }
    }
    pub fn set_tag(&mut self, tag: T) {
        let mut packed = self.packed.get();
        let new_tag = T::into_usize(tag) << Self::TAG_BIT_SHIFT;
        let tag_mask = (1 << T::BITS) - 1;
        packed &= !(tag_mask << Self::TAG_BIT_SHIFT);
        packed |= new_tag;
        self.packed = unsafe { NonZeroUsize::new_unchecked(packed) };
    }
}

impl<P, T, const COMPARE_PACKED: bool> std::ops::Deref for CopyTaggedPtr<P, T, COMPARE_PACKED>
where
    P: Pointer,
    T: Tag,
{
    type Target = P::Target;
    fn deref(&self) -> &Self::Target {
        self.pointer_ref()
    }
}

impl<P, T, const COMPARE_PACKED: bool> std::ops::DerefMut for CopyTaggedPtr<P, T, COMPARE_PACKED>
where
    P: Pointer + std::ops::DerefMut,
    T: Tag,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.pointer_mut()
    }
}

impl<P, T, const COMPARE_PACKED: bool> fmt::Debug for CopyTaggedPtr<P, T, COMPARE_PACKED>
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<P, T> PartialEq for CopyTaggedPtr<P, T, true>
where
    P: Pointer,
    T: Tag,
{
    fn eq(&self, other: &Self) -> bool {
        self.packed == other.packed
    }
}

impl<P, T> Eq for CopyTaggedPtr<P, T, true>
where
    P: Pointer,
    T: Tag,
{
}

impl<P, T> std::hash::Hash for CopyTaggedPtr<P, T, true>
where
    P: Pointer,
    T: Tag,
{
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.packed.hash(state);
    }
}

impl<P, T, HCX, const COMPARE_PACKED: bool> HashStable<HCX> for CopyTaggedPtr<P, T, COMPARE_PACKED>
where
    P: Pointer + HashStable<HCX>,
    T: Tag + HashStable<HCX>,
{
    fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
        unsafe {
            Pointer::with_ref(self.pointer_raw(), |p: &P| p.hash_stable(hcx, hasher));
        }
        self.tag().hash_stable(hcx, hasher);
    }
}