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,
impl<'scope, T> ScopedJoinSet<'scope, T>where
T: 'static,
Sourcepub fn spawn<F>(&mut self, task: F)
pub fn spawn<F>(&mut self, task: F)
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.
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.
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()
.