diff options
| author | joboet <jonasboettiger@icloud.com> | 2024-03-12 14:55:06 +0100 |
|---|---|---|
| committer | joboet <jonasboettiger@icloud.com> | 2024-03-12 15:41:06 +0100 |
| commit | 22a5267c83a3e17f2b763279eb24bb632c45dc6b (patch) | |
| tree | 81eca4925b77ec92b4f2fd66962af948fe59d094 /library/std/src/sys/pal/unsupported | |
| parent | 3b85d2c7fc6d1698e68b94f7bc1a5c9633f2554d (diff) | |
| download | rust-22a5267c83a3e17f2b763279eb24bb632c45dc6b.tar.gz rust-22a5267c83a3e17f2b763279eb24bb632c45dc6b.zip | |
std: move `Once` implementations to `sys`
Diffstat (limited to 'library/std/src/sys/pal/unsupported')
| -rw-r--r-- | library/std/src/sys/pal/unsupported/mod.rs | 1 | ||||
| -rw-r--r-- | library/std/src/sys/pal/unsupported/once.rs | 100 |
2 files changed, 0 insertions, 101 deletions
diff --git a/library/std/src/sys/pal/unsupported/mod.rs b/library/std/src/sys/pal/unsupported/mod.rs index 9ce275ee72d..be344fb7cae 100644 --- a/library/std/src/sys/pal/unsupported/mod.rs +++ b/library/std/src/sys/pal/unsupported/mod.rs @@ -6,7 +6,6 @@ pub mod env; pub mod fs; pub mod io; pub mod net; -pub mod once; pub mod os; pub mod pipe; pub mod process; diff --git a/library/std/src/sys/pal/unsupported/once.rs b/library/std/src/sys/pal/unsupported/once.rs deleted file mode 100644 index 11fde1888ba..00000000000 --- a/library/std/src/sys/pal/unsupported/once.rs +++ /dev/null @@ -1,100 +0,0 @@ -use crate::cell::Cell; -use crate::sync as public; -use crate::sync::once::ExclusiveState; - -pub struct Once { - state: Cell<State>, -} - -pub struct OnceState { - poisoned: bool, - set_state_to: Cell<State>, -} - -#[derive(Clone, Copy, PartialEq, Eq)] -enum State { - Incomplete, - Poisoned, - Running, - Complete, -} - -struct CompletionGuard<'a> { - state: &'a Cell<State>, - set_state_on_drop_to: State, -} - -impl<'a> Drop for CompletionGuard<'a> { - fn drop(&mut self) { - self.state.set(self.set_state_on_drop_to); - } -} - -// Safety: threads are not supported on this platform. -unsafe impl Sync for Once {} - -impl Once { - #[inline] - #[rustc_const_stable(feature = "const_once_new", since = "1.32.0")] - pub const fn new() -> Once { - Once { state: Cell::new(State::Incomplete) } - } - - #[inline] - pub fn is_completed(&self) -> bool { - 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)) { - let state = self.state.get(); - match state { - State::Poisoned if !ignore_poisoning => { - // Panic to propagate the poison. - panic!("Once instance has previously been poisoned"); - } - State::Incomplete | State::Poisoned => { - self.state.set(State::Running); - // `guard` will set the new state on drop. - let mut guard = - CompletionGuard { state: &self.state, set_state_on_drop_to: State::Poisoned }; - // Run the function, letting it know if we're poisoned or not. - let f_state = public::OnceState { - inner: OnceState { - poisoned: state == State::Poisoned, - set_state_to: Cell::new(State::Complete), - }, - }; - f(&f_state); - guard.set_state_on_drop_to = f_state.inner.set_state_to.get(); - } - State::Running => { - panic!("one-time initialization may not be performed recursively"); - } - State::Complete => {} - } - } -} - -impl OnceState { - #[inline] - pub fn is_poisoned(&self) -> bool { - self.poisoned - } - - #[inline] - pub fn poison(&self) { - self.set_state_to.set(State::Poisoned) - } -} |
