File tree Expand file tree Collapse file tree 1 file changed +3
-5
lines changed Expand file tree Collapse file tree 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> {
128128 // checked that slot is currently `None`, so this write
129129 // maintains the `inner`'s invariant.
130130 let slot = unsafe { & mut * self . inner . get ( ) } ;
131- * slot = Some ( value) ;
132- Ok ( self . get ( ) . unwrap ( ) )
131+ Ok ( slot. insert ( value) )
133132 }
134133
135134 /// Gets the contents of the cell, initializing it with `f`
@@ -213,10 +212,9 @@ impl<T> OnceCell<T> {
213212 let val = outlined_call ( f) ?;
214213 // Note that *some* forms of reentrant initialization might lead to
215214 // 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
217216 // 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" ) }
220218 }
221219
222220 /// Consumes the cell, returning the wrapped value.
You can’t perform that action at this time.
0 commit comments