187
187
//! ```
188
188
//!
189
189
190
- // ignore-tidy-undocumented-unsafe
191
-
192
190
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
193
191
194
192
use crate :: cmp:: Ordering ;
@@ -368,6 +366,10 @@ impl<T> Cell<T> {
368
366
if ptr:: eq ( self , other) {
369
367
return ;
370
368
}
369
+ // SAFETY: This can be risky if called from separate threads, but `Cell`
370
+ // is `!Sync` so this won't happen. This also won't invalidate any
371
+ // pointers since `Cell` makes sure nothing else will be pointing into
372
+ // either of these `Cell`s.
371
373
unsafe {
372
374
ptr:: swap ( self . value . get ( ) , other. value . get ( ) ) ;
373
375
}
@@ -387,6 +389,8 @@ impl<T> Cell<T> {
387
389
/// ```
388
390
#[ stable( feature = "move_cell" , since = "1.17.0" ) ]
389
391
pub fn replace ( & self , val : T ) -> T {
392
+ // SAFETY: This can cause data races if called from a separate thread,
393
+ // but `Cell` is `!Sync` so this won't happen.
390
394
mem:: replace ( unsafe { & mut * self . value . get ( ) } , val)
391
395
}
392
396
@@ -423,6 +427,8 @@ impl<T: Copy> Cell<T> {
423
427
#[ inline]
424
428
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
425
429
pub fn get ( & self ) -> T {
430
+ // SAFETY: This can cause data races if called from a separate thread,
431
+ // but `Cell` is `!Sync` so this won't happen.
426
432
unsafe { * self . value . get ( ) }
427
433
}
428
434
@@ -491,6 +497,9 @@ impl<T: ?Sized> Cell<T> {
491
497
#[ inline]
492
498
#[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
493
499
pub fn get_mut ( & mut self ) -> & mut T {
500
+ // SAFETY: This can cause data races if called from a separate thread,
501
+ // but `Cell` is `!Sync` so this won't happen, and `&mut` guarantees
502
+ // unique access.
494
503
unsafe { & mut * self . value . get ( ) }
495
504
}
496
505
@@ -510,6 +519,7 @@ impl<T: ?Sized> Cell<T> {
510
519
#[ inline]
511
520
#[ stable( feature = "as_cell" , since = "1.37.0" ) ]
512
521
pub fn from_mut ( t : & mut T ) -> & Cell < T > {
522
+ // SAFETY: `&mut` ensures unique access.
513
523
unsafe { & * ( t as * mut T as * const Cell < T > ) }
514
524
}
515
525
}
@@ -553,6 +563,7 @@ impl<T> Cell<[T]> {
553
563
/// ```
554
564
#[ stable( feature = "as_cell" , since = "1.37.0" ) ]
555
565
pub fn as_slice_of_cells ( & self ) -> & [ Cell < T > ] {
566
+ // SAFETY: `Cell<T>` has the same memory layout as `T`.
556
567
unsafe { & * ( self as * const Cell < [ T ] > as * const [ Cell < T > ] ) }
557
568
}
558
569
}
@@ -816,6 +827,8 @@ impl<T: ?Sized> RefCell<T> {
816
827
#[ inline]
817
828
pub fn try_borrow ( & self ) -> Result < Ref < ' _ , T > , BorrowError > {
818
829
match BorrowRef :: new ( & self . borrow ) {
830
+ // SAFETY: `BorrowRef` ensures that there is only immutable access
831
+ // to the value while borrowed.
819
832
Some ( b) => Ok ( Ref { value : unsafe { & * self . value . get ( ) } , borrow : b } ) ,
820
833
None => Err ( BorrowError { _private : ( ) } ) ,
821
834
}
@@ -891,6 +904,7 @@ impl<T: ?Sized> RefCell<T> {
891
904
#[ inline]
892
905
pub fn try_borrow_mut ( & self ) -> Result < RefMut < ' _ , T > , BorrowMutError > {
893
906
match BorrowRefMut :: new ( & self . borrow ) {
907
+ // SAFETY: `BorrowRef` guarantees unique access.
894
908
Some ( b) => Ok ( RefMut { value : unsafe { & mut * self . value . get ( ) } , borrow : b } ) ,
895
909
None => Err ( BorrowMutError { _private : ( ) } ) ,
896
910
}
@@ -940,6 +954,7 @@ impl<T: ?Sized> RefCell<T> {
940
954
#[ inline]
941
955
#[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
942
956
pub fn get_mut ( & mut self ) -> & mut T {
957
+ // SAFETY: `&mut` guarantees unique access.
943
958
unsafe { & mut * self . value . get ( ) }
944
959
}
945
960
0 commit comments