From 2e080dcaf9ef13031c9f50c88d40718b7de92df9 Mon Sep 17 00:00:00 2001 From: daxpedda Date: Sun, 8 Oct 2023 21:12:43 +0200 Subject: [PATCH] Add some optimizations --- library/core/src/cell/once.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/library/core/src/cell/once.rs b/library/core/src/cell/once.rs index 98ae0f29c30b..bc1082be0e5a 100644 --- a/library/core/src/cell/once.rs +++ b/library/core/src/cell/once.rs @@ -126,8 +126,7 @@ impl OnceCell { // 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` @@ -211,10 +210,9 @@ impl OnceCell { 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 + // `expect`, 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()) + Ok(self.try_insert(val).expect("reentrant init")) } /// Consumes the cell, returning the wrapped value.