diff --git a/compiler/rustc_data_structures/src/tagged_ptr/copy.rs b/compiler/rustc_data_structures/src/tagged_ptr/copy.rs index edf429abfe99..90500b2de89d 100644 --- a/compiler/rustc_data_structures/src/tagged_ptr/copy.rs +++ b/compiler/rustc_data_structures/src/tagged_ptr/copy.rs @@ -1,6 +1,7 @@ use super::{Pointer, Tag}; use crate::stable_hasher::{HashStable, StableHasher}; use std::fmt; +use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::mem::ManuallyDrop; use std::num::NonZeroUsize; @@ -24,25 +25,6 @@ where tag_ghost: PhantomData, } -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. @@ -55,6 +37,27 @@ where Self { packed: Self::pack(P::into_ptr(pointer), tag), tag_ghost: PhantomData } } + 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_ptr(self.pointer_raw()) } + } + + #[inline] + pub fn tag(&self) -> T { + unsafe { T::from_usize(self.packed.addr().get() >> Self::TAG_BIT_SHIFT) } + } + + #[inline] + pub fn set_tag(&mut self, tag: T) { + self.packed = Self::pack(self.pointer_raw(), tag); + } + const TAG_BIT_SHIFT: usize = usize::BITS as usize - T::BITS; const ASSERTION: () = { assert!(T::BITS <= P::BITS) }; @@ -103,26 +106,22 @@ where let ptr = unsafe { ManuallyDrop::new(P::from_ptr(self.pointer_raw())) }; f(&ptr) } +} - 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_ptr(self.pointer_raw()) } - } +impl Copy for CopyTaggedPtr +where + P: Pointer + Copy, + T: Tag, +{ +} - #[inline] - pub fn tag(&self) -> T { - unsafe { T::from_usize(self.packed.addr().get() >> Self::TAG_BIT_SHIFT) } - } - - #[inline] - pub fn set_tag(&mut self, tag: T) { - self.packed = Self::pack(self.pointer_raw(), tag); +impl Clone for CopyTaggedPtr +where + P: Pointer + Copy, + T: Tag, +{ + fn clone(&self) -> Self { + *self } } @@ -184,12 +183,12 @@ where { } -impl std::hash::Hash for CopyTaggedPtr +impl Hash for CopyTaggedPtr where P: Pointer, T: Tag, { - fn hash(&self, state: &mut H) { + fn hash(&self, state: &mut H) { self.packed.hash(state); } } diff --git a/compiler/rustc_data_structures/src/tagged_ptr/drop.rs b/compiler/rustc_data_structures/src/tagged_ptr/drop.rs index a722a0f1f321..60f3e1d24610 100644 --- a/compiler/rustc_data_structures/src/tagged_ptr/drop.rs +++ b/compiler/rustc_data_structures/src/tagged_ptr/drop.rs @@ -1,4 +1,6 @@ use std::fmt; +use std::hash::{Hash, Hasher}; +use std::ops::{Deref, DerefMut}; use super::CopyTaggedPtr; use super::{Pointer, Tag}; @@ -17,18 +19,6 @@ where raw: CopyTaggedPtr, } -impl Clone for TaggedPtr -where - P: Pointer + Clone, - T: Tag, -{ - fn clone(&self) -> Self { - let ptr = self.raw.with_pointer_ref(P::clone); - - Self::new(ptr, self.tag()) - } -} - // We pack the tag into the *upper* bits of the pointer to ease retrieval of the // value; a right shift is a multiplication and those are embeddable in // instruction encoding. @@ -46,7 +36,19 @@ where } } -impl std::ops::Deref for TaggedPtr +impl Clone for TaggedPtr +where + P: Pointer + Clone, + T: Tag, +{ + fn clone(&self) -> Self { + let ptr = self.raw.with_pointer_ref(P::clone); + + Self::new(ptr, self.tag()) + } +} + +impl Deref for TaggedPtr where P: Pointer, T: Tag, @@ -57,9 +59,9 @@ where } } -impl std::ops::DerefMut for TaggedPtr +impl DerefMut for TaggedPtr where - P: Pointer + std::ops::DerefMut, + P: Pointer + DerefMut, T: Tag, { fn deref_mut(&mut self) -> &mut Self::Target { @@ -109,12 +111,12 @@ where { } -impl std::hash::Hash for TaggedPtr +impl Hash for TaggedPtr where P: Pointer, T: Tag, { - fn hash(&self, state: &mut H) { + fn hash(&self, state: &mut H) { self.raw.hash(state); } }