ScopedJoinSet

Struct ScopedJoinSet 

Source
pub struct ScopedJoinSet<'scope, T>
where T: 'scope + Send,
{ /* private fields */ }
Expand description

A scoped wrapper around tokio::task::JoinSet for dynamically spawning futures with a lifetime 'scope.

Unlike normal Tokio tasks requiring 'static futures, this set accepts futures tied to a temporary stack lifetime, as long as the ScopedJoinSet lives long enough.

Each call to spawn places a non-'static future into the runtime via a controlled internal mechanism (FutureHolder + WeakFuture).

§Type Parameters

  • 'scope: The lifetime during which spawned futures must live.
  • T: The output of each spawned future.

Implementations§

Source§

impl<'scope, T> ScopedJoinSet<'scope, T>
where T: 'scope + Send,

Source

pub fn new() -> Self

Creates an empty ScopedJoinSet.

Source

pub fn spawn<F>(&mut self, task: F)
where F: Future<Output = T> + Send + 'scope, T: Send,

Spawns a new scoped future into the set.

This function is analogous to tokio::spawn, but accepts futures with the given scope lifetime.

§Parameters
  • task: The user future to spawn.
§Requirements
  • The returned value T must be Send.
  • The future must be Send and live at least 'scope.
Source

pub fn is_empty(&self) -> bool

Returns whether no tasks remain.

Equivalent to JoinSet::is_empty.

Source

pub fn len(&self) -> usize

Returns the number of tasks currently stored in the set.

Equivalent to JoinSet::len.

Source

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

Waits for the next completed task in the set.

Returns:

  • Some(Ok(T)) on success.
  • Some(Err(JoinError)) on cancellation or panic.
  • None when the set is empty.

This method is cancellation-safe: dropping the future returned by .await does not remove the task from the set.

Source

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

Attempts to join one completed task without awaiting.

This method is non-blocking and returns immediately.

Returns:

  • Some(Ok(T)) if a task has already completed successfully,
  • Some(Err(JoinError)) if a completed task was cancelled or panicked,
  • None if no tasks have completed yet or the set is empty.

This differs from join_next:

  • join_next() awaits the next completed task.
  • try_join_next() returns immediately without awaiting.
§Cancellation Safety

This method is always cancellation-safe, because it does not perform any async operations and does not remove tasks unless they have already finished.

Source

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

Waits for all tasks in the set to complete, consuming the set and returning a list of results.

The returned results are in completion order, not spawn order.

§Cancellation Safety

If this future is dropped, remaining tasks stay inside the set.

Source

pub fn abort_all(&mut self)

Aborts all currently-running tasks.

This does not await task completion; it merely issues abort signals. To also wait for them, call join_remaining or drop the entire set.

After calling abort_all, the set will still contain all tasks until they complete their aborts and are polled again.

Source

pub async fn shutdown(self) -> Vec<Result<T, JoinError>>

Aborts all tasks and then waits for them to finish.

Unlike dropping the set, shutdown consumes it gracefully.

Trait Implementations§

Source§

impl<'scope, T> Default for ScopedJoinSet<'scope, T>
where T: 'scope + Send + Default,

Source§

fn default() -> ScopedJoinSet<'scope, T>

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

impl<'scope, T: Send> Send for ScopedJoinSet<'scope, T>

Auto Trait Implementations§

§

impl<'scope, T> Freeze for ScopedJoinSet<'scope, T>

§

impl<'scope, T> !RefUnwindSafe for ScopedJoinSet<'scope, T>

§

impl<'scope, T> !Sync for ScopedJoinSet<'scope, T>

§

impl<'scope, T> Unpin for ScopedJoinSet<'scope, T>
where T: Unpin,

§

impl<'scope, T> !UnwindSafe for ScopedJoinSet<'scope, T>

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.