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,7 @@ impl<T> Cell<T> {
368366 if ptr:: eq ( self , other) {
369367 return ;
370368 }
369+ // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
371370 unsafe {
372371 ptr:: swap ( self . value . get ( ) , other. value . get ( ) ) ;
373372 }
@@ -387,6 +386,7 @@ impl<T> Cell<T> {
387386 /// ```
388387 #[ stable( feature = "move_cell" , since = "1.17.0" ) ]
389388 pub fn replace ( & self , val : T ) -> T {
389+ // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
390390 mem:: replace ( unsafe { & mut * self . value . get ( ) } , val)
391391 }
392392
@@ -423,6 +423,7 @@ impl<T: Copy> Cell<T> {
423423 #[ inline]
424424 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
425425 pub fn get ( & self ) -> T {
426+ // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
426427 unsafe { * self . value . get ( ) }
427428 }
428429
@@ -491,6 +492,7 @@ impl<T: ?Sized> Cell<T> {
491492 #[ inline]
492493 #[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
493494 pub fn get_mut ( & mut self ) -> & mut T {
495+ // SAFETY: not threadsafe, but it's OK since we know `Cell` isn't threadsafe
494496 unsafe { & mut * self . value . get ( ) }
495497 }
496498
@@ -510,6 +512,7 @@ impl<T: ?Sized> Cell<T> {
510512 #[ inline]
511513 #[ stable( feature = "as_cell" , since = "1.37.0" ) ]
512514 pub fn from_mut ( t : & mut T ) -> & Cell < T > {
515+ // SAFETY: `&mut` ensures unique access
513516 unsafe { & * ( t as * mut T as * const Cell < T > ) }
514517 }
515518}
@@ -553,6 +556,7 @@ impl<T> Cell<[T]> {
553556 /// ```
554557 #[ stable( feature = "as_cell" , since = "1.37.0" ) ]
555558 pub fn as_slice_of_cells ( & self ) -> & [ Cell < T > ] {
559+ // SAFETY: `Cell<T>` has the same memory layout as `T`
556560 unsafe { & * ( self as * const Cell < [ T ] > as * const [ Cell < T > ] ) }
557561 }
558562}
@@ -816,6 +820,8 @@ impl<T: ?Sized> RefCell<T> {
816820 #[ inline]
817821 pub fn try_borrow ( & self ) -> Result < Ref < ' _ , T > , BorrowError > {
818822 match BorrowRef :: new ( & self . borrow ) {
823+ // SAFETY: `BorrowRef` ensures that there is only immutable access
824+ // to the value while borrowed
819825 Some ( b) => Ok ( Ref { value : unsafe { & * self . value . get ( ) } , borrow : b } ) ,
820826 None => Err ( BorrowError { _private : ( ) } ) ,
821827 }
@@ -891,6 +897,7 @@ impl<T: ?Sized> RefCell<T> {
891897 #[ inline]
892898 pub fn try_borrow_mut ( & self ) -> Result < RefMut < ' _ , T > , BorrowMutError > {
893899 match BorrowRefMut :: new ( & self . borrow ) {
900+ // SAFETY: `BorrowRef` gurantees unique access
894901 Some ( b) => Ok ( RefMut { value : unsafe { & mut * self . value . get ( ) } , borrow : b } ) ,
895902 None => Err ( BorrowMutError { _private : ( ) } ) ,
896903 }
@@ -940,6 +947,7 @@ impl<T: ?Sized> RefCell<T> {
940947 #[ inline]
941948 #[ stable( feature = "cell_get_mut" , since = "1.11.0" ) ]
942949 pub fn get_mut ( & mut self ) -> & mut T {
950+ // SAFETY: `&mut` guarantees unique access
943951 unsafe { & mut * self . value . get ( ) }
944952 }
945953
0 commit comments