Skip to content

Commit

Permalink
Add some optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Oct 13, 2023
1 parent 6db2587 commit dd34d90
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions library/core/src/cell/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ impl<T> OnceCell<T> {
// checked that slot is currently `None`, so this write
// maintains the `inner`'s invariant.
let slot = unsafe { &mut *self.inner.get() };
*slot = Some(value);
Ok(self.get().unwrap())
Ok(slot.insert(value))
}

/// Gets the contents of the cell, initializing it with `f`
Expand Down Expand Up @@ -213,10 +212,9 @@ impl<T> OnceCell<T> {
let val = outlined_call(f)?;
// Note that *some* forms of reentrant initialization might lead to
// UB (see `reentrant_init` test). I believe that just removing this
// `assert`, while keeping `set/get` would be sound, but it seems
// `panic`, while keeping `try_insert` would be sound, but it seems
// better to panic, rather than to silently use an old value.
assert!(self.set(val).is_ok(), "reentrant init");
Ok(self.get().unwrap())
if let Ok(val) = self.try_insert(val) { Ok(val) } else { panic!("reentrant init") }
}

/// Consumes the cell, returning the wrapped value.
Expand Down

0 comments on commit dd34d90

Please sign in to comment.