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>
impl<'scope, 'env, R> Scope<'scope, 'env, R>
Sourcepub fn terminate<T>(&'scope self, value: R) -> impl Future<Output = T> + 'scopewhere
T: 'scope,
pub fn terminate<T>(&'scope self, value: R) -> impl Future<Output = T> + 'scopewhere
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");
Sourcepub fn spawn<T>(
&'scope self,
future: impl Future<Output = T> + 'scope,
) -> Spawned<impl Future<Output = T>> ⓘwhere
T: 'scope,
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.