@@ -77,23 +77,19 @@ where
77
77
78
78
let v = i. and_then ( Option :: take) . unwrap_or_else ( f) ;
79
79
80
+ // SAFETY: we cannot be inside a `LocalKey::with` scope, as the initializer
81
+ // has already returned and the next scope only starts after we return
82
+ // the pointer. Therefore, there can be no references to the old value,
83
+ // even if it was initialized. Thus because we are !Sync we have exclusive
84
+ // access to self.value and may replace it.
85
+ let mut old_value = unsafe { self . value . get ( ) . replace ( MaybeUninit :: new ( v) ) } ;
80
86
match self . state . replace ( State :: Alive ) {
81
87
State :: Uninitialized => D :: register_dtor ( self ) ,
82
-
83
- State :: Alive => {
84
- // An init occurred during a recursive call, this could be a panic in the future.
85
-
86
- // SAFETY: we cannot be inside a `LocalKey::with` scope, as the initializer
87
- // has already returned and the next scope only starts after we return
88
- // the pointer. Therefore, there can be no references to the old value.
89
- unsafe { ( * self . value . get ( ) ) . assume_init_drop ( ) }
90
- }
91
-
88
+ State :: Alive => unsafe { old_value. assume_init_drop ( ) } ,
92
89
State :: Destroyed ( _) => unreachable ! ( ) ,
93
90
}
94
91
95
- // SAFETY: we are !Sync so we have exclusive access to self.value.
96
- unsafe { ( * self . value . get ( ) ) . write ( v) }
92
+ self . value . get ( ) . cast ( )
97
93
}
98
94
}
99
95
0 commit comments