Struct ScopedJoinSet

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

A scoped variant of tokio::task::JoinSet that ensures all futures outlive 'scope, while allowing them to be spawned and awaited dynamically.

This structure tracks all spawned futures internally with a holder, allowing weak references to be used for polling in a way that is memory-safe and supports scoped lifetimes.

§Safety

There is only one FutureHolder and one WeakFuture at a time per task. If the WeakFuture successfully upgrades, the future is guaranteed to still be valid because FutureHolder holds the strong reference until it is dropped (after task join).

Implementations§

Source§

impl<'scope, T> ScopedJoinSet<'scope, T>
where T: 'static,

Source

pub fn new() -> Self

Creates a new, empty ScopedJoinSet.

Source

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

Spawns a new task on the join set.

The future must be 'scope-bound and Send. Internally, the future is wrapped in a FutureHolder and a weak reference is passed to the join set for execution.

§Panics

This method panics if called outside of a Tokio runtime.

Source

pub fn is_empty(&self) -> bool

Returns true if there are no remaining tasks in the join set.

Source

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

Waits for the next task to complete.

Returns:

  • Some(Ok(T)) if a task completed successfully.
  • Some(Err(JoinError)) if the task was cancelled or panicked.
  • None if there are no more tasks in the set.
§Cancel Safety

This method is cancellation safe — if the join_next().await is itself cancelled (e.g., due to timeout or a select! branch winning), no state is lost. The underlying task remains in the join set and will be yielded again on the next call to join_next().

Internally, the JoinSet’s join_next_with_id() ensures that the task is not removed from the set unless it has completed and its result has been received.

The associated future holder is only dropped and removed from internal tracking once the task finishes and is returned from join_next().

Trait Implementations§

Source§

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

Source§

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

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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.