pub struct SetOnce<T> { /* private fields */ }Expand description
A cell that is set once, and that readers can wait on until it is.
It is a OnceCell that nobody initializes asynchronously, paired with
an Event that wait sleeps on: set
publishes the value with one compare-and-swap, and then sets the event,
which wakes every waiter.
use nuclei::sync::SetOnce;
static GREETING: SetOnce<&str> = SetOnce::const_new();
assert!(GREETING.get().is_none());
assert!(GREETING.set("hello").is_ok());
assert_eq!(GREETING.get(), Some(&"hello"));
assert!(GREETING.set("again").is_err());Implementations§
Source§impl<T> SetOnce<T>
impl<T> SetOnce<T>
Sourcepub fn new_with(value: Option<T>) -> Self
pub fn new_with(value: Option<T>) -> Self
A cell that holds value if there is one, and is unset otherwise.
Sourcepub const fn const_new_with(value: T) -> Self
pub const fn const_new_with(value: T) -> Self
A cell that is set to value already, usable in a static.
Sourcepub fn initialized(&self) -> bool
pub fn initialized(&self) -> bool
Whether the cell holds a value.
Sourcepub fn set(&self, value: T) -> Result<(), SetOnceError<T>>
pub fn set(&self, value: T) -> Result<(), SetOnceError<T>>
Sets the value, and wakes every task waiting in
wait. One compare-and-swap publishes; never waits.
§Errors
SetOnceError with the value when the cell is set already, or
another call is setting it at this moment.
Sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Consumes the cell and returns its value, if it has one.
Trait Implementations§
impl<T: Eq> Eq for SetOnce<T>
Auto Trait Implementations§
impl<T> !Freeze for SetOnce<T>
impl<T> !RefUnwindSafe for SetOnce<T>
impl<T> !UnwindSafe for SetOnce<T>
impl<T> Send for SetOnce<T>where
T: Send,
impl<T> Sync for SetOnce<T>
impl<T> Unpin for SetOnce<T>where
T: Unpin,
impl<T> UnsafeUnpin for SetOnce<T>where
T: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more