pub struct Task<T>(/* private fields */);
Expand description
This is the same as a JoinHandle, except that the future will be cancelled when the task is dropped.
Implementations§
Source§impl<T> Task<T>
impl<T> Task<T>
Sourcepub fn detach_on_drop(self) -> JoinHandle<T> ⓘ
pub fn detach_on_drop(self) -> JoinHandle<T> ⓘ
Returns a JoinHandle
which will have detach-on-drop semantics.
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. This can result in flaky tests and makes it impossible to return values (including errors).
If your goal is to run multiple tasks concurrently, use Scope
.
You can also use 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 + 'static> Task<T>
impl<T: Send + 'static> 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 global scope of 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.
Code that uses scopes is encouraged to spawn on a shorter lived scope or
explicitly call Scope::global()
for spawning.
§Panics
May panic if not called in the context of an executor (e.g. within a
call to run
).
Source§impl<T: 'static> Task<T>
impl<T: 'static> 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 global scope of 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.
Code that uses scopes is encouraged to spawn on a shorter lived scope or
explicitly call Scope::global()
for spawning.
§Panics
May panic if not called in the context of an executor (e.g. within a
call to run
).
Trait Implementations§
Source§impl<T> From<JoinHandle<T>> for Task<T>
impl<T> From<JoinHandle<T>> for Task<T>
Source§fn from(value: JoinHandle<T>) -> Self
fn from(value: JoinHandle<T>) -> Self
Auto Trait Implementations§
impl<T> Freeze for Task<T>
impl<T> !RefUnwindSafe for Task<T>
impl<T> Send for Task<T>where
T: Send,
impl<T> Sync for Task<T>where
T: Sync,
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 T
impl<T> FutureExt for T
§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f
. Read more§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
§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>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
§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>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 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<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere
F: Future,
Source§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> ⓘ
fn on_timeout<WT, OT>(self, time: WT, on_timeout: OT) -> OnTimeout<Self, OT> ⓘ
on_timeout
to produce a result
when the timeout occurs.Source§fn on_stalled<OS>(
self,
timeout: Duration,
on_stalled: OS,
) -> OnStalled<Self, OS> ⓘ
fn on_stalled<OS>( self, timeout: Duration, on_stalled: OS, ) -> OnStalled<Self, OS> ⓘ
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<F, T, E> TryFuture for F
impl<F, T, E> TryFuture for F
§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