Skip to content

Commit a61c8be

Browse files
committed
rename once::ExclusiveState to OnceExclusiveState
It is a bit confusing when reading code that uses this type since it is not immediately obvious that it is specific to `Once`. Signed-off-by: Connor Tsui <[email protected]>
1 parent a41214f commit a61c8be

File tree

5 files changed

+52
-44
lines changed

5 files changed

+52
-44
lines changed

library/std/src/sync/lazy_lock.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::poison::once::ExclusiveState;
1+
use super::poison::once::OnceExclusiveState;
22
use crate::cell::UnsafeCell;
33
use crate::mem::ManuallyDrop;
44
use crate::ops::{Deref, DerefMut};
@@ -140,14 +140,18 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
140140
pub fn into_inner(mut this: Self) -> Result<T, F> {
141141
let state = this.once.state();
142142
match state {
143-
ExclusiveState::Poisoned => panic_poisoned(),
143+
OnceExclusiveState::Poisoned => panic_poisoned(),
144144
state => {
145145
let this = ManuallyDrop::new(this);
146146
let data = unsafe { ptr::read(&this.data) }.into_inner();
147147
match state {
148-
ExclusiveState::Incomplete => Err(ManuallyDrop::into_inner(unsafe { data.f })),
149-
ExclusiveState::Complete => Ok(ManuallyDrop::into_inner(unsafe { data.value })),
150-
ExclusiveState::Poisoned => unreachable!(),
148+
OnceExclusiveState::Incomplete => {
149+
Err(ManuallyDrop::into_inner(unsafe { data.f }))
150+
}
151+
OnceExclusiveState::Complete => {
152+
Ok(ManuallyDrop::into_inner(unsafe { data.value }))
153+
}
154+
OnceExclusiveState::Poisoned => unreachable!(),
151155
}
152156
}
153157
}
@@ -189,7 +193,7 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
189193
impl<T, F> Drop for PoisonOnPanic<'_, T, F> {
190194
#[inline]
191195
fn drop(&mut self) {
192-
self.0.once.set_state(ExclusiveState::Poisoned);
196+
self.0.once.set_state(OnceExclusiveState::Poisoned);
193197
}
194198
}
195199

@@ -200,19 +204,19 @@ impl<T, F: FnOnce() -> T> LazyLock<T, F> {
200204
let guard = PoisonOnPanic(this);
201205
let data = f();
202206
guard.0.data.get_mut().value = ManuallyDrop::new(data);
203-
guard.0.once.set_state(ExclusiveState::Complete);
207+
guard.0.once.set_state(OnceExclusiveState::Complete);
204208
core::mem::forget(guard);
205209
// SAFETY: We put the value there above.
206210
unsafe { &mut this.data.get_mut().value }
207211
}
208212

209213
let state = this.once.state();
210214
match state {
211-
ExclusiveState::Poisoned => panic_poisoned(),
215+
OnceExclusiveState::Poisoned => panic_poisoned(),
212216
// SAFETY: The `Once` states we completed the initialization.
213-
ExclusiveState::Complete => unsafe { &mut this.data.get_mut().value },
217+
OnceExclusiveState::Complete => unsafe { &mut this.data.get_mut().value },
214218
// SAFETY: The state is `Incomplete`.
215-
ExclusiveState::Incomplete => unsafe { really_init_mut(this) },
219+
OnceExclusiveState::Incomplete => unsafe { really_init_mut(this) },
216220
}
217221
}
218222

