@@ -2,7 +2,7 @@ use crate::{
2
2
access:: { Access , Copyable , ReadOnly , ReadWrite , WriteOnly } ,
3
3
volatile_ptr:: VolatilePtr ,
4
4
} ;
5
- use core:: { fmt, marker:: PhantomData , ptr:: NonNull } ;
5
+ use core:: { cmp :: Ordering , fmt, hash , marker:: PhantomData , ptr:: NonNull } ;
6
6
7
7
/// Volatile pointer type that respects Rust's aliasing rules.
8
8
///
@@ -12,6 +12,9 @@ use core::{fmt, marker::PhantomData, ptr::NonNull};
12
12
/// - only read-only types implement [`Clone`] and [`Copy`]
13
13
/// - [`Send`] and [`Sync`] are implemented if `T: Sync`
14
14
///
15
+ /// However, trait implementations like [`fmt::Debug`] and [`Eq`] behave like they do on pointer
16
+ /// types and don't access the referenced value.
17
+ ///
15
18
/// To perform volatile operations on `VolatileRef` types, use the [`as_ptr`][Self::as_ptr]
16
19
/// or [`as_mut_ptr`](Self::as_mut_ptr) methods to create a temporary
17
20
/// [`VolatilePtr`][crate::VolatilePtr] instance.
@@ -239,12 +242,56 @@ unsafe impl<T, A> Sync for VolatileRef<'_, T, A> where T: Sync {}
239
242
240
243
impl < T , A > fmt:: Debug for VolatileRef < ' _ , T , A >
241
244
where
242
- T : Copy + fmt:: Debug + ?Sized ,
245
+ T : ?Sized ,
246
+ {
247
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
248
+ fmt:: Pointer :: fmt ( & self . pointer . as_ptr ( ) , f)
249
+ }
250
+ }
251
+
252
+ impl < T , A > fmt:: Pointer for VolatileRef < ' _ , T , A >
253
+ where
254
+ T : ?Sized ,
243
255
{
244
256
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
245
- f. debug_struct ( "VolatileRef" )
246
- . field ( "pointer" , & self . pointer )
247
- . field ( "access" , & self . access )
248
- . finish ( )
257
+ fmt:: Pointer :: fmt ( & self . pointer . as_ptr ( ) , f)
258
+ }
259
+ }
260
+
261
+ impl < T , A > PartialEq for VolatileRef < ' _ , T , A >
262
+ where
263
+ T : ?Sized ,
264
+ {
265
+ fn eq ( & self , other : & Self ) -> bool {
266
+ core:: ptr:: eq ( self . pointer . as_ptr ( ) , other. pointer . as_ptr ( ) )
267
+ }
268
+ }
269
+
270
+ impl < T , A > Eq for VolatileRef < ' _ , T , A > where T : ?Sized { }
271
+
272
+ impl < T , A > PartialOrd for VolatileRef < ' _ , T , A >
273
+ where
274
+ T : ?Sized ,
275
+ {
276
+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
277
+ Some ( Ord :: cmp ( & self . pointer . as_ptr ( ) , & other. pointer . as_ptr ( ) ) )
278
+ }
279
+ }
280
+
281
+ impl < T , A > Ord for VolatileRef < ' _ , T , A >
282
+ where
283
+ T : ?Sized ,
284
+ {
285
+ fn cmp ( & self , other : & Self ) -> Ordering {
286
+ Ord :: cmp ( & self . pointer . as_ptr ( ) , & other. pointer . as_ptr ( ) )
287
+ }
288
+ }
289
+
290
+ impl < T , A > hash:: Hash for VolatileRef < ' _ , T , A >
291
+ where
292
+ T : ?Sized ,
293
+ {
294
+ fn hash < H : hash:: Hasher > ( & self , state : & mut H ) {
295
+ self . pointer . as_ptr ( ) . hash ( state) ;
249
296
}
250
297
}
0 commit comments