187187//! ```
188188//!
189189
190- // ignore-tidy-undocumented-unsafe
191-
192190#![ stable( feature = "rust1" , since = "1.0.0" ) ]
193191
194192use crate :: cmp:: Ordering ;
@@ -368,6 +366,10 @@ impl<T> Cell<T> {
368366 if ptr:: eq ( self , other) {
369367 return ;
370368 }
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.
371373 unsafe {
372374 ptr:: swap ( self . value . get ( ) , other. value . get ( ) ) ;
373375 }
@@ -387,6 +389,8 @@ impl<T> Cell<T> {
387389 /// ```
388390 #[ stable( feature = "move_cell" , since = "1.17.0" ) ]
389391 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.
390394 mem:: replace ( unsafe { & mut * self . value . get ( ) } , val)
391395 }
392396
@@ -423,6 +427,8 @@ impl<T: Copy> Cell<T> {
423427 #[ inline]
424428 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
425429 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.
426432 unsafe { * self . value . get ( ) }
427433 }
428434
@@ -491,6 +497,9 @@ impl<T: ?Sized> Cell<T> {
491497 #[ inline]
492498 #[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
493499 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.
494503 unsafe { & mut * self . value . get ( ) }
495504 }
496505
@@ -510,6 +519,7 @@ impl<T: ?Sized> Cell<T> {
510519 #[ inline]
511520 #[ stable( feature = "as_cell" , since = "1.37.0" ) ]
512521 pub fn from_mut ( t : & mut T ) -> & Cell < T > {
522+ // SAFETY: `&mut` ensures unique access.
513523 unsafe { & * ( t as * mut T as * const Cell < T > ) }
514524 }
515525}
@@ -553,6 +563,7 @@ impl<T> Cell<[T]> {
553563 /// ```
554564 #[ stable( feature = "as_cell" , since = "1.37.0" ) ]
555565 pub fn as_slice_of_cells ( & self ) -> & [ Cell < T > ] {
566+ // SAFETY: `Cell<T>` has the same memory layout as `T`.
556567 unsafe { & * ( self as * const Cell < [ T ] > as * const [ Cell < T > ] ) }
557568 }
558569}
@@ -816,6 +827,8 @@ impl<T: ?Sized> RefCell<T> {
816827 #[ inline]
817828 pub fn try_borrow ( & self ) -> Result < Ref < ' _ , T > , BorrowError > {
818829 match BorrowRef :: new ( & self . borrow ) {
830+ // SAFETY: `BorrowRef` ensures that there is only immutable access
831+ // to the value while borrowed.
819832 Some ( b) => Ok ( Ref { value : unsafe { & * self . value . get ( ) } , borrow : b } ) ,
820833 None => Err ( BorrowError { _private : ( ) } ) ,
821834 }
@@ -891,6 +904,7 @@ impl<T: ?Sized> RefCell<T> {
891904 #[ inline]
892905 pub fn try_borrow_mut ( & self ) -> Result < RefMut < ' _ , T > , BorrowMutError > {
893906 match BorrowRefMut :: new ( & self . borrow ) {
907+ // SAFETY: `BorrowRef` guarantees unique access.
894908 Some ( b) => Ok ( RefMut { value : unsafe { & mut * self . value . get ( ) } , borrow : b } ) ,
895909 None => Err ( BorrowMutError { _private : ( ) } ) ,
896910 }
@@ -940,6 +954,7 @@ impl<T: ?Sized> RefCell<T> {
940954 #[ inline]
941955 #[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
942956 pub fn get_mut ( & mut self ) -> & mut T {
957+ // SAFETY: `&mut` guarantees unique access.
943958 unsafe { & mut * self . value . get ( ) }
944959 }
945960
0 commit comments