about summary refs log tree commit diff
path: root/library/std/src/sys
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-02-18 09:29:21 +0000
committerbors <bors@rust-lang.org>2023-02-18 09:29:21 +0000
commit3701bdc6333145410f009c83bd03f424eca05009 (patch)
treecabec78c289224033894c07fa509d6d5a99c9617 /library/std/src/sys
parent6d819a4b8f45b170e7c2c415df20cfa2e0cbbf7f (diff)
parent6520488e37f39a11affd776ab1283a0a3fe8087e (diff)
downloadrust-3701bdc6333145410f009c83bd03f424eca05009.tar.gz
rust-3701bdc6333145410f009c83bd03f424eca05009.zip
Auto merge of #107329 - joboet:optimize_lazylock, r=m-ou-se
Optimize `LazyLock` size

The initialization function was unnecessarily stored separately from the data to be initialized. Since both cannot exist at the same time, a `union` can be used, with the `Once` acting as discriminant. This unfortunately requires some extra methods on `Once` so that `Drop` can be implemented correctly and efficiently.

`@rustbot` label +T-libs +A-atomic
Diffstat (limited to 'library/std/src/sys')
-rw-r--r--library/std/src/sys/unsupported/once.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/library/std/src/sys/unsupported/once.rs b/library/std/src/sys/unsupported/once.rs
index b4bb4975f41..11fde1888ba 100644
--- a/library/std/src/sys/unsupported/once.rs
+++ b/library/std/src/sys/unsupported/once.rs
@@ -1,5 +1,6 @@
 use crate::cell::Cell;
 use crate::sync as public;
+use crate::sync::once::ExclusiveState;
 
 pub struct Once {
     state: Cell<State>,
@@ -44,6 +45,16 @@ impl Once {
         self.state.get() == State::Complete
     }
 
+    #[inline]
+    pub(crate) fn state(&mut self) -> ExclusiveState {
+        match self.state.get() {
+            State::Incomplete => ExclusiveState::Incomplete,
+            State::Poisoned => ExclusiveState::Poisoned,
+            State::Complete => ExclusiveState::Complete,
+            _ => unreachable!("invalid Once state"),
+        }
+    }
+
     #[cold]
     #[track_caller]
     pub fn call(&self, ignore_poisoning: bool, f: &mut impl FnMut(&public::OnceState)) {