@@ -293,7 +297,7 @@ impl<T, F> LazyLock<T, F> {
293297
match state {
294298
// SAFETY:
295299
// The closure has been run successfully, so `value` has been initialized.
296-
ExclusiveState::Complete => Some(unsafe { &mut this.data.get_mut().value }),
300+
OnceExclusiveState::Complete => Some(unsafe { &mut this.data.get_mut().value }),
297301
_ => None,
298302
}
299303
}
@@ -332,11 +336,13 @@ impl<T, F> LazyLock<T, F> {
332336
impl<T, F> Drop for LazyLock<T, F> {
333337
fn drop(&mut self) {
334338
match self.once.state() {
335-
ExclusiveState::Incomplete => unsafe { ManuallyDrop::drop(&mut self.data.get_mut().f) },
336-
ExclusiveState::Complete => unsafe {
339+
OnceExclusiveState::Incomplete => unsafe {
340+
ManuallyDrop::drop(&mut self.data.get_mut().f)
341+
},
342+
OnceExclusiveState::Complete => unsafe {
337343
ManuallyDrop::drop(&mut self.data.get_mut().value)
338344
},
339-
ExclusiveState::Poisoned => {}
345+
OnceExclusiveState::Poisoned => {}
340346
}
341347
}
342348
}

library/std/src/sync/poison/once.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ pub struct OnceState {
4949
pub(crate) inner: sys::OnceState,
5050
}
5151

52-
pub(crate) enum ExclusiveState {
52+
/// Used for the internal implementation of `sys::sync::once` on different platforms and the
53+
/// [`LazyLock`](crate::sync::LazyLock) implementation.
54+
pub(crate) enum OnceExclusiveState {
5355
Incomplete,
5456
Poisoned,
5557
Complete,
@@ -310,7 +312,7 @@ impl Once {
310312
/// be running, so the state must be either "incomplete", "poisoned" or
311313
/// "complete".
312314
#[inline]
313-
pub(crate) fn state(&mut self) -> ExclusiveState {
315+
pub(crate) fn state(&mut self) -> OnceExclusiveState {
314316
self.inner.state()
315317
}
316318

@@ -320,7 +322,7 @@ impl Once {
320322
/// be running, so the state must be either "incomplete", "poisoned" or
321323
/// "complete".
322324
#[inline]
323-
pub(crate) fn set_state(&mut self, new_state: ExclusiveState) {
325+
pub(crate) fn set_state(&mut self, new_state: OnceExclusiveState) {
324326
self.inner.set_state(new_state);
325327
}
326328
}

library/std/src/sys/sync/once/futex.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cell::Cell;
22
use crate::sync as public;
33
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
4-
use crate::sync::poison::once::ExclusiveState;
4+
use crate::sync::poison::once::OnceExclusiveState;
55
use crate::sys::futex::{Futex, Primitive, futex_wait, futex_wake_all};
66

77
// On some platforms, the OS is very nice and handles the waiter queue for us.
@@ -83,21 +83,21 @@ impl Once {
8383
}
8484

8585
#[inline]
86-
pub(crate) fn state(&mut self) -> ExclusiveState {
86+
pub(crate) fn state(&mut self) -> OnceExclusiveState {
8787
match *self.state_and_queued.get_mut() {
88-
INCOMPLETE => ExclusiveState::Incomplete,
89-
POISONED => ExclusiveState::Poisoned,
90-
COMPLETE => ExclusiveState::Complete,
88+
INCOMPLETE => OnceExclusiveState::Incomplete,
89+
POISONED => OnceExclusiveState::Poisoned,
90+
COMPLETE => OnceExclusiveState::Complete,
9191
_ => unreachable!("invalid Once state"),
9292
}
9393
}
9494

9595
#[inline]
96-
pub(crate) fn set_state(&mut self, new_state: ExclusiveState) {
96+
pub(crate) fn set_state(&mut self, new_state: OnceExclusiveState) {
9797
*self.state_and_queued.get_mut() = match new_state {
98-
ExclusiveState::Incomplete => INCOMPLETE,
99-
ExclusiveState::Poisoned => POISONED,
100-
ExclusiveState::Complete => COMPLETE,
98+
OnceExclusiveState::Incomplete => INCOMPLETE,
99+
OnceExclusiveState::Poisoned => POISONED,
100+
OnceExclusiveState::Complete => COMPLETE,
101101
};
102102
}
103103

library/std/src/sys/sync/once/no_threads.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cell::Cell;
22
use crate::sync as public;
3-
use crate::sync::poison::once::ExclusiveState;
3+
use crate::sync::poison::once::OnceExclusiveState;
44

55
pub struct Once {
66
state: Cell<State>,
@@ -45,21 +45,21 @@ impl Once {
4545
}
4646

4747
#[inline]
48-
pub(crate) fn state(&mut self) -> ExclusiveState {
48+
pub(crate) fn state(&mut self) -> OnceExclusiveState {
4949
match self.state.get() {
50-
State::Incomplete => ExclusiveState::Incomplete,
51-
State::Poisoned => ExclusiveState::Poisoned,
52-
State::Complete => ExclusiveState::Complete,
50+
State::Incomplete => OnceExclusiveState::Incomplete,
51+
State::Poisoned => OnceExclusiveState::Poisoned,
52+
State::Complete => OnceExclusiveState::Complete,
5353
_ => unreachable!("invalid Once state"),
5454
}
5555
}
5656

5757
#[inline]
58-
pub(crate) fn set_state(&mut self, new_state: ExclusiveState) {
58+
pub(crate) fn set_state(&mut self, new_state: OnceExclusiveState) {
5959
self.state.set(match new_state {
60-
ExclusiveState::Incomplete => State::Incomplete,
61-
ExclusiveState::Poisoned => State::Poisoned,
62-
ExclusiveState::Complete => State::Complete,
60+
OnceExclusiveState::Incomplete => State::Incomplete,
61+
OnceExclusiveState::Poisoned => State::Poisoned,
62+
OnceExclusiveState::Complete => State::Complete,
6363
});
6464
}
6565

library/std/src/sys/sync/once/queue.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
use crate::cell::Cell;
5959
use crate::sync::atomic::Ordering::{AcqRel, Acquire, Release};
6060
use crate::sync::atomic::{Atomic, AtomicBool, AtomicPtr};
61-
use crate::sync::poison::once::ExclusiveState;
61+
use crate::sync::poison::once::OnceExclusiveState;
6262
use crate::thread::{self, Thread};
6363
use crate::{fmt, ptr, sync as public};
6464

@@ -131,21 +131,21 @@ impl Once {
131131
}
132132

133133
#[inline]
134-
pub(crate) fn state(&mut self) -> ExclusiveState {
134+
pub(crate) fn state(&mut self) -> OnceExclusiveState {
135135
match self.state_and_queue.get_mut().addr() {
136-
INCOMPLETE => ExclusiveState::Incomplete,
137-
POISONED => ExclusiveState::Poisoned,
138-
COMPLETE => ExclusiveState::Complete,
136+
INCOMPLETE => OnceExclusiveState::Incomplete,
137+
POISONED => OnceExclusiveState::Poisoned,
138+
COMPLETE => OnceExclusiveState::Complete,
139139
_ => unreachable!("invalid Once state"),
140140
}
141141
}
142142

143143
#[inline]
144-
pub(crate) fn set_state(&mut self, new_state: ExclusiveState) {
144+
pub(crate) fn set_state(&mut self, new_state: OnceExclusiveState) {
145145
*self.state_and_queue.get_mut() = match new_state {
146-
ExclusiveState::Incomplete => ptr::without_provenance_mut(INCOMPLETE),
147-
ExclusiveState::Poisoned => ptr::without_provenance_mut(POISONED),
148-
ExclusiveState::Complete => ptr::without_provenance_mut(COMPLETE),
146+
OnceExclusiveState::Incomplete => ptr::without_provenance_mut(INCOMPLETE),
147+
OnceExclusiveState::Poisoned => ptr::without_provenance_mut(POISONED),
148+
OnceExclusiveState::Complete => ptr::without_provenance_mut(COMPLETE),
149149
};
150150
}
151151

0 commit comments

Comments
 (0)