ScopedJoinSet

Struct ScopedJoinSet 

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

A scoped task set analogous to tokio::task::JoinSet but accepting non-'static futures.

ScopedJoinSet allows spawning futures that borrow data with lifetime 'scope, while still being executed by Tokio, which normally requires 'static futures.

Each task’s result is written into a dedicated heap allocation and later recovered by join_next(). No maps, arenas, or auxiliary structures are used—the completed task returns a raw pointer identifying its output slot.

Results are yielded in completion order. non-'static futures.

§Mandatory explicit shutdown

A ScopedJoinSet must be terminated using either ScopedJoinSet::shutdown() or ScopedJoinSet::join_all().

If dropped without having called one of these methods, the destructor prints an error message and aborts the process to prevent tasks from escaping the scope.

This rule ensures that tasks which borrow stack data never outlive that data.

Implementations§

Source§

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

Source

pub fn new() -> Self

Create an empty ScopedJoinSet.

Source

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

Spawns a non-'static future into the set.

The future’s output is written into a heap-allocated slot owned by the set. The future itself is wrapped and safely promoted to 'static because ScopedJoinSet guarantees that its allocation outlives the task.

Source

pub fn is_empty(&self) -> bool

Returns true if no tasks remain.

Source

pub fn len(&self) -> usize

Returns the number of active tasks.

Source

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

Waits for the next task to complete and returns its result.

On success, returns the output value produced by the task.
On failure, returns a JoinError.

The associated output allocation is reclaimed automatically.

Source

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

Non-async version of [join_next].

Returns immediately if no task has finished polling.

Source

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

Waits for the completion of all tasks and returns their results in completion order.

Source

pub fn abort_all(&mut self)

Aborts all running tasks.

Any output slots associated with incomplete tasks are cleaned up when the task wrapper is dropped.

Source

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

Aborts all tasks and then waits for them to finish.

Returns results for tasks that completed before the abort took effect.

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> Drop for ScopedJoinSet<'scope, T>
where T: Send,

Source§

fn drop(&mut self)

Drops the set.

§Panics / Aborts

A ScopedJoinSet cannot be dropped implicitly.
If .join_all() or .shutdown() has not been called beforehand, this destructor prints a diagnostic and aborts the process.

This is a safety guarantee: tasks may borrow stack data, and allowing implicit drop would risk those tasks outliving their scope.

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>
where T: RefUnwindSafe,

§

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

§

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

§

impl<'scope, T> UnwindSafe for ScopedJoinSet<'scope, T>
where T: RefUnwindSafe,

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.