Skip to main content

executor_scope

Function executor_scope 

Source
pub async fn executor_scope<'scope, E, F, T>(executor: &'scope E, f: F) -> T
where E: Executor, F: AsyncFnOnce(&ScopeExecutor<'scope, E>) -> T,
Expand description

Run an async closure with a scoped Executor wrapper.

If the future containing executor_scope itself is dropped (canceled externally), it will abort every outstanding task.

Unlike scope, futures spawned here must be Send + 'static they’re going onto the real executor, so they can’t borrow from the enclosing stack frame.

§Example

use async_rt::Executor;
use async_rt::global::ConfiguredExecutor;

let executor: ConfiguredExecutor = ConfiguredExecutor::default();
let total = executor
    .executor_scope(async |s| {
        let a = s.spawn(async { 1 + 2 });
        let b = s.spawn(async { 3 + 4 });
        a.await.unwrap() + b.await.unwrap()
    })
    .await;
    assert_eq!(total, 10);