pub struct WakeFlag { /* private fields */ }Expand description
A Listener destination which can wake an async task.
This is similar to an async MPSC channel except that it carries no data, only wakeups.
Like a channel, it has a sending side WakeFlagListener, a receiving side WakeFlag,
and either side can notice when the other is closed (dropped).
Its intended use is to allow a looping task to sleep until it needs to take an action.
WakeFlag uses only atomic operations and no locks, and therefore may be used on no_std
platforms.
It is Send and Sync regardless of whether the "sync" crate feature is enabled.
§Example
In this async code sample, we wake a task which updates output_cell based on the contents of
input_cell:
use futures::join;
use nosy::{Source as _, unsync::Cell, future::WakeFlag};
let input_cell: Cell<i32> = Cell::new(0);
let input_source = input_cell.as_source();
let output_cell: Cell<i32> = Cell::new(0);
let output_source = output_cell.as_source();
let mut flag = WakeFlag::listening(true, &input_source);
join!(
async move {
// Woken task.
// This async block owns `flag`, `input_source`, and `output_cell`.
// Its loop will exit when it is impossible to have any more work to do,
// that is, when `input_cell` is dropped.
while flag.wait().await {
// In a real application this might be some substantial computation.
output_cell.set_if_unequal(input_source.get() * 10);
}
},
async move {
// Writing task.
// This async block owns `input_cell`, and drops it when done making
// changes.
//
// (Note: This “yield, then expect the result to have been computed
// by now” strategy is not reliable, and therefore not appropriate,
// in more complex async programs. It is used here only to prove
// that the computation is actually being done in a reasonably short
// example.)
input_cell.set(1);
yield_now().await;
assert_eq!(output_source.get(), 10);
input_cell.set(2);
yield_now().await;
assert_eq!(output_source.get(), 20);
},
);Implementations§
Source§impl WakeFlag
impl WakeFlag
Sourcepub fn new(wake_immediately: bool) -> (Self, WakeFlagListener)
pub fn new(wake_immediately: bool) -> (Self, WakeFlagListener)
Constructs a WakeFlag and paired WakeFlagListener.
If wake_immediately is true, then the waiting task will be woken on the first call
to wait(), even if no message has been received.
Sourcepub fn listening<L>(wake_immediately: bool, source: L) -> Self
pub fn listening<L>(wake_immediately: bool, source: L) -> Self
Constructs a WakeFlag with the given initial state and call
Listen::listen() with its listener.
This is a convenience for calling WakeFlag::new() followed by
source.listen(listener).
Sourcepub async fn wait(&mut self) -> bool
pub async fn wait(&mut self) -> bool
Suspend the current async task until at least one message is received,
messages have already been received since the last call to wait(),
or no more messages will arrive.
When a message is received, returns true.
When no more messages will be received because all listeners have been dropped,
returns false;
afterward, calls to wait() will always immediately return false.
This function is “cancellation safe”: if the future is dropped before it completes,
there is no effect on the state of the WakeFlag, as if wait() had never been
called at all.
Sourcepub fn notify(&self)
pub fn notify(&self)
Set the flag, causing the next call to wait() to return immediately.
This is equivalent to calling .receive(&[()]) on the listener, but can be done
without access to the listener.
It may be useful in situations where the task wishes to immediately wake again
for some reason, without having separate logic for that.
Trait Implementations§
Source§impl Stream for WakeFlag
As a Stream, WakeFlag will produce () once for each time
WakeFlag::wait() would produce true.
impl Stream for WakeFlag
As a Stream, WakeFlag will produce () once for each time
WakeFlag::wait() would produce true.
Auto Trait Implementations§
impl Freeze for WakeFlag
impl RefUnwindSafe for WakeFlag
impl Send for WakeFlag
impl Sync for WakeFlag
impl Unpin for WakeFlag
impl UnwindSafe for WakeFlag
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
Source§impl<T> StreamExt for T
impl<T> StreamExt for T
Source§fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
Source§fn into_future(self) -> StreamFuture<Self>
fn into_future(self) -> StreamFuture<Self>
Source§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
Source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
Source§fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
Source§fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
Source§fn collect<C>(self) -> Collect<Self, C>
fn collect<C>(self) -> Collect<Self, C>
Source§fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB>
fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB>
Source§fn concat(self) -> Concat<Self>
fn concat(self) -> Concat<Self>
Source§fn count(self) -> Count<Self>where
Self: Sized,
fn count(self) -> Count<Self>where
Self: Sized,
Source§fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F>
fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F>
Source§fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F>
fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F>
true if any element in stream satisfied a predicate. Read moreSource§fn all<Fut, F>(self, f: F) -> All<Self, Fut, F>
fn all<Fut, F>(self, f: F) -> All<Self, Fut, F>
true if all element in stream satisfied a predicate. Read moreSource§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
StreamExt::fold that holds internal state
and produces a new stream. Read moreSource§fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
true. Read moreSource§fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
true. Read moreSource§fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
Source§fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F>
fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F>
Source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n items of the underlying stream. Read moreSource§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n items of the underlying stream. Read moreSource§fn zip<St>(self, other: St) -> Zip<Self, St>
fn zip<St>(self, other: St) -> Zip<Self, St>
Source§fn peekable(self) -> Peekable<Self>where
Self: Sized,
fn peekable(self) -> Peekable<Self>where
Self: Sized,
peek method. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn left_stream<B>(self) -> Either<Self, B>
fn left_stream<B>(self) -> Either<Self, B>
Source§fn right_stream<B>(self) -> Either<B, Self>
fn right_stream<B>(self) -> Either<B, Self>
Source§fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where
Self: Unpin,
fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where
Self: Unpin,
Stream::poll_next on Unpin
stream types.