@@ -7,7 +7,6 @@ use core::marker::PhantomData;
7
7
use core:: mem:: { self , MaybeUninit } ;
8
8
use core:: ops:: { Deref , DerefMut } ;
9
9
use core:: ptr;
10
- use core:: slice;
11
10
12
11
use crate :: guard:: Guard ;
13
12
#[ cfg( not( miri) ) ]
@@ -125,26 +124,26 @@ pub trait Pointable {
125
124
///
126
125
/// - The given `ptr` should have been initialized with [`Pointable::init`].
127
126
/// - `ptr` should not have yet been dropped by [`Pointable::drop`].
128
- /// - `ptr` should not be mutably dereferenced by [`Pointable::deref_mut `] concurrently.
129
- unsafe fn deref < ' a > ( ptr : * mut ( ) ) -> & ' a Self ;
127
+ /// - `ptr` should not be mutably dereferenced by [`Pointable::as_mut_ptr `] concurrently.
128
+ unsafe fn as_ptr ( ptr : * mut ( ) ) -> * const Self ;
130
129
131
130
/// Mutably dereferences the given pointer.
132
131
///
133
132
/// # Safety
134
133
///
135
134
/// - The given `ptr` should have been initialized with [`Pointable::init`].
136
135
/// - `ptr` should not have yet been dropped by [`Pointable::drop`].
137
- /// - `ptr` should not be dereferenced by [`Pointable::deref `] or [`Pointable::deref_mut `]
136
+ /// - `ptr` should not be dereferenced by [`Pointable::as_ptr `] or [`Pointable::as_mut_ptr `]
138
137
/// concurrently.
139
- unsafe fn deref_mut < ' a > ( ptr : * mut ( ) ) -> & ' a mut Self ;
138
+ unsafe fn as_mut_ptr ( ptr : * mut ( ) ) -> * mut Self ;
140
139
141
140
/// Drops the object pointed to by the given pointer.
142
141
///
143
142
/// # Safety
144
143
///
145
144
/// - The given `ptr` should have been initialized with [`Pointable::init`].
146
145
/// - `ptr` should not have yet been dropped by [`Pointable::drop`].
147
- /// - `ptr` should not be dereferenced by [`Pointable::deref `] or [`Pointable::deref_mut `]
146
+ /// - `ptr` should not be dereferenced by [`Pointable::as_ptr `] or [`Pointable::as_mut_ptr `]
148
147
/// concurrently.
149
148
unsafe fn drop ( ptr : * mut ( ) ) ;
150
149
}
@@ -158,12 +157,12 @@ impl<T> Pointable for T {
158
157
Box :: into_raw ( Box :: new ( init) ) . cast :: < ( ) > ( )
159
158
}
160
159
161
- unsafe fn deref < ' a > ( ptr : * mut ( ) ) -> & ' a Self {
162
- unsafe { & * ( ptr as * const T ) }
160
+ unsafe fn as_ptr ( ptr : * mut ( ) ) -> * const Self {
161
+ ptr as * const T
163
162
}
164
163
165
- unsafe fn deref_mut < ' a > ( ptr : * mut ( ) ) -> & ' a mut Self {
166
- unsafe { & mut * ptr. cast :: < T > ( ) }
164
+ unsafe fn as_mut_ptr ( ptr : * mut ( ) ) -> * mut Self {
165
+ ptr. cast :: < T > ( )
167
166
}
168
167
169
168
unsafe fn drop ( ptr : * mut ( ) ) {
@@ -225,17 +224,22 @@ impl<T> Pointable for [MaybeUninit<T>] {
225
224
}
226
225
}
227
226
228
- unsafe fn deref < ' a > ( ptr : * mut ( ) ) -> & ' a Self {
227
+ unsafe fn as_ptr ( ptr : * mut ( ) ) -> * const Self {
229
228
unsafe {
230
- let array = & * ( ptr as * const Array < T > ) ;
231
- slice:: from_raw_parts ( array. elements . as_ptr ( ) , array. len )
229
+ let len = ( * ptr. cast :: < Array < T > > ( ) ) . len ;
230
+ // Use addr_of_mut for stacked borrows: https://github.com/rust-lang/miri/issues/1976
231
+ let elements =
232
+ ptr:: addr_of_mut!( ( * ptr. cast:: <Array <T >>( ) ) . elements) . cast :: < MaybeUninit < T > > ( ) ;
233
+ ptr:: slice_from_raw_parts ( elements, len)
232
234
}
233
235
}
234
236
235
- unsafe fn deref_mut < ' a > ( ptr : * mut ( ) ) -> & ' a mut Self {
237
+ unsafe fn as_mut_ptr ( ptr : * mut ( ) ) -> * mut Self {
236
238
unsafe {
237
- let array = & mut * ptr. cast :: < Array < T > > ( ) ;
238
- slice:: from_raw_parts_mut ( array. elements . as_mut_ptr ( ) , array. len )
239
+ let len = ( * ptr. cast :: < Array < T > > ( ) ) . len ;
240
+ let elements =
241
+ ptr:: addr_of_mut!( ( * ptr. cast:: <Array <T >>( ) ) . elements) . cast :: < MaybeUninit < T > > ( ) ;
242
+ ptr:: slice_from_raw_parts_mut ( elements, len)
239
243
}
240
244
}
241
245
@@ -846,7 +850,7 @@ impl<T: ?Sized + Pointable> fmt::Pointer for Atomic<T> {
846
850
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
847
851
let data = self . data . load ( Ordering :: SeqCst ) ;
848
852
let ( raw, _) = decompose_tag :: < T > ( data) ;
849
- fmt:: Pointer :: fmt ( & ( unsafe { T :: deref ( raw) as * const _ } ) , f)
853
+ fmt:: Pointer :: fmt ( & ( unsafe { T :: as_ptr ( raw) } ) , f)
850
854
}
851
855
}
852
856
@@ -1134,14 +1138,14 @@ impl<T: ?Sized + Pointable> Deref for Owned<T> {
1134
1138
1135
1139
fn deref ( & self ) -> & T {
1136
1140
let ( raw, _) = decompose_tag :: < T > ( self . data ) ;
1137
- unsafe { T :: deref ( raw) }
1141
+ unsafe { & * T :: as_ptr ( raw) }
1138
1142
}
1139
1143
}
1140
1144
1141
1145
impl < T : ?Sized + Pointable > DerefMut for Owned < T > {
1142
1146
fn deref_mut ( & mut self ) -> & mut T {
1143
1147
let ( raw, _) = decompose_tag :: < T > ( self . data ) ;
1144
- unsafe { T :: deref_mut ( raw) }
1148
+ unsafe { & mut * T :: as_mut_ptr ( raw) }
1145
1149
}
1146
1150
}
1147
1151
@@ -1291,6 +1295,18 @@ impl<'g, T: ?Sized + Pointable> Shared<'g, T> {
1291
1295
raw. is_null ( )
1292
1296
}
1293
1297
1298
+ #[ doc( hidden) ]
1299
+ pub unsafe fn as_ptr ( & self ) -> * const T {
1300
+ let ( raw, _) = decompose_tag :: < T > ( self . data ) ;
1301
+ unsafe { T :: as_ptr ( raw) }
1302
+ }
1303
+
1304
+ #[ doc( hidden) ]
1305
+ pub unsafe fn as_mut_ptr ( & self ) -> * mut T {
1306
+ let ( raw, _) = decompose_tag :: < T > ( self . data ) ;
1307
+ unsafe { T :: as_mut_ptr ( raw) }
1308
+ }
1309
+
1294
1310
/// Dereferences the pointer.
1295
1311
///
1296
1312
/// Returns a reference to the pointee that is valid during the lifetime `'g`.
@@ -1324,8 +1340,7 @@ impl<'g, T: ?Sized + Pointable> Shared<'g, T> {
1324
1340
/// # unsafe { drop(a.into_owned()); } // avoid leak
1325
1341
/// ```
1326
1342
pub unsafe fn deref ( & self ) -> & ' g T {
1327
- let ( raw, _) = decompose_tag :: < T > ( self . data ) ;
1328
- unsafe { T :: deref ( raw) }
1343
+ unsafe { & * self . as_ptr ( ) }
1329
1344
}
1330
1345
1331
1346
/// Dereferences the pointer.
@@ -1366,8 +1381,7 @@ impl<'g, T: ?Sized + Pointable> Shared<'g, T> {
1366
1381
/// # unsafe { drop(a.into_owned()); } // avoid leak
1367
1382
/// ```
1368
1383
pub unsafe fn deref_mut ( & mut self ) -> & ' g mut T {
1369
- let ( raw, _) = decompose_tag :: < T > ( self . data ) ;
1370
- unsafe { T :: deref_mut ( raw) }
1384
+ unsafe { & mut * self . as_mut_ptr ( ) }
1371
1385
}
1372
1386
1373
1387
/// Converts the pointer to a reference.
@@ -1407,7 +1421,7 @@ impl<'g, T: ?Sized + Pointable> Shared<'g, T> {
1407
1421
if raw. is_null ( ) {
1408
1422
None
1409
1423
} else {
1410
- Some ( unsafe { T :: deref ( raw) } )
1424
+ Some ( unsafe { & * T :: as_ptr ( raw) } )
1411
1425
}
1412
1426
}
1413
1427
@@ -1569,7 +1583,7 @@ impl<T: ?Sized + Pointable> fmt::Debug for Shared<'_, T> {
1569
1583
1570
1584
impl < T : ?Sized + Pointable > fmt:: Pointer for Shared < ' _ , T > {
1571
1585
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1572
- fmt:: Pointer :: fmt ( & ( unsafe { self . deref ( ) as * const _ } ) , f)
1586
+ fmt:: Pointer :: fmt ( & ( unsafe { self . as_ptr ( ) } ) , f)
1573
1587
}
1574
1588
}
1575
1589
0 commit comments