Skip to content

Commit 6f64ae3

Browse files
committed
Move code around
1 parent 3df9a7b commit 6f64ae3

File tree

2 files changed

+57
-56
lines changed

2 files changed

+57
-56
lines changed

compiler/rustc_data_structures/src/tagged_ptr/copy.rs

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{Pointer, Tag};
22
use crate::stable_hasher::{HashStable, StableHasher};
33
use std::fmt;
4+
use std::hash::{Hash, Hasher};
45
use std::marker::PhantomData;
56
use std::mem::ManuallyDrop;
67
use std::num::NonZeroUsize;
@@ -24,25 +25,6 @@ where
2425
tag_ghost: PhantomData<T>,
2526
}
2627

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-
4628
// We pack the tag into the *upper* bits of the pointer to ease retrieval of the
4729
// value; a left shift is a multiplication and those are embeddable in
4830
// instruction encoding.
@@ -55,6 +37,27 @@ where
5537
Self { packed: Self::pack(P::into_ptr(pointer), tag), tag_ghost: PhantomData }
5638
}
5739

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+
5861
const TAG_BIT_SHIFT: usize = usize::BITS as usize - T::BITS;
5962
const ASSERTION: () = { assert!(T::BITS <= P::BITS) };
6063

@@ -103,26 +106,22 @@ where
103106
let ptr = unsafe { ManuallyDrop::new(P::from_ptr(self.pointer_raw())) };
104107
f(&ptr)
105108
}
109+
}
106110

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+
}
122117

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
126125
}
127126
}
128127

@@ -184,12 +183,12 @@ where
184183
{
185184
}
186185

187-
impl<P, T> std::hash::Hash for CopyTaggedPtr<P, T, true>
186+
impl<P, T> Hash for CopyTaggedPtr<P, T, true>
188187
where
189188
P: Pointer,
190189
T: Tag,
191190
{
192-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
191+
fn hash<H: Hasher>(&self, state: &mut H) {
193192
self.packed.hash(state);
194193
}
195194
}

compiler/rustc_data_structures/src/tagged_ptr/drop.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::fmt;
2+
use std::hash::{Hash, Hasher};
3+
use std::ops::{Deref, DerefMut};
24

35
use super::CopyTaggedPtr;
46
use super::{Pointer, Tag};
@@ -17,18 +19,6 @@ where
1719
raw: CopyTaggedPtr<P, T, COMPARE_PACKED>,
1820
}
1921

20-
impl<P, T, const CP: bool> Clone for TaggedPtr<P, T, CP>
21-
where
22-
P: Pointer + Clone,
23-
T: Tag,
24-
{
25-
fn clone(&self) -> Self {
26-
let ptr = self.raw.with_pointer_ref(P::clone);
27-
28-
Self::new(ptr, self.tag())
29-
}
30-
}
31-
3222
// We pack the tag into the *upper* bits of the pointer to ease retrieval of the
3323
// value; a right shift is a multiplication and those are embeddable in
3424
// instruction encoding.
@@ -46,7 +36,19 @@ where
4636
}
4737
}
4838

49-
impl<P, T, const CP: bool> std::ops::Deref for TaggedPtr<P, T, CP>
39+
impl<P, T, const CP: bool> Clone for TaggedPtr<P, T, CP>
40+
where
41+
P: Pointer + Clone,
42+
T: Tag,
43+
{
44+
fn clone(&self) -> Self {
45+
let ptr = self.raw.with_pointer_ref(P::clone);
46+
47+
Self::new(ptr, self.tag())
48+
}
49+
}
50+
51+
impl<P, T, const CP: bool> Deref for TaggedPtr<P, T, CP>
5052
where
5153
P: Pointer,
5254
T: Tag,
@@ -57,9 +59,9 @@ where
5759
}
5860
}
5961

60-
impl<P, T, const CP: bool> std::ops::DerefMut for TaggedPtr<P, T, CP>
62+
impl<P, T, const CP: bool> DerefMut for TaggedPtr<P, T, CP>
6163
where
62-
P: Pointer + std::ops::DerefMut,
64+
P: Pointer + DerefMut,
6365
T: Tag,
6466
{
6567
fn deref_mut(&mut self) -> &mut Self::Target {
@@ -109,12 +111,12 @@ where
109111
{
110112
}
111113

112-
impl<P, T> std::hash::Hash for TaggedPtr<P, T, true>
114+
impl<P, T> Hash for TaggedPtr<P, T, true>
113115
where
114116
P: Pointer,
115117
T: Tag,
116118
{
117-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
119+
fn hash<H: Hasher>(&self, state: &mut H) {
118120
self.raw.hash(state);
119121
}
120122
}

0 commit comments

Comments
 (0)