@@ -366,7 +366,10 @@ impl<T> Cell<T> {
366
366
if ptr:: eq ( self , other) {
367
367
return ;
368
368
}
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.
370
373
unsafe {
371
374
ptr:: swap ( self . value . get ( ) , other. value . get ( ) ) ;
372
375
}
@@ -386,7 +389,8 @@ impl<T> Cell<T> {
386
389
/// ```
387
390
#[ stable( feature = "move_cell" , since = "1.17.0" ) ]
388
391
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.
390
394
mem:: replace ( unsafe { & mut * self . value . get ( ) } , val)
391
395
}
392
396
@@ -423,7 +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 {
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.
427
432
unsafe { * self . value . get ( ) }
428
433
}
429
434
@@ -492,7 +497,9 @@ impl<T: ?Sized> Cell<T> {
492
497
#[ inline]
493
498
#[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
494
499
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.
496
503
unsafe { & mut * self . value . get ( ) }
497
504
}
498
505
@@ -512,7 +519,7 @@ impl<T: ?Sized> Cell<T> {
512
519
#[ inline]
513
520
#[ stable( feature = "as_cell" , since = "1.37.0" ) ]
514
521
pub fn from_mut ( t : & mut T ) -> & Cell < T > {
515
- // SAFETY: `&mut` ensures unique access
522
+ // SAFETY: `&mut` ensures unique access.
516
523
unsafe { & * ( t as * mut T as * const Cell < T > ) }
517
524
}
518
525
}
@@ -556,7 +563,7 @@ impl<T> Cell<[T]> {
556
563
/// ```
557
564
#[ stable( feature = "as_cell" , since = "1.37.0" ) ]
558
565
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`.
560
567
unsafe { & * ( self as * const Cell < [ T ] > as * const [ Cell < T > ] ) }
561
568
}
562
569
}
@@ -821,7 +828,7 @@ impl<T: ?Sized> RefCell<T> {
821
828
pub fn try_borrow ( & self ) -> Result < Ref < ' _ , T > , BorrowError > {
822
829
match BorrowRef :: new ( & self . borrow ) {
823
830
// SAFETY: `BorrowRef` ensures that there is only immutable access
824
- // to the value while borrowed
831
+ // to the value while borrowed.
825
832
Some ( b) => Ok ( Ref { value : unsafe { & * self . value . get ( ) } , borrow : b } ) ,
826
833
None => Err ( BorrowError { _private : ( ) } ) ,
827
834
}
@@ -897,7 +904,7 @@ impl<T: ?Sized> RefCell<T> {
897
904
#[ inline]
898
905
pub fn try_borrow_mut ( & self ) -> Result < RefMut < ' _ , T > , BorrowMutError > {
899
906
match BorrowRefMut :: new ( & self . borrow ) {
900
- // SAFETY: `BorrowRef` guarantees unique access
907
+ // SAFETY: `BorrowRef` guarantees unique access.
901
908
Some ( b) => Ok ( RefMut { value : unsafe { & mut * self . value . get ( ) } , borrow : b } ) ,
902
909
None => Err ( BorrowMutError { _private : ( ) } ) ,
903
910
}
@@ -947,7 +954,7 @@ impl<T: ?Sized> RefCell<T> {
947
954
#[ inline]
948
955
#[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
949
956
pub fn get_mut ( & mut self ) -> & mut T {
950
- // SAFETY: `&mut` guarantees unique access
957
+ // SAFETY: `&mut` guarantees unique access.
951
958
unsafe { & mut * self . value . get ( ) }
952
959
}
953
960
0 commit comments