13
13
//! The tag must implement the `Tag` trait. We assert that the tag and `Pointer`
14
14
//! are compatible at compile time.
15
15
16
- use std:: mem:: ManuallyDrop ;
16
+ use std:: mem:: { self , ManuallyDrop } ;
17
17
use std:: ops:: Deref ;
18
18
use std:: rc:: Rc ;
19
19
use std:: sync:: Arc ;
@@ -104,14 +104,17 @@ pub unsafe trait Tag: Copy {
104
104
105
105
unsafe impl < T > Pointer for Box < T > {
106
106
const BITS : usize = bits_for :: < Self :: Target > ( ) ;
107
+
107
108
#[ inline]
108
109
fn into_usize ( self ) -> usize {
109
110
Box :: into_raw ( self ) as usize
110
111
}
112
+
111
113
#[ inline]
112
114
unsafe fn from_usize ( ptr : usize ) -> Self {
113
115
Box :: from_raw ( ptr as * mut T )
114
116
}
117
+
115
118
unsafe fn with_ref < R , F : FnOnce ( & Self ) -> R > ( ptr : usize , f : F ) -> R {
116
119
let raw = ManuallyDrop :: new ( Self :: from_usize ( ptr) ) ;
117
120
f ( & raw )
@@ -120,14 +123,17 @@ unsafe impl<T> Pointer for Box<T> {
120
123
121
124
unsafe impl < T > Pointer for Rc < T > {
122
125
const BITS : usize = bits_for :: < Self :: Target > ( ) ;
126
+
123
127
#[ inline]
124
128
fn into_usize ( self ) -> usize {
125
129
Rc :: into_raw ( self ) as usize
126
130
}
131
+
127
132
#[ inline]
128
133
unsafe fn from_usize ( ptr : usize ) -> Self {
129
134
Rc :: from_raw ( ptr as * const T )
130
135
}
136
+
131
137
unsafe fn with_ref < R , F : FnOnce ( & Self ) -> R > ( ptr : usize , f : F ) -> R {
132
138
let raw = ManuallyDrop :: new ( Self :: from_usize ( ptr) ) ;
133
139
f ( & raw )
@@ -136,14 +142,17 @@ unsafe impl<T> Pointer for Rc<T> {
136
142
137
143
unsafe impl < T > Pointer for Arc < T > {
138
144
const BITS : usize = bits_for :: < Self :: Target > ( ) ;
145
+
139
146
#[ inline]
140
147
fn into_usize ( self ) -> usize {
141
148
Arc :: into_raw ( self ) as usize
142
149
}
150
+
143
151
#[ inline]
144
152
unsafe fn from_usize ( ptr : usize ) -> Self {
145
153
Arc :: from_raw ( ptr as * const T )
146
154
}
155
+
147
156
unsafe fn with_ref < R , F : FnOnce ( & Self ) -> R > ( ptr : usize , f : F ) -> R {
148
157
let raw = ManuallyDrop :: new ( Self :: from_usize ( ptr) ) ;
149
158
f ( & raw )
@@ -152,14 +161,17 @@ unsafe impl<T> Pointer for Arc<T> {
152
161
153
162
unsafe impl < ' a , T : ' a > Pointer for & ' a T {
154
163
const BITS : usize = bits_for :: < Self :: Target > ( ) ;
164
+
155
165
#[ inline]
156
166
fn into_usize ( self ) -> usize {
157
167
self as * const T as usize
158
168
}
169
+
159
170
#[ inline]
160
171
unsafe fn from_usize ( ptr : usize ) -> Self {
161
172
& * ( ptr as * const T )
162
173
}
174
+
163
175
unsafe fn with_ref < R , F : FnOnce ( & Self ) -> R > ( ptr : usize , f : F ) -> R {
164
176
f ( & * ( & ptr as * const usize as * const Self ) )
165
177
}
@@ -183,7 +195,7 @@ unsafe impl<'a, T: 'a> Pointer for &'a mut T {
183
195
/// Returns the number of bits available for use for tags in a pointer to `T`
184
196
/// (this is based on `T`'s alignment).
185
197
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 ( ) ;
187
199
188
200
// This is a replacement for `.try_into().unwrap()` unavailable in `const`
189
201
// (it's fine to make an assert here, since this is only called in compile time)
0 commit comments