pub struct WaitableCell<T> { /* private fields */ }Expand description
A cell where we can wait (with timeout) for a value to be set
Implementations§
Source§impl<T> WaitableCell<T>
impl<T> WaitableCell<T>
Sourcepub fn set(&self, val: impl Into<T>) -> Result<(), T>
pub fn set(&self, val: impl Into<T>) -> Result<(), T>
Sets a value to the WaitableCell. This method has no effect if the WaitableCell already has a value.
Sourcepub fn set_guard_with<R: Into<T>, F: FnOnce() -> R>(
&self,
f: F,
) -> impl Drop + use<F, T, R>
pub fn set_guard_with<R: Into<T>, F: FnOnce() -> R>( &self, f: F, ) -> impl Drop + use<F, T, R>
If the WaitableCell is empty when this guard is dropped, the cell will be set to result of f.
let cell = WaitableCell::<i32>::new();
{
let _guard = cell.set_guard_with(|| 42);
}
assert_eq!(&42, cell.wait().await);The operation is a no-op if the cell conbtains a value before the guard is dropped.
let cell = WaitableCell::<i32>::new();
{
let _guard = cell.set_guard_with(|| 42);
let _ = cell.set(24);
}
assert_eq!(&24, cell.wait().await);The function f will always be called, regardless of whether the WaitableCell has a value or not.
The WaitableCell is going to be set even in the case of an unwind. In this case, ff the function f
panics it will cause an abort, so it’s recommended to avoid any panics in f.
Trait Implementations§
Source§impl<T> Clone for WaitableCell<T>
impl<T> Clone for WaitableCell<T>
Source§impl<T> Default for WaitableCell<T>
impl<T> Default for WaitableCell<T>
impl<T> Send for WaitableCell<T>
impl<T> Sync for WaitableCell<T>
Auto Trait Implementations§
impl<T> Freeze for WaitableCell<T>
impl<T> !RefUnwindSafe for WaitableCell<T>
impl<T> Unpin for WaitableCell<T>
impl<T> !UnwindSafe for WaitableCell<T>
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