Struct Scope

Source
pub struct Scope<'scope, 'env: 'scope, R: 'env> { /* private fields */ }
Expand description

Represents a moro “async scope”. See the async_scope macro for details.

Implementations§

Source§

impl<'scope, 'env, R> Scope<'scope, 'env, R>

Source

pub fn terminate<T>(&'scope self, value: R) -> impl Future<Output = T> + 'scope
where T: 'scope,

Terminate the scope immediately – all existing jobs will stop at their next await point and never wake up again. Anything on their stacks will be dropped. This is most useful for propagating errors, but it can be used to propagate any kind of final value (e.g., perhaps you are searching for something and want to stop once you find it.)

This returns a future that you should await, but it will never complete (because you will never be reawoken). Since termination takes effect at the next await point, awaiting the returned future ensures that your current future stops immediately.

§Examples
let result = moro::async_scope!(|scope| {
    scope.spawn(async { /* ... */ });

    // Calling `scope.terminate` here will terminate the async
    // scope and use the string `"cancellation-value"` as
    // the final value.
    let result: () = scope.terminate("cancellation-value").await;
    unreachable!() // this code never executes
}).await;

assert_eq!(result, "cancellation-value");
Source

pub fn spawn<T>( &'scope self, future: impl Future<Output = T> + 'scope, ) -> Spawned<impl Future<Output = T>>
where T: 'scope,

Spawn a job that will run concurrently with everything else in the scope. The job may access stack fields defined outside the scope. The scope will not terminate until this job completes or the scope is cancelled.

Auto Trait Implementations§

§

impl<'scope, 'env, R> !Freeze for Scope<'scope, 'env, R>

§

impl<'scope, 'env, R> !RefUnwindSafe for Scope<'scope, 'env, R>

§

impl<'scope, 'env, R> !Send for Scope<'scope, 'env, R>

§

impl<'scope, 'env, R> !Sync for Scope<'scope, 'env, R>

§

impl<'scope, 'env, R> Unpin for Scope<'scope, 'env, R>
where R: Unpin,

§

impl<'scope, 'env, R> !UnwindSafe for Scope<'scope, 'env, R>

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.