Struct fuchsia_async::Task
source · pub struct Task<T> { /* private fields */ }
Expand description
A handle to a future that is owned and polled by the executor.
Once a task is created, the executor will poll it until done, even if the task handle itself is not polled.
When a task is dropped its future will no longer be polled by the
executor. See Task::cancel
for cancellation semantics.
Polling (or attempting to extract the value from) a task after the executor is dropped may trigger a panic.
Implementations§
source§impl Task<()>
impl Task<()>
sourcepub fn detach(self)
pub fn detach(self)
Detach this task so that it can run independently in the background.
Note: this is usually not what you want. This API severs the control flow from the
caller, making it impossible to return values (including errors). If your goal is to run
multiple futures concurrently, consider using [TaskGroup
] or other futures combinators
such as:
- [
futures::future::join
] - [
futures::future::select
] - [
futures::select
]
or their error-aware variants
- [
futures::future::try_join
] - [
futures::future::try_select
]
or their stream counterparts
- [
futures::stream::StreamExt::for_each
] - [
futures::stream::StreamExt::for_each_concurrent
] - [
futures::stream::TryStreamExt::try_for_each
] - [
futures::stream::TryStreamExt::try_for_each_concurrent
]
can meet your needs.
source§impl<T: Send> Task<T>
impl<T: Send> Task<T>
sourcepub fn spawn(future: impl Future<Output = T> + Send + 'static) -> Task<T> ⓘ
pub fn spawn(future: impl Future<Output = T> + Send + 'static) -> Task<T> ⓘ
Spawn a new task on the current executor.
The task may be executed on any thread(s) owned by the current executor.
See Task::local
for an equivalent that ensures locality.
The passed future will live until either (a) the future completes,
(b) the returned Task
is dropped while the executor is running, or
(c) the executor is destroyed; whichever comes first.
Panics
spawn
may panic if not called in the context of an executor (e.g.
within a call to run
or run_singlethreaded
).
source§impl<T> Task<T>
impl<T> Task<T>
sourcepub fn local(future: impl Future<Output = T> + 'static) -> Task<T> ⓘ
pub fn local(future: impl Future<Output = T> + 'static) -> Task<T> ⓘ
Spawn a new task on the thread local executor.
The passed future will live until either (a) the future completes,
(b) the returned Task
is dropped while the executor is running, or
(c) the executor is destroyed; whichever comes first.
NOTE: This is not supported with a [SendExecutor
] and will cause a
runtime panic. Use Task::spawn
instead.
Panics
local
may panic if not called in the context of a local executor (e.g.
within a call to run
or run_singlethreaded
).
source§impl<T: 'static> Task<T>
impl<T: 'static> Task<T>
sourcepub async fn cancel(self) -> Option<T>
pub async fn cancel(self) -> Option<T>
Initiate cancellation of this task.
Returns the tasks output if it was available prior to cancelation.
NOTE: If None
is returned, the underlying future may continue executing for a
short period before getting dropped. If so, do not assume any resources held
by the task’s future are released. If Some(..)
is returned, such resources
are guaranteed to be released.
Trait Implementations§
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Task<T>
impl<T> Send for Task<T>where T: Send,
impl<T> Sync for Task<T>where T: Send,
impl<T> Unpin for Task<T>
impl<T> !UnwindSafe for Task<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
§impl<T> FutureExt for Twhere
T: Future + ?Sized,
impl<T> FutureExt for Twhere T: Future + ?Sized,
§fn map<U, F>(self, f: F) -> Map<Self, F>where
F: FnOnce(Self::Output) -> U,
Self: Sized,
fn map<U, F>(self, f: F) -> Map<Self, F>where F: FnOnce(Self::Output) -> U, Self: Sized,
§fn map_into<U>(self) -> MapInto<Self, U>where
Self::Output: Into<U>,
Self: Sized,
fn map_into<U>(self) -> MapInto<Self, U>where Self::Output: Into<U>, Self: Sized,
§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where
F: FnOnce(Self::Output) -> Fut,
Fut: Future,
Self: Sized,
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where F: FnOnce(Self::Output) -> Fut, Fut: Future, Self: Sized,
f
. Read more§fn left_future<B>(self) -> Either<Self, B>where
B: Future<Output = Self::Output>,
Self: Sized,
fn left_future<B>(self) -> Either<Self, B>where B: Future<Output = Self::Output>, Self: Sized,
§fn right_future<A>(self) -> Either<A, Self>where
A: Future<Output = Self::Output>,
Self: Sized,
fn right_future<A>(self) -> Either<A, Self>where A: Future<Output = Self::Output>, Self: Sized,
§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where Self: Sized,
§fn flatten(self) -> Flatten<Self>where
Self::Output: Future,
Self: Sized,
fn flatten(self) -> Flatten<Self>where Self::Output: Future, Self: Sized,
§fn flatten_stream(self) -> FlattenStream<Self>where
Self::Output: Stream,
Self: Sized,
fn flatten_stream(self) -> FlattenStream<Self>where Self::Output: Stream, Self: Sized,
§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where Self: Sized,
poll
will never again be called once it has
completed. This method can be used to turn any Future
into a
FusedFuture
. Read more§fn inspect<F>(self, f: F) -> Inspect<Self, F>where
F: FnOnce(&Self::Output),
Self: Sized,
fn inspect<F>(self, f: F) -> Inspect<Self, F>where F: FnOnce(&Self::Output), Self: Sized,
§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where Self: Sized + UnwindSafe,
§fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where Self: Sized,
()
on completion and sends
its output to another future on a separate task. Read more§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>where
Self: Sized + Send + 'a,
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>where Self: Sized + Send + 'a,
§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where Self: Sized + 'a,
§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = ()
>.§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = Never
>.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere F: Future,
§type IntoFuture = F
type IntoFuture = F
source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<F> TimeoutExt for Fwhere
F: Future,
impl<F> TimeoutExt for Fwhere F: Future,
source§fn on_timeout<WT, OT>(self, time: WT, on_timeout: OT) -> OnTimeout<Self, OT> ⓘwhere
WT: WakeupTime,
OT: FnOnce() -> Self::Output,
fn on_timeout<WT, OT>(self, time: WT, on_timeout: OT) -> OnTimeout<Self, OT> ⓘwhere WT: WakeupTime, OT: FnOnce() -> Self::Output,
on_timeout
to produce a result
when the timeout occurs.source§fn on_stalled<OS>(
self,
timeout: Duration,
on_stalled: OS
) -> OnStalled<Self, OS> ⓘwhere
OS: FnOnce() -> Self::Output,
fn on_stalled<OS>( self, timeout: Duration, on_stalled: OS ) -> OnStalled<Self, OS> ⓘwhere OS: FnOnce() -> Self::Output,
on_stalled
to produce a result
when the future hasn’t been otherwise polled within the timeout
.
This is a heuristic - spurious wakeups will keep the detection from triggering,
and moving all work to external tasks or threads with force the triggering early.§impl<Fut> TryFutureExt for Futwhere
Fut: TryFuture + ?Sized,
impl<Fut> TryFutureExt for Futwhere Fut: TryFuture + ?Sized,
§fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>where
Self::Ok: Sink<Item, Error = Self::Error>,
Self: Sized,
fn flatten_sink<Item>(self) -> FlattenSink<Self, Self::Ok>where Self::Ok: Sink<Item, Error = Self::Error>, Self: Sized,
Sink
]. Read more