with_type_spawn_group

Function with_type_spawn_group 

Source
pub async fn with_type_spawn_group<Closure, Fut, ResultType, ReturnType>(
    of_type: PhantomData<ResultType>,
    body: Closure,
) -> ReturnType
where Closure: FnOnce(SpawnGroup<ResultType>) -> Fut, Fut: Future<Output = ReturnType> + 'static,
Expand description

Starts a scoped closure that takes a mutable SpawnGroup instance as an argument which can execute any number of child tasks which its result values are of the generic ResultType type.

This closure ensures that before the function call ends, all spawned child tasks are implicitly waited for, or the programmer can explicitly wait by calling its wait_for_all() method of the SpawnGroup struct.

This function use a threadpool of the same number of threads as the number of active processor count that is default amount of parallelism a program can use on the system for polling the spawned tasks

See SpawnGroup for more.

§Parameters

  • of_type: The type which the child task can return
  • body: an async closure that takes a mutable instance of SpawnGroup as an argument

§Returns

Anything the body parameter returns

§Example

use spawn_groups::GetType;
use spawn_groups::with_type_spawn_group;
use futures_lite::StreamExt;
use spawn_groups::Priority;

let final_result = with_type_spawn_group(i64::TYPE, |mut group| async move {
     for i in 0..=10 {
        group.spawn_task(Priority::default(), async move {
           // simulate asynchronous operation
           i
        });
     }

     group.fold(0, |acc, x| {
         acc + x
     }).await
 }).await;

 assert_eq!(final_result, 55);