File tree 1 file changed +3
-5
lines changed
1 file changed +3
-5
lines changed Original file line number Diff line number Diff line change @@ -126,8 +126,7 @@ impl<T> OnceCell<T> {
126
126
// checked that slot is currently `None`, so this write
127
127
// maintains the `inner`'s invariant.
128
128
let slot = unsafe { & mut * self . inner . get ( ) } ;
129
- * slot = Some ( value) ;
130
- Ok ( self . get ( ) . unwrap ( ) )
129
+ Ok ( slot. insert ( value) )
131
130
}
132
131
133
132
/// Gets the contents of the cell, initializing it with `f`
@@ -211,10 +210,9 @@ impl<T> OnceCell<T> {
211
210
let val = outlined_call ( f) ?;
212
211
// Note that *some* forms of reentrant initialization might lead to
213
212
// UB (see `reentrant_init` test). I believe that just removing this
214
- // `assert `, while keeping `set/get ` would be sound, but it seems
213
+ // `panic `, while keeping `try_insert ` would be sound, but it seems
215
214
// better to panic, rather than to silently use an old value.
216
- assert ! ( self . set( val) . is_ok( ) , "reentrant init" ) ;
217
- Ok ( self . get ( ) . unwrap ( ) )
215
+ if let Ok ( val) = self . try_insert ( val) { Ok ( val) } else { panic ! ( "reentrant init" ) }
218
216
}
219
217
220
218
/// Consumes the cell, returning the wrapped value.
You can’t perform that action at this time.
0 commit comments