2 unstable releases

0.2.0 Mar 8, 2022
0.1.0 Sep 6, 2018

#1015 in Asynchronous

Download history 4787/week @ 2023-12-15 2231/week @ 2023-12-22 2941/week @ 2023-12-29 4145/week @ 2024-01-05 3559/week @ 2024-01-12 4664/week @ 2024-01-19 4572/week @ 2024-01-26 3997/week @ 2024-02-02 4491/week @ 2024-02-09 4445/week @ 2024-02-16 4324/week @ 2024-02-23 4339/week @ 2024-03-01 3981/week @ 2024-03-08 4064/week @ 2024-03-15 3795/week @ 2024-03-22 3024/week @ 2024-03-29

15,772 downloads per month
Used in 7 crates

MIT license

15KB
287 lines

tokio-scoped

tokio-scoped provides a scope function inspired by crossbeam but for the tokio Runtime. A scope allows one to spawn futures which do not have a 'static lifetime by ensuring each future spawned in the scope completes before the scope exits.

Example

#[tokio::main]
async fn main() {
    let mut v = String::from("Hello");
    tokio_scoped::scope(|scope| {
        // Use the scope to spawn the future.
        scope.spawn(async {
            v.push('!');
        });
    });
    // The scope won't exit until all spawned futures are complete.
    assert_eq!(v.as_str(), "Hello!");
}

lib.rs:

A scoped tokio Runtime that can be used to create Scopes which can spawn futures which can access stack data. That is, the futures spawned by the Scope do not require the 'static lifetime bound. This can be done safely by ensuring that the Scope doesn't exit until all spawned futures have finished executing. Be aware, that when a Scope exits it will block until every future spawned by the Scope completes. Therefore, one should take caution when created scopes within an asynchronous context, such as from within another spawned future.

Example

#[tokio::main]
async fn main() {
    let mut v = String::from("Hello");
    tokio_scoped::scope(|scope| {
        // Use the scope to spawn the future.
        scope.spawn(async {
            v.push('!');
        });
    });
    // The scope won't exit until all spawned futures are complete.
    assert_eq!(v.as_str(), "Hello!");
}

See also crossbeam::scope

Dependencies

~2.2–3.5MB
~51K SLoC