@@ -782,7 +782,7 @@ impl<T: 'static> LocalKey<RefCell<T>> {
782
782
mod lazy {
783
783
use crate :: cell:: UnsafeCell ;
784
784
use crate :: hint;
785
- use crate :: mem ;
785
+ use crate :: ptr ;
786
786
787
787
pub struct LazyKeyInner < T > {
788
788
inner : UnsafeCell < Option < T > > ,
@@ -811,7 +811,7 @@ mod lazy {
811
811
812
812
// SAFETY:
813
813
//
814
- // note that this can in theory just be `*ptr = Some(value)`, but due to
814
+ // Note that this can in theory just be `*ptr = Some(value)`, but due to
815
815
// the compiler will currently codegen that pattern with something like:
816
816
//
817
817
// ptr::drop_in_place(ptr)
@@ -821,16 +821,20 @@ mod lazy {
821
821
// `ptr` (e.g., if this is being recursively initialized) to re-access
822
822
// TLS, in which case there will be a `&` and `&mut` pointer to the same
823
823
// value (an aliasing violation). To avoid setting the "I'm running a
824
- // destructor" flag we just use `mem::replace` which should sequence the
825
- // operations a little differently and make this safe to call.
824
+ // destructor" flag we just use `ptr::replace` which should sequence the
825
+ // operations a little differently and make this safe to call:
826
+ //
827
+ // let tmp = ptr::read(ptr)
828
+ // ptr::write(ptr, Some(value))
829
+ // drop(tmp)
826
830
//
827
831
// The precondition also ensures that we are the only one accessing
828
832
// `self` at the moment so replacing is fine.
829
833
unsafe {
830
- let _ = mem :: replace ( & mut * ptr, Some ( value) ) ;
834
+ let _ = ptr :: replace ( ptr, Some ( value) ) ;
831
835
}
832
836
833
- // SAFETY: With the call to `mem ::replace` it is guaranteed there is
837
+ // SAFETY: With the call to `ptr ::replace` it is guaranteed there is
834
838
// a `Some` behind `ptr`, not a `None` so `unreachable_unchecked`
835
839
// will never be reached.
836
840
unsafe {
0 commit comments