Expand description
A scoped, lifetime-aware wrapper around tokio::task::JoinSet.
ScopedJoinSet enables dynamically spawning async tasks whose futures are
tied to a specific Rust lifetime 'scope. This provides a safe alternative
to hand-managed unsafe approaches while enabling advanced async task
orchestration where futures do not need to be 'static.
Each spawned task is split into:
- A [
FutureHolder], owning the actual boxed future and ensuring proper drop ordering. - A [
WeakFuture] (inside theJoinSet), which the Tokio runtime polls.
When the ScopedJoinSet is dropped, all tasks are aborted and all future
memory is reclaimed safely.
§Guarantees
- Futures spawned via
ScopedJoinSet::spawncannot outlive'scope. join_next()is cancellation-safe.- Dropping the entire set aborts all tasks.
§Example
use scoped_join_set::ScopedJoinSet;
#[tokio::main]
async fn main() {
let temporary_value = Box::new(5);
let value_ref = &temporary_value;
let mut set = ScopedJoinSet::<u32>::new();
set.spawn(async {
println!("I have a {value_ref}");
**value_ref
});
while let Some(result) = set.join_next().await {
assert_eq!(result.unwrap(), *temporary_value);
}
}Structs§
- Scoped
Join Set - A scoped wrapper around
tokio::task::JoinSetfor dynamically spawning futures with a lifetime'scope.
Enums§
- Join
Error - Errors returned when joining scoped tasks.