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