Skip to content

Commit dd34d90

Browse files
committed
Add some optimizations
1 parent 6db2587 commit dd34d90

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

library/core/src/cell/once.rs

+3-5
Original file line numberDiff line numberDiff 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.

0 commit comments

Comments
 (0)