Skip to main content

Group

Struct Group 

Source
pub struct Group<E> { /* private fields */ }
Expand description

A group of concurrent tasks that propagates the first error.

Create a new group with Group::new, spawn tasks with spawn or try_spawn, and collect results with wait.

Implementations§

Source§

impl<E> Group<E>
where E: Send + 'static,

Source

pub fn new() -> Self

Creates a new Group with no concurrency limit.

Source

pub fn set_limit(&mut self, n: usize)

Limits the number of tasks that may run concurrently to at most n.

A limit of 0 prevents any new task from running. A negative value would indicate no limit, but since this is usize, no limit is the default (created by new).

§Panics

Must not be called while any tasks are active.

Source§

impl<E> Group<E>
where E: Send + 'static,

Source

pub fn spawn<F, Fut>(&self, f: F)
where F: FnOnce() -> Fut + Send + 'static, Fut: Future<Output = Result<(), E>> + Send + 'static,

Spawns a new task.

If a concurrency limit was set with set_limit and the maximum number of tasks is already running, the newly spawned task will block inside itself (not the caller) until a slot opens up.

This method returns immediately and never blocks the caller.

§Panics

The task must not panic. See the crate-level docs.

Source

pub fn try_spawn<F, Fut>(&self, f: F) -> bool
where F: FnOnce() -> Fut + Send + 'static, Fut: Future<Output = Result<(), E>> + Send + 'static,

Tries to spawn a new task without waiting for a concurrency slot.

If a limit is set and the maximum number of tasks is running, returns false without spawning the task. Otherwise spawns the task and returns true.

If no limit was set, this is equivalent to spawn and always returns true.

Source§

impl<E> Group<E>
where E: Send + 'static,

Source

pub async fn wait(self) -> Result<(), E>

Waits for all spawned tasks to complete.

Returns the first error encountered, or Ok(()) if all tasks succeeded.

Consumes the group — no more tasks can be spawned after calling this.

Trait Implementations§

Source§

impl<E> Default for Group<E>
where E: Send + 'static,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<E> !Freeze for Group<E>

§

impl<E> !RefUnwindSafe for Group<E>

§

impl<E> !UnwindSafe for Group<E>

§

impl<E> Send for Group<E>
where E: Send,

§

impl<E> Sync for Group<E>
where E: Send,

§

impl<E> Unpin for Group<E>

§

impl<E> UnsafeUnpin for Group<E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.