Skip to content

Commit 274548d

Browse files
committed
Add some optimizations
1 parent e1d6a1a commit 274548d

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
@@ -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,9 @@ 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) { Ok(val) } else { panic!("reentrant init") }
218216
}
219217

220218
/// Consumes the cell, returning the wrapped value.

0 commit comments

Comments
 (0)