@@ -366,7 +366,10 @@ impl<T> Cell<T> {
366366 if ptr:: eq ( self , other) {
367367 return ;
368368 }
369- // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
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.
370373 unsafe {
371374 ptr:: swap ( self . value . get ( ) , other. value . get ( ) ) ;
372375 }
@@ -386,7 +389,8 @@ impl<T> Cell<T> {
386389 /// ```
387390 #[ stable( feature = "move_cell" , since = "1.17.0" ) ]
388391 pub fn replace ( & self , val : T ) -> T {
389- // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
392+ // SAFETY: This can cause data races if called from a separate thread,
393+ // but `Cell` is `!Sync` so this won't happen.
390394 mem:: replace ( unsafe { & mut * self . value . get ( ) } , val)
391395 }
392396
@@ -423,7 +427,8 @@ impl<T: Copy> Cell<T> {
423427 #[ inline]
424428 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
425429 pub fn get ( & self ) -> T {
426- // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
430+ // SAFETY: This can cause data races if called from a separate thread,
431+ // but `Cell` is `!Sync` so this won't happen.
427432 unsafe { * self . value . get ( ) }
428433 }
429434
@@ -492,7 +497,9 @@ impl<T: ?Sized> Cell<T> {
492497 #[ inline]
493498 #[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
494499 pub fn get_mut ( & mut self ) -> & mut T {
495- // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
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.
496503 unsafe { & mut * self . value . get ( ) }
497504 }
498505
@@ -512,7 +519,7 @@ impl<T: ?Sized> Cell<T> {
512519 #[ inline]
513520 #[ stable( feature = "as_cell" , since = "1.37.0" ) ]
514521 pub fn from_mut ( t : & mut T ) -> & Cell < T > {
515- // SAFETY: `&mut` ensures unique access
522+ // SAFETY: `&mut` ensures unique access.
516523 unsafe { & * ( t as * mut T as * const Cell < T > ) }
517524 }
518525}
@@ -556,7 +563,7 @@ impl<T> Cell<[T]> {
556563 /// ```
557564 #[ stable( feature = "as_cell" , since = "1.37.0" ) ]
558565 pub fn as_slice_of_cells ( & self ) -> & [ Cell < T > ] {
559- // SAFETY: `Cell<T>` has the same memory layout as `T`
566+ // SAFETY: `Cell<T>` has the same memory layout as `T`.
560567 unsafe { & * ( self as * const Cell < [ T ] > as * const [ Cell < T > ] ) }
561568 }
562569}
@@ -821,7 +828,7 @@ impl<T: ?Sized> RefCell<T> {
821828 pub fn try_borrow ( & self ) -> Result < Ref < ' _ , T > , BorrowError > {
822829 match BorrowRef :: new ( & self . borrow ) {
823830 // SAFETY: `BorrowRef` ensures that there is only immutable access
824- // to the value while borrowed
831+ // to the value while borrowed.
825832 Some ( b) => Ok ( Ref { value : unsafe { & * self . value . get ( ) } , borrow : b } ) ,
826833 None => Err ( BorrowError { _private : ( ) } ) ,
827834 }
@@ -897,7 +904,7 @@ impl<T: ?Sized> RefCell<T> {
897904 #[ inline]
898905 pub fn try_borrow_mut ( & self ) -> Result < RefMut < ' _ , T > , BorrowMutError > {
899906 match BorrowRefMut :: new ( & self . borrow ) {
900- // SAFETY: `BorrowRef` guarantees unique access
907+ // SAFETY: `BorrowRef` guarantees unique access.
901908 Some ( b) => Ok ( RefMut { value : unsafe { & mut * self . value . get ( ) } , borrow : b } ) ,
902909 None => Err ( BorrowMutError { _private : ( ) } ) ,
903910 }
@@ -947,7 +954,7 @@ impl<T: ?Sized> RefCell<T> {
947954 #[ inline]
948955 #[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
949956 pub fn get_mut ( & mut self ) -> & mut T {
950- // SAFETY: `&mut` guarantees unique access
957+ // SAFETY: `&mut` guarantees unique access.
951958 unsafe { & mut * self . value . get ( ) }
952959 }
953960
0 commit comments