|
1 | 1 | use super::{Pointer, Tag};
|
2 | 2 | use crate::stable_hasher::{HashStable, StableHasher};
|
3 | 3 | use std::fmt;
|
| 4 | +use std::hash::{Hash, Hasher}; |
4 | 5 | use std::marker::PhantomData;
|
5 | 6 | use std::mem::ManuallyDrop;
|
6 | 7 | use std::num::NonZeroUsize;
|
|
24 | 25 | tag_ghost: PhantomData<T>,
|
25 | 26 | }
|
26 | 27 |
|
27 |
| -impl<P, T, const CP: bool> Copy for CopyTaggedPtr<P, T, CP> |
28 |
| -where |
29 |
| - P: Pointer, |
30 |
| - T: Tag, |
31 |
| - P: Copy, |
32 |
| -{ |
33 |
| -} |
34 |
| - |
35 |
| -impl<P, T, const CP: bool> Clone for CopyTaggedPtr<P, T, CP> |
36 |
| -where |
37 |
| - P: Pointer, |
38 |
| - T: Tag, |
39 |
| - P: Copy, |
40 |
| -{ |
41 |
| - fn clone(&self) -> Self { |
42 |
| - *self |
43 |
| - } |
44 |
| -} |
45 |
| - |
46 | 28 | // We pack the tag into the *upper* bits of the pointer to ease retrieval of the
|
47 | 29 | // value; a left shift is a multiplication and those are embeddable in
|
48 | 30 | // instruction encoding.
|
|
55 | 37 | Self { packed: Self::pack(P::into_ptr(pointer), tag), tag_ghost: PhantomData }
|
56 | 38 | }
|
57 | 39 |
|
| 40 | + pub fn pointer(self) -> P |
| 41 | + where |
| 42 | + P: Copy, |
| 43 | + { |
| 44 | + // SAFETY: pointer_raw returns the original pointer |
| 45 | + // |
| 46 | + // Note that this isn't going to double-drop or anything because we have |
| 47 | + // P: Copy |
| 48 | + unsafe { P::from_ptr(self.pointer_raw()) } |
| 49 | + } |
| 50 | + |
| 51 | + #[inline] |
| 52 | + pub fn tag(&self) -> T { |
| 53 | + unsafe { T::from_usize(self.packed.addr().get() >> Self::TAG_BIT_SHIFT) } |
| 54 | + } |
| 55 | + |
| 56 | + #[inline] |
| 57 | + pub fn set_tag(&mut self, tag: T) { |
| 58 | + self.packed = Self::pack(self.pointer_raw(), tag); |
| 59 | + } |
| 60 | + |
58 | 61 | const TAG_BIT_SHIFT: usize = usize::BITS as usize - T::BITS;
|
59 | 62 | const ASSERTION: () = { assert!(T::BITS <= P::BITS) };
|
60 | 63 |
|
@@ -103,26 +106,22 @@ where
|
103 | 106 | let ptr = unsafe { ManuallyDrop::new(P::from_ptr(self.pointer_raw())) };
|
104 | 107 | f(&ptr)
|
105 | 108 | }
|
| 109 | +} |
106 | 110 |
|
107 |
| - pub fn pointer(self) -> P |
108 |
| - where |
109 |
| - P: Copy, |
110 |
| - { |
111 |
| - // SAFETY: pointer_raw returns the original pointer |
112 |
| - // |
113 |
| - // Note that this isn't going to double-drop or anything because we have |
114 |
| - // P: Copy |
115 |
| - unsafe { P::from_ptr(self.pointer_raw()) } |
116 |
| - } |
117 |
| - |
118 |
| - #[inline] |
119 |
| - pub fn tag(&self) -> T { |
120 |
| - unsafe { T::from_usize(self.packed.addr().get() >> Self::TAG_BIT_SHIFT) } |
121 |
| - } |
| 111 | +impl<P, T, const CP: bool> Copy for CopyTaggedPtr<P, T, CP> |
| 112 | +where |
| 113 | + P: Pointer + Copy, |
| 114 | + T: Tag, |
| 115 | +{ |
| 116 | +} |
122 | 117 |
|
123 |
| - #[inline] |
124 |
| - pub fn set_tag(&mut self, tag: T) { |
125 |
| - self.packed = Self::pack(self.pointer_raw(), tag); |
| 118 | +impl<P, T, const CP: bool> Clone for CopyTaggedPtr<P, T, CP> |
| 119 | +where |
| 120 | + P: Pointer + Copy, |
| 121 | + T: Tag, |
| 122 | +{ |
| 123 | + fn clone(&self) -> Self { |
| 124 | + *self |
126 | 125 | }
|
127 | 126 | }
|
128 | 127 |
|
@@ -184,12 +183,12 @@ where
|
184 | 183 | {
|
185 | 184 | }
|
186 | 185 |
|
187 |
| -impl<P, T> std::hash::Hash for CopyTaggedPtr<P, T, true> |
| 186 | +impl<P, T> Hash for CopyTaggedPtr<P, T, true> |
188 | 187 | where
|
189 | 188 | P: Pointer,
|
190 | 189 | T: Tag,
|
191 | 190 | {
|
192 |
| - fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 191 | + fn hash<H: Hasher>(&self, state: &mut H) { |
193 | 192 | self.packed.hash(state);
|
194 | 193 | }
|
195 | 194 | }
|
|
0 commit comments