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,7 @@ impl<T> Cell<T> {
368
366
if ptr:: eq ( self , other) {
369
367
return ;
370
368
}
369
+ // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
371
370
unsafe {
372
371
ptr:: swap ( self . value . get ( ) , other. value . get ( ) ) ;
373
372
}
@@ -387,6 +386,7 @@ impl<T> Cell<T> {
387
386
/// ```
388
387
#[ stable( feature = "move_cell" , since = "1.17.0" ) ]
389
388
pub fn replace ( & self , val : T ) -> T {
389
+ // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
390
390
mem:: replace ( unsafe { & mut * self . value . get ( ) } , val)
391
391
}
392
392
@@ -423,6 +423,7 @@ impl<T: Copy> Cell<T> {
423
423
#[ inline]
424
424
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
425
425
pub fn get ( & self ) -> T {
426
+ // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
426
427
unsafe { * self . value . get ( ) }
427
428
}
428
429
@@ -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: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
494
496
unsafe { & mut * self . value . get ( ) }
495
497
}
496
498
@@ -510,6 +512,7 @@ impl<T: ?Sized> Cell<T> {
510
512
#[ inline]
511
513
#[ stable( feature = "as_cell" , since = "1.37.0" ) ]
512
514
pub fn from_mut ( t : & mut T ) -> & Cell < T > {
515
+ // SAFETY: `&mut` ensures unique access
513
516
unsafe { & * ( t as * mut T as * const Cell < T > ) }
514
517
}
515
518
}
@@ -553,6 +556,7 @@ impl<T> Cell<[T]> {
553
556
/// ```
554
557
#[ stable( feature = "as_cell" , since = "1.37.0" ) ]
555
558
pub fn as_slice_of_cells ( & self ) -> & [ Cell < T > ] {
559
+ // SAFETY: `Cell<T>` has the same memory layout as `T`
556
560
unsafe { & * ( self as * const Cell < [ T ] > as * const [ Cell < T > ] ) }
557
561
}
558
562
}
@@ -816,6 +820,8 @@ impl<T: ?Sized> RefCell<T> {
816
820
#[ inline]
817
821
pub fn try_borrow ( & self ) -> Result < Ref < ' _ , T > , BorrowError > {
818
822
match BorrowRef :: new ( & self . borrow ) {
823
+ // SAFETY: `BorrowRef` ensures that there is only immutable access
824
+ // to the value while borrowed
819
825
Some ( b) => Ok ( Ref { value : unsafe { & * self . value . get ( ) } , borrow : b } ) ,
820
826
None => Err ( BorrowError { _private : ( ) } ) ,
821
827
}
@@ -891,6 +897,7 @@ impl<T: ?Sized> RefCell<T> {
891
897
#[ inline]
892
898
pub fn try_borrow_mut ( & self ) -> Result < RefMut < ' _ , T > , BorrowMutError > {
893
899
match BorrowRefMut :: new ( & self . borrow ) {
900
+ // SAFETY: `BorrowRef` gurantees unique access
894
901
Some ( b) => Ok ( RefMut { value : unsafe { & mut * self . value . get ( ) } , borrow : b } ) ,
895
902
None => Err ( BorrowMutError { _private : ( ) } ) ,
896
903
}
@@ -940,6 +947,7 @@ impl<T: ?Sized> RefCell<T> {
940
947
#[ inline]
941
948
#[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
942
949
pub fn get_mut ( & mut self ) -> & mut T {
950
+ // SAFETY: `&mut` guarantees unique access
943
951
unsafe { & mut * self . value . get ( ) }
944
952
}
945
953
0 commit comments