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 ;
@@ -369,6 +367,7 @@ impl<T> Cell<T> {
369
367
if ptr:: eq ( self , other) {
370
368
return ;
371
369
}
370
+ // SAFETY: Cell is not Sync
372
371
unsafe {
373
372
ptr:: swap ( self . value . get ( ) , other. value . get ( ) ) ;
374
373
}
@@ -388,6 +387,7 @@ impl<T> Cell<T> {
388
387
/// ```
389
388
#[ stable( feature = "move_cell" , since = "1.17.0" ) ]
390
389
pub fn replace ( & self , val : T ) -> T {
390
+ // SAFETY: Cell is not Sync
391
391
mem:: replace ( unsafe { & mut * self . value . get ( ) } , val)
392
392
}
393
393
@@ -424,6 +424,7 @@ impl<T:Copy> Cell<T> {
424
424
#[ inline]
425
425
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
426
426
pub fn get ( & self ) -> T {
427
+ // SAFETY: Cell is not Sync
427
428
unsafe { * self . value . get ( ) }
428
429
}
429
430
@@ -491,6 +492,7 @@ impl<T: ?Sized> Cell<T> {
491
492
#[ inline]
492
493
#[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
493
494
pub fn get_mut ( & mut self ) -> & mut T {
495
+ // SAFETY: &mut ensures unique access
494
496
unsafe {
495
497
& mut * self . value . get ( )
496
498
}
@@ -512,6 +514,7 @@ impl<T: ?Sized> Cell<T> {
512
514
#[ inline]
513
515
#[ stable( feature = "as_cell" , since = "1.37.0" ) ]
514
516
pub fn from_mut ( t : & mut T ) -> & Cell < T > {
517
+ // SAFETY: &mut ensures unique access
515
518
unsafe {
516
519
& * ( t as * mut T as * const Cell < T > )
517
520
}
@@ -557,6 +560,7 @@ impl<T> Cell<[T]> {
557
560
/// ```
558
561
#[ stable( feature = "as_cell" , since = "1.37.0" ) ]
559
562
pub fn as_slice_of_cells ( & self ) -> & [ Cell < T > ] {
563
+ // SAFETY: Cell<T> has the same memory layout as T
560
564
unsafe {
561
565
& * ( self as * const Cell < [ T ] > as * const [ Cell < T > ] )
562
566
}
@@ -825,6 +829,8 @@ impl<T: ?Sized> RefCell<T> {
825
829
pub fn try_borrow ( & self ) -> Result < Ref < ' _ , T > , BorrowError > {
826
830
match BorrowRef :: new ( & self . borrow ) {
827
831
Some ( b) => Ok ( Ref {
832
+ // SAFETY: BorrowRef ensures that there is only immutable access to the value while
833
+ // borrowed
828
834
value : unsafe { & * self . value . get ( ) } ,
829
835
borrow : b,
830
836
} ) ,
@@ -903,6 +909,7 @@ impl<T: ?Sized> RefCell<T> {
903
909
pub fn try_borrow_mut ( & self ) -> Result < RefMut < ' _ , T > , BorrowMutError > {
904
910
match BorrowRefMut :: new ( & self . borrow ) {
905
911
Some ( b) => Ok ( RefMut {
912
+ // SAFETY: BorrowRef gurantees unique access
906
913
value : unsafe { & mut * self . value . get ( ) } ,
907
914
borrow : b,
908
915
} ) ,
@@ -954,6 +961,7 @@ impl<T: ?Sized> RefCell<T> {
954
961
#[ inline]
955
962
#[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
956
963
pub fn get_mut ( & mut self ) -> & mut T {
964
+ // SAFETY: &mut guarantees unique access
957
965
unsafe {
958
966
& mut * self . value . get ( )
959
967
}
0 commit comments