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,
impl<'scope, T> ScopedJoinSet<'scope, T>where
T: 'scope + Send,
Sourcepub fn spawn<F>(&mut self, task: F)
pub fn spawn<F>(&mut self, task: F)
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.
Sourcepub async fn join_next(&mut self) -> Option<Result<T, JoinError>>
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.
Sourcepub fn try_join_next(&mut self) -> Option<Result<T, JoinError>>
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.
Sourcepub async fn join_all(self) -> Vec<Result<T, JoinError>>
pub async fn join_all(self) -> Vec<Result<T, JoinError>>
Waits for the completion of all tasks and returns their results in completion order.
Trait Implementations§
Source§impl<'scope, T> Default for ScopedJoinSet<'scope, T>
impl<'scope, T> Default for ScopedJoinSet<'scope, T>
Source§fn default() -> ScopedJoinSet<'scope, T>
fn default() -> ScopedJoinSet<'scope, T>
Source§impl<'scope, T> Drop for ScopedJoinSet<'scope, T>where
T: Send,
impl<'scope, T> Drop for ScopedJoinSet<'scope, T>where
T: Send,
Source§fn drop(&mut self)
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.