Skip to main content

JoinSet

Struct JoinSet 

Source
pub struct JoinSet<T> { /* private fields */ }
Expand description

A set of spawned tasks, joined in the order they finish.

Dropping the set cancels every task still in it, as dropping their Task handles would; detach_all lets them run on.

let mut set = nuclei::task::JoinSet::new();
for i in 0..4u64 {
    set.spawn(async move { i * 10 });
}
let mut sum = 0;
while let Some(out) = set.join_next().await {
    sum += out.unwrap();
}
assert_eq!(sum, 60);

Implementations§

Source§

impl<T> JoinSet<T>

Source

pub fn new() -> Self

An empty set.

Source

pub fn len(&self) -> usize

The tasks in the set, finished and not joined ones included.

Source

pub fn is_empty(&self) -> bool

Whether the set holds no task.

Source

pub fn insert(&mut self, task: Task<T>) -> AbortHandle

Takes task into the set and answers a handle that cancels it.

Source

pub fn spawn<F>(&mut self, future: F) -> AbortHandle
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Spawns future as spawn does, into the set.

Source

pub fn spawn_local<F>(&mut self, future: F) -> AbortHandle
where F: Future<Output = T> + 'static, T: 'static,

Spawns future as spawn_local does, into the set.

Source

pub fn spawn_blocking<F>(&mut self, f: F) -> AbortHandle
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

Runs f on the blocking pool, into the set.

Source

pub fn spawn_on<F>(&mut self, future: F, handle: &RuntimeHandle) -> AbortHandle
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Spawns future on the runtime handle refers to, into the set.

Source

pub async fn join_next(&mut self) -> Option<Result<T, JoinError>>

The output of the next task to finish, None when the set is empty. A task that was cancelled or panicked gives its JoinError.

Source

pub async fn join_next_with_id(&mut self) -> Option<Result<(Id, T), JoinError>>

join_next, with the id of the task.

Source

pub fn try_join_next(&mut self) -> Option<Result<T, JoinError>>

The output of a task that has finished, without waiting: None when none has (the set may still hold tasks).

Source

pub fn poll_join_next( &mut self, cx: &mut Context<'_>, ) -> Poll<Option<Result<(Id, T), JoinError>>>

Polls for the next finished task.

Source

pub async fn join_all(self) -> Vec<T>

Every task’s output, in finishing order.

§Panics

When a task panicked (its panic is resumed) or was cancelled.

Source

pub fn abort_all(&mut self)

Cancels every task in the set; they stay in it, and join_next gives each one’s cancellation (or its output, if it finished first).

Source

pub fn detach_all(&mut self)

Takes every task out of the set and lets each run to its end. The slots go with them, so a detached task’s later wake meets none.

Source

pub async fn shutdown(&mut self)

Cancels every task and waits until each one is out of the set.

Trait Implementations§

Source§

impl<T> Debug for JoinSet<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for JoinSet<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, F> Extend<F> for JoinSet<T>
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Source§

fn extend<I: IntoIterator<Item = F>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, F> FromIterator<F> for JoinSet<T>
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Source§

fn from_iter<I: IntoIterator<Item = F>>(iter: I) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for JoinSet<T>

§

impl<T> !UnwindSafe for JoinSet<T>

§

impl<T> Freeze for JoinSet<T>
where Vec<Slot<T>>: Freeze,

§

impl<T> Send for JoinSet<T>
where Vec<Slot<T>>: Send,

§

impl<T> Sync for JoinSet<T>
where Vec<Slot<T>>: Sync,

§

impl<T> Unpin for JoinSet<T>
where Vec<Slot<T>>: Unpin,

§

impl<T> UnsafeUnpin for JoinSet<T>
where Vec<Slot<T>>: UnsafeUnpin,

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> MaybeSend for T
where T: Send + ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.