File tree Expand file tree Collapse file tree 1 file changed +7
-5
lines changed Expand file tree Collapse file tree 1 file changed +7
-5
lines changed Original file line number Diff line number Diff line change @@ -126,8 +126,7 @@ impl<T> OnceCell<T> {
126126 // checked that slot is currently `None`, so this write
127127 // maintains the `inner`'s invariant.
128128 let slot = unsafe { & mut * self . inner . get ( ) } ;
129- * slot = Some ( value) ;
130- Ok ( self . get ( ) . unwrap ( ) )
129+ Ok ( slot. insert ( value) )
131130 }
132131
133132 /// Gets the contents of the cell, initializing it with `f`
@@ -211,10 +210,13 @@ impl<T> OnceCell<T> {
211210 let val = outlined_call ( f) ?;
212211 // Note that *some* forms of reentrant initialization might lead to
213212 // 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
215214 // 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) {
216+ Ok ( val)
217+ } else {
218+ panic ! ( "reentrant init" )
219+ }
218220 }
219221
220222 /// Consumes the cell, returning the wrapped value.
You can’t perform that action at this time.
0 commit comments