Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f028636

Browse files
committed
Sprinkle some whitespace & uses
1 parent c738dcc commit f028636

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

compiler/rustc_data_structures/src/tagged_ptr.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! The tag must implement the `Tag` trait. We assert that the tag and `Pointer`
1414
//! are compatible at compile time.
1515
16-
use std::mem::ManuallyDrop;
16+
use std::mem::{self, ManuallyDrop};
1717
use std::ops::Deref;
1818
use std::rc::Rc;
1919
use std::sync::Arc;
@@ -104,14 +104,17 @@ pub unsafe trait Tag: Copy {
104104

105105
unsafe impl<T> Pointer for Box<T> {
106106
const BITS: usize = bits_for::<Self::Target>();
107+
107108
#[inline]
108109
fn into_usize(self) -> usize {
109110
Box::into_raw(self) as usize
110111
}
112+
111113
#[inline]
112114
unsafe fn from_usize(ptr: usize) -> Self {
113115
Box::from_raw(ptr as *mut T)
114116
}
117+
115118
unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
116119
let raw = ManuallyDrop::new(Self::from_usize(ptr));
117120
f(&raw)
@@ -120,14 +123,17 @@ unsafe impl<T> Pointer for Box<T> {
120123

121124
unsafe impl<T> Pointer for Rc<T> {
122125
const BITS: usize = bits_for::<Self::Target>();
126+
123127
#[inline]
124128
fn into_usize(self) -> usize {
125129
Rc::into_raw(self) as usize
126130
}
131+
127132
#[inline]
128133
unsafe fn from_usize(ptr: usize) -> Self {
129134
Rc::from_raw(ptr as *const T)
130135
}
136+
131137
unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
132138
let raw = ManuallyDrop::new(Self::from_usize(ptr));
133139
f(&raw)
@@ -136,14 +142,17 @@ unsafe impl<T> Pointer for Rc<T> {
136142

137143
unsafe impl<T> Pointer for Arc<T> {
138144
const BITS: usize = bits_for::<Self::Target>();
145+
139146
#[inline]
140147
fn into_usize(self) -> usize {
141148
Arc::into_raw(self) as usize
142149
}
150+
143151
#[inline]
144152
unsafe fn from_usize(ptr: usize) -> Self {
145153
Arc::from_raw(ptr as *const T)
146154
}
155+
147156
unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
148157
let raw = ManuallyDrop::new(Self::from_usize(ptr));
149158
f(&raw)
@@ -152,14 +161,17 @@ unsafe impl<T> Pointer for Arc<T> {
152161

153162
unsafe impl<'a, T: 'a> Pointer for &'a T {
154163
const BITS: usize = bits_for::<Self::Target>();
164+
155165
#[inline]
156166
fn into_usize(self) -> usize {
157167
self as *const T as usize
158168
}
169+
159170
#[inline]
160171
unsafe fn from_usize(ptr: usize) -> Self {
161172
&*(ptr as *const T)
162173
}
174+
163175
unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
164176
f(&*(&ptr as *const usize as *const Self))
165177
}
@@ -183,7 +195,7 @@ unsafe impl<'a, T: 'a> Pointer for &'a mut T {
183195
/// Returns the number of bits available for use for tags in a pointer to `T`
184196
/// (this is based on `T`'s alignment).
185197
pub const fn bits_for<T>() -> usize {
186-
let bits = std::mem::align_of::<T>().trailing_zeros();
198+
let bits = mem::align_of::<T>().trailing_zeros();
187199

188200
// This is a replacement for `.try_into().unwrap()` unavailable in `const`
189201
// (it's fine to make an assert here, since this is only called in compile time)

compiler/rustc_data_structures/src/tagged_ptr/copy.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::stable_hasher::{HashStable, StableHasher};
33
use std::fmt;
44
use std::marker::PhantomData;
55
use std::num::NonZeroUsize;
6+
use std::ops::{Deref, DerefMut};
67

78
/// A `Copy` TaggedPtr.
89
///
@@ -73,6 +74,7 @@ where
7374
pub(super) fn pointer_raw(&self) -> usize {
7475
self.packed.get() << T::BITS
7576
}
77+
7678
pub fn pointer(self) -> P
7779
where
7880
P: Copy,
@@ -83,21 +85,25 @@ where
8385
// P: Copy
8486
unsafe { P::from_usize(self.pointer_raw()) }
8587
}
88+
8689
pub fn pointer_ref(&self) -> &P::Target {
8790
// SAFETY: pointer_raw returns the original pointer
8891
unsafe { std::mem::transmute_copy(&self.pointer_raw()) }
8992
}
93+
9094
pub fn pointer_mut(&mut self) -> &mut P::Target
9195
where
92-
P: std::ops::DerefMut,
96+
P: DerefMut,
9397
{
9498
// SAFETY: pointer_raw returns the original pointer
9599
unsafe { std::mem::transmute_copy(&self.pointer_raw()) }
96100
}
101+
97102
#[inline]
98103
pub fn tag(&self) -> T {
99104
unsafe { T::from_usize(self.packed.get() >> Self::TAG_BIT_SHIFT) }
100105
}
106+
101107
#[inline]
102108
pub fn set_tag(&mut self, tag: T) {
103109
let mut packed = self.packed.get();
@@ -109,20 +115,21 @@ where
109115
}
110116
}
111117

112-
impl<P, T, const COMPARE_PACKED: bool> std::ops::Deref for CopyTaggedPtr<P, T, COMPARE_PACKED>
118+
impl<P, T, const COMPARE_PACKED: bool> Deref for CopyTaggedPtr<P, T, COMPARE_PACKED>
113119
where
114120
P: Pointer,
115121
T: Tag,
116122
{
117123
type Target = P::Target;
124+
118125
fn deref(&self) -> &Self::Target {
119126
self.pointer_ref()
120127
}
121128
}
122129

123-
impl<P, T, const COMPARE_PACKED: bool> std::ops::DerefMut for CopyTaggedPtr<P, T, COMPARE_PACKED>
130+
impl<P, T, const COMPARE_PACKED: bool> DerefMut for CopyTaggedPtr<P, T, COMPARE_PACKED>
124131
where
125-
P: Pointer + std::ops::DerefMut,
132+
P: Pointer + DerefMut,
126133
T: Tag,
127134
{
128135
fn deref_mut(&mut self) -> &mut Self::Target {

0 commit comments

Comments
 (0)