Struct JoinSet
struct JoinSet<T> { ... }
A collection of tasks spawned on a Tokio runtime.
A JoinSet can be used to await the completion of some or all of the tasks
in the set. The set is not ordered, and the tasks will be returned in the
order they complete.
All of the tasks must have the same return type T.
When the JoinSet is dropped, all tasks in the JoinSet are immediately aborted.
Examples
Spawn multiple tasks and wait for them.
use JoinSet;
async
Implementations
impl<T> JoinSet<T>
fn new() -> SelfCreate a new
JoinSet.fn len(self: &Self) -> usizeReturns the number of tasks currently in the
JoinSet.fn is_empty(self: &Self) -> boolReturns whether the
JoinSetis empty.
impl<T: 'static> JoinSet<T>
fn spawn<F>(self: &mut Self, task: F) -> AbortHandle where F: Future<Output = T> + Send + 'static, T: SendSpawn the provided task on the
JoinSet, returning anAbortHandlethat can be used to remotely cancel the task.The provided future will start running in the background immediately when this method is called, even if you don't await anything on this
JoinSet.Panics
This method panics if called outside of a Tokio runtime.
fn spawn_on<F>(self: &mut Self, task: F, handle: &Handle) -> AbortHandle where F: Future<Output = T> + Send + 'static, T: SendSpawn the provided task on the provided runtime and store it in this
JoinSetreturning anAbortHandlethat can be used to remotely cancel the task.The provided future will start running in the background immediately when this method is called, even if you don't await anything on this
JoinSet.fn spawn_local<F>(self: &mut Self, task: F) -> AbortHandle where F: Future<Output = T> + 'staticSpawn the provided task on the current
LocalSetand store it in thisJoinSet, returning anAbortHandlethat can be used to remotely cancel the task.The provided future will start running in the background immediately when this method is called, even if you don't await anything on this
JoinSet.Panics
This method panics if it is called outside of a
LocalSet.fn spawn_local_on<F>(self: &mut Self, task: F, local_set: &LocalSet) -> AbortHandle where F: Future<Output = T> + 'staticSpawn the provided task on the provided
LocalSetand store it in thisJoinSet, returning anAbortHandlethat can be used to remotely cancel the task.Unlike the
spawn_localmethod, this method may be used to spawn local tasks on aLocalSetthat is not currently running. The provided future will start running whenever theLocalSetis next started.fn spawn_blocking<F>(self: &mut Self, f: F) -> AbortHandle where F: FnOnce() -> T + Send + 'static, T: SendSpawn the blocking code on the blocking threadpool and store it in this
JoinSet, returning anAbortHandlethat can be used to remotely cancel the task.Examples
Spawn multiple blocking tasks and wait for them.
use JoinSet; asyncPanics
This method panics if called outside of a Tokio runtime.
fn spawn_blocking_on<F>(self: &mut Self, f: F, handle: &Handle) -> AbortHandle where F: FnOnce() -> T + Send + 'static, T: SendSpawn the blocking code on the blocking threadpool of the provided runtime and store it in this
JoinSet, returning anAbortHandlethat can be used to remotely cancel the task.async fn join_next(self: &mut Self) -> Option<Result<T, JoinError>>Waits until one of the tasks in the set completes and returns its output.
Returns
Noneif the set is empty.Cancel Safety
This method is cancel safe. If
join_nextis used as the event in atokio::select!statement and some other branch completes first, it is guaranteed that no tasks were removed from thisJoinSet.async fn join_next_with_id(self: &mut Self) -> Option<Result<(Id, T), JoinError>>Waits until one of the tasks in the set completes and returns its output, along with the task ID of the completed task.
Returns
Noneif the set is empty.When this method returns an error, then the id of the task that failed can be accessed using the
JoinError::idmethod.Cancel Safety
This method is cancel safe. If
join_next_with_idis used as the event in atokio::select!statement and some other branch completes first, it is guaranteed that no tasks were removed from thisJoinSet.fn try_join_next(self: &mut Self) -> Option<Result<T, JoinError>>Tries to join one of the tasks in the set that has completed and return its output.
Returns
Noneif there are no completed tasks, or if the set is empty.fn try_join_next_with_id(self: &mut Self) -> Option<Result<(Id, T), JoinError>>Tries to join one of the tasks in the set that has completed and return its output, along with the task ID of the completed task.
Returns
Noneif there are no completed tasks, or if the set is empty.When this method returns an error, then the id of the task that failed can be accessed using the
JoinError::idmethod.async fn shutdown(self: &mut Self)Aborts all tasks and waits for them to finish shutting down.
Calling this method is equivalent to calling
abort_alland then callingjoin_nextin a loop until it returnsNone.This method ignores any panics in the tasks shutting down. When this call returns, the
JoinSetwill be empty.async fn join_all(self: Self) -> Vec<T>Awaits the completion of all tasks in this
JoinSet, returning a vector of their results.The results will be stored in the order they completed not the order they were spawned. This is a convenience method that is equivalent to calling
join_nextin a loop. If any tasks on theJoinSetfail with anJoinError, then this call tojoin_allwill panic and all remaining tasks on theJoinSetare cancelled. To handle errors in any other way, manually calljoin_nextin a loop.Examples
Spawn multiple tasks and
join_allthem.use JoinSet; use Duration; asyncEquivalent implementation of
join_all, usingjoin_nextand loop.use JoinSet; use panic; asyncfn abort_all(self: &mut Self)Aborts all tasks on this
JoinSet.This does not remove the tasks from the
JoinSet. To wait for the tasks to complete cancellation, you should calljoin_nextin a loop until theJoinSetis empty.fn detach_all(self: &mut Self)Removes all tasks from this
JoinSetwithout aborting them.The tasks removed by this call will continue to run in the background even if the
JoinSetis dropped.fn poll_join_next(self: &mut Self, cx: &mut Context<'_>) -> Poll<Option<Result<T, JoinError>>>Polls for one of the tasks in the set to complete.
If this returns
Poll::Ready(Some(_)), then the task that completed is removed from the set.When the method returns
Poll::Pending, theWakerin the providedContextis scheduled to receive a wakeup when a task in theJoinSetcompletes. Note that on multiple calls topoll_join_next, only theWakerfrom theContextpassed to the most recent call is scheduled to receive a wakeup.Returns
This function returns:
Poll::Pendingif theJoinSetis not empty but there is no task whose output is available right now.Poll::Ready(Some(Ok(value)))if one of the tasks in thisJoinSethas completed. Thevalueis the return value of one of the tasks that completed.Poll::Ready(Some(Err(err)))if one of the tasks in thisJoinSethas panicked or been aborted. Theerris theJoinErrorfrom the panicked/aborted task.Poll::Ready(None)if theJoinSetis empty.
Note that this method may return
Poll::Pendingeven if one of the tasks has completed. This can happen if the coop budget is reached.fn poll_join_next_with_id(self: &mut Self, cx: &mut Context<'_>) -> Poll<Option<Result<(Id, T), JoinError>>>Polls for one of the tasks in the set to complete.
If this returns
Poll::Ready(Some(_)), then the task that completed is removed from the set.When the method returns
Poll::Pending, theWakerin the providedContextis scheduled to receive a wakeup when a task in theJoinSetcompletes. Note that on multiple calls topoll_join_next, only theWakerfrom theContextpassed to the most recent call is scheduled to receive a wakeup.Returns
This function returns:
Poll::Pendingif theJoinSetis not empty but there is no task whose output is available right now.Poll::Ready(Some(Ok((id, value))))if one of the tasks in thisJoinSethas completed. Thevalueis the return value of one of the tasks that completed, andidis the task ID of that task.Poll::Ready(Some(Err(err)))if one of the tasks in thisJoinSethas panicked or been aborted. Theerris theJoinErrorfrom the panicked/aborted task.Poll::Ready(None)if theJoinSetis empty.
Note that this method may return
Poll::Pendingeven if one of the tasks has completed. This can happen if the coop budget is reached.
impl<T> Any for JoinSet<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for JoinSet<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for JoinSet<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Debug for JoinSet<T>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<T> Default for JoinSet<T>
fn default() -> Self
impl<T> Drop for JoinSet<T>
fn drop(self: &mut Self)
impl<T> Freeze for JoinSet<T>
impl<T> From for JoinSet<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for JoinSet<T>
impl<T> Send for JoinSet<T>
impl<T> Sync for JoinSet<T>
impl<T> Unpin for JoinSet<T>
impl<T> UnsafeUnpin for JoinSet<T>
impl<T> UnwindSafe for JoinSet<T>
impl<T, F> FromIterator for JoinSet<T>
fn from_iter<I: IntoIterator<Item = F>>(iter: I) -> Self
impl<T, U> Into for JoinSet<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for JoinSet<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for JoinSet<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>