about summary refs log tree commit diff
diff options
context:
space:
mode:
authordaxpedda <daxpedda@gmail.com>2023-10-08 21:12:43 +0200
committerdaxpedda <daxpedda@gmail.com>2023-10-13 14:54:33 +0200
commitdd34d9027af3c0286aa038a365ff14387a6bf80c (patch)
treedb52f55abce70ad18088a033a833437ed851be52
parent6db2587999872358e565e403504bc5ebfbfec7a5 (diff)
downloadrust-dd34d9027af3c0286aa038a365ff14387a6bf80c.tar.gz
rust-dd34d9027af3c0286aa038a365ff14387a6bf80c.zip
Add some optimizations
-rw-r--r--library/core/src/cell/once.rs8
1 files changed, 3 insertions, 5 deletions
diff --git a/library/core/src/cell/once.rs b/library/core/src/cell/once.rs
index f85df8a3895..3877a0c48cb 100644
--- a/library/core/src/cell/once.rs
+++ b/library/core/src/cell/once.rs
@@ -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`
@@ -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.