@@ -48,7 +48,9 @@ use core::cell::UnsafeCell;
48
48
use core:: cmp;
49
49
use core:: fmt;
50
50
use core:: fmt:: { Debug , Display } ;
51
+ use core:: marker:: PhantomData ;
51
52
use core:: ops:: { Deref , DerefMut } ;
53
+ use core:: ptr:: NonNull ;
52
54
use core:: sync:: atomic;
53
55
use core:: sync:: atomic:: AtomicUsize ;
54
56
@@ -116,7 +118,7 @@ impl<T: ?Sized> AtomicRefCell<T> {
116
118
pub fn borrow ( & self ) -> AtomicRef < T > {
117
119
match AtomicBorrowRef :: try_new ( & self . borrow ) {
118
120
Ok ( borrow) => AtomicRef {
119
- value : unsafe { & * self . value . get ( ) } ,
121
+ value : unsafe { NonNull :: new_unchecked ( self . value . get ( ) ) } ,
120
122
borrow,
121
123
} ,
122
124
Err ( s) => panic ! ( "{}" , s) ,
@@ -129,7 +131,7 @@ impl<T: ?Sized> AtomicRefCell<T> {
129
131
pub fn try_borrow ( & self ) -> Result < AtomicRef < T > , BorrowError > {
130
132
match AtomicBorrowRef :: try_new ( & self . borrow ) {
131
133
Ok ( borrow) => Ok ( AtomicRef {
132
- value : unsafe { & * self . value . get ( ) } ,
134
+ value : unsafe { NonNull :: new_unchecked ( self . value . get ( ) ) } ,
133
135
borrow,
134
136
} ) ,
135
137
Err ( _) => Err ( BorrowError { _private : ( ) } ) ,
@@ -141,8 +143,9 @@ impl<T: ?Sized> AtomicRefCell<T> {
141
143
pub fn borrow_mut ( & self ) -> AtomicRefMut < T > {
142
144
match AtomicBorrowRefMut :: try_new ( & self . borrow ) {
143
145
Ok ( borrow) => AtomicRefMut {
144
- value : unsafe { & mut * self . value . get ( ) } ,
146
+ value : unsafe { NonNull :: new_unchecked ( self . value . get ( ) ) } ,
145
147
borrow,
148
+ marker : PhantomData ,
146
149
} ,
147
150
Err ( s) => panic ! ( "{}" , s) ,
148
151
}
@@ -154,8 +157,9 @@ impl<T: ?Sized> AtomicRefCell<T> {
154
157
pub fn try_borrow_mut ( & self ) -> Result < AtomicRefMut < T > , BorrowMutError > {
155
158
match AtomicBorrowRefMut :: try_new ( & self . borrow ) {
156
159
Ok ( borrow) => Ok ( AtomicRefMut {
157
- value : unsafe { & mut * self . value . get ( ) } ,
160
+ value : unsafe { NonNull :: new_unchecked ( self . value . get ( ) ) } ,
158
161
borrow,
162
+ marker : PhantomData ,
159
163
} ) ,
160
164
Err ( _) => Err ( BorrowMutError { _private : ( ) } ) ,
161
165
}
@@ -366,7 +370,7 @@ impl<'b> Clone for AtomicBorrowRef<'b> {
366
370
367
371
/// A wrapper type for an immutably borrowed value from an `AtomicRefCell<T>`.
368
372
pub struct AtomicRef < ' b , T : ?Sized + ' b > {
369
- value : & ' b T ,
373
+ value : NonNull < T > ,
370
374
borrow : AtomicBorrowRef < ' b > ,
371
375
}
372
376
@@ -375,7 +379,8 @@ impl<'b, T: ?Sized> Deref for AtomicRef<'b, T> {
375
379
376
380
#[ inline]
377
381
fn deref ( & self ) -> & T {
378
- self . value
382
+ // SAFETY: We hold shared borrow of the value.
383
+ unsafe { self . value . as_ref ( ) }
379
384
}
380
385
}
381
386
@@ -396,7 +401,7 @@ impl<'b, T: ?Sized> AtomicRef<'b, T> {
396
401
F : FnOnce ( & T ) -> & U ,
397
402
{
398
403
AtomicRef {
399
- value : f ( orig. value ) ,
404
+ value : NonNull :: from ( f ( & * orig) ) ,
400
405
borrow : orig. borrow ,
401
406
}
402
407
}
@@ -408,7 +413,7 @@ impl<'b, T: ?Sized> AtomicRef<'b, T> {
408
413
F : FnOnce ( & T ) -> Option < & U > ,
409
414
{
410
415
Some ( AtomicRef {
411
- value : f ( orig. value ) ? ,
416
+ value : NonNull :: from ( f ( & * orig) ? ) ,
412
417
borrow : orig. borrow ,
413
418
} )
414
419
}
@@ -418,48 +423,58 @@ impl<'b, T: ?Sized> AtomicRefMut<'b, T> {
418
423
/// Make a new `AtomicRefMut` for a component of the borrowed data, e.g. an enum
419
424
/// variant.
420
425
#[ inline]
421
- pub fn map < U : ?Sized , F > ( orig : AtomicRefMut < ' b , T > , f : F ) -> AtomicRefMut < ' b , U >
426
+ pub fn map < U : ?Sized , F > ( mut orig : AtomicRefMut < ' b , T > , f : F ) -> AtomicRefMut < ' b , U >
422
427
where
423
428
F : FnOnce ( & mut T ) -> & mut U ,
424
429
{
425
430
AtomicRefMut {
426
- value : f ( orig. value ) ,
431
+ value : NonNull :: from ( f ( & mut * orig) ) ,
427
432
borrow : orig. borrow ,
433
+ marker : PhantomData ,
428
434
}
429
435
}
430
436
431
437
/// Make a new `AtomicRefMut` for an optional component of the borrowed data.
432
438
#[ inline]
433
- pub fn filter_map < U : ?Sized , F > ( orig : AtomicRefMut < ' b , T > , f : F ) -> Option < AtomicRefMut < ' b , U > >
439
+ pub fn filter_map < U : ?Sized , F > (
440
+ mut orig : AtomicRefMut < ' b , T > ,
441
+ f : F ,
442
+ ) -> Option < AtomicRefMut < ' b , U > >
434
443
where
435
444
F : FnOnce ( & mut T ) -> Option < & mut U > ,
436
445
{
437
446
Some ( AtomicRefMut {
438
- value : f ( orig. value ) ? ,
447
+ value : NonNull :: from ( f ( & mut * orig) ? ) ,
439
448
borrow : orig. borrow ,
449
+ marker : PhantomData ,
440
450
} )
441
451
}
442
452
}
443
453
444
454
/// A wrapper type for a mutably borrowed value from an `AtomicRefCell<T>`.
445
455
pub struct AtomicRefMut < ' b , T : ?Sized + ' b > {
446
- value : & ' b mut T ,
456
+ value : NonNull < T > ,
447
457
borrow : AtomicBorrowRefMut < ' b > ,
458
+ // `NonNull` is covariant over `T`, but this is used in place of a mutable
459
+ // reference so we need to be invariant over `T`.
460
+ marker : PhantomData < & ' b mut T > ,
448
461
}
449
462
450
463
impl < ' b , T : ?Sized > Deref for AtomicRefMut < ' b , T > {
451
464
type Target = T ;
452
465
453
466
#[ inline]
454
467
fn deref ( & self ) -> & T {
455
- self . value
468
+ // SAFETY: We hold an exclusive borrow of the value.
469
+ unsafe { self . value . as_ref ( ) }
456
470
}
457
471
}
458
472
459
473
impl < ' b , T : ?Sized > DerefMut for AtomicRefMut < ' b , T > {
460
474
#[ inline]
461
475
fn deref_mut ( & mut self ) -> & mut T {
462
- self . value
476
+ // SAFETY: We hold an exclusive borrow of the value.
477
+ unsafe { self . value . as_mut ( ) }
463
478
}
464
479
}
465
480
0 commit comments