Struct Signals

Source
pub struct Signals<'a, S> { /* private fields */ }
Expand description

Signal state manager and event listener.

Signals maintains the signal state of an Executor. This consists of the raised signal set and the wakeup signal set. Upon polling the future, the raised signal set is frozen to the then-current value of the event mask, all bits in the wakeup signal set are removed from the event mask (atomically), and the wakeup signal set is cleared. See also Step::poll() for behavior when the wakeup signal set is zero. Current poll functions which are driven by any of the raised signals will be attempted. If a poll function is not attempted or does not yet resolve to an output then its signal mask will be OR-ed into the wakeup signal set. The future won’t be polled again until a raised signal matches the wakeup signal set.

The type parameter S is an EventMask.

§Delayed signals

The executor will examine the event mask after polling the future to check for any immediate updates. Since the raised signal set remains frozen during polling, any signal raised by the future itself through Events won’t become visible until this happens. This also means that external events are not tested for until the future yields. As an optimization for the first case, Signals::raise() updates the raised signal set immediately. Correct programs are not able to observe this behavior.

Implementations§

Source§

impl<'a, S: EventMask> Signals<'a, S>

Source

pub fn bind(&self) -> Executor<'_>

Bind a new executor to this signal source.

The executor will be constructed with a waker that does nothing. It can be replaced by calling Executor::with_waker().

Source

pub fn raise(&self, signals: S)

Raise a signal set without delays.

This will perform the equivalent of [pending().raise(signals)], but it will also update the raised signal set. This prevents the “delayed signals” effect that is shown above. Prefer raise() if another branch of this future (such as in [futures::join!()] might benefit from this, otherwise use pending().raise(). It is impossible to visibly influence a well-behaved poll function by switching between raise() or pending().raise() at any call site; this is a best-effort optimization only.

Source

pub fn pending(&self) -> &Events<S>

Retrieve the underlying event mask.

Source

pub async fn drive<T, E, F>(&self, signals: S, poll: F) -> Result<T, E>
where F: FnMut() -> Result<T, E>,

Asynchronously drive a fallible poll function to completion

The future produced by this method checks on each poll whether any of the signals in signals is present in the raised signal set. If that is so, it invokes the poll function. On first poll, this check is omitted, thus guaranteeing at least one call to the poll function. If the poll function succeeds or fails with nb::Error::Other, the future completes immediately with that value. If the poll function returns nb::Error::WouldBlock, or if none of the signals in signals is present in the raised signal sent, then signals is added to the wakeup signal set and the future pends.

poll() must handle spurious calls gracefully. There is no guarantee that any of the intended effects of any signal in signals has actually taken place. poll() may not block.

Source

pub async fn drive_infallible<T, F>(&self, signals: S, poll: F) -> T
where F: FnMut() -> Result<T, Infallible>,

Asynchronously drive an infallible poll function to completion.

This is a variant of Signals::drive() intended for cases where there is no proper error type. Although drive(sig, poll).await.unwrap() works the same, it often requires explicit type annotations if poll is a closure. This method should be preferred in such cases, such as when poll is just a wrapper around some_option.ok_or(WouldBlock).

Auto Trait Implementations§

§

impl<'a, S> !Freeze for Signals<'a, S>

§

impl<'a, S> !RefUnwindSafe for Signals<'a, S>

§

impl<'a, S> Send for Signals<'a, S>
where S: Sync,

§

impl<'a, S> !Sync for Signals<'a, S>

§

impl<'a, S> Unpin for Signals<'a, S>

§

impl<'a, S> UnwindSafe for Signals<'a, S>
where S: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.