pub struct Context(/* private fields */);
Expand description
Represents an ongoing operation which could be cancelled in the future. Inspired by contexts in the Go programming language.
§Concepts
Contexts have a few important concepts: “cancellation”, “parent” and
“deadline”. A context can be cancelled by calling its Context::cancel()
method; this notifies any tasks which are waiting on its Context::done()
event. It is also cancelled automatically if and when its parent context is
cancelled, and when the last copy of it goes out of scope. A “deadline”
allows a context to be automatically cancelled at a certain timestamp; this
is implemented without creating extra tasks/threads.
§Forking
A context can be “forked”, which creates a new child context. This new context can optionally be created with a deadline.
Implementations§
Source§impl Context
impl Context
Sourcepub fn new_global() -> Self
pub fn new_global() -> Self
Creates a new global context (i.e., one which has no parent or deadline).
Sourcepub fn done(&self) -> impl Selectable + '_
pub fn done(&self) -> impl Selectable + '_
A Selectable
event which occurs when the context is
cancelled. The sleep amount takes the context deadline into
consideration.
Sourcepub fn wrap<'a, T: 'a>(
&'a self,
event: impl Selectable<T> + 'a,
) -> impl Selectable<Option<T>> + 'a
pub fn wrap<'a, T: 'a>( &'a self, event: impl Selectable<T> + 'a, ) -> impl Selectable<Option<T>> + 'a
Creates a Selectable
event which occurs when either the given
event
resolves, or when the context is cancelled, whichever occurs
first.
Trait Implementations§
Source§impl ParentContext for Context
impl ParentContext for Context
Source§fn fork_with_deadline(&self, deadline: Instant) -> Context
fn fork_with_deadline(&self, deadline: Instant) -> Context
Self::fork()
, except that the new
context has a deadline which is the earliest of those in self
and
the one provided.Source§fn fork_with_timeout(&self, timeout: Duration) -> Context
fn fork_with_timeout(&self, timeout: Duration) -> Context
Self::fork_with_deadline()
, except
that the deadline is calculated from the current time and the
provided timeout duration.