Crate async_coroutine

Source
Expand description

§Async Coroutine

License Build Status crates.io docs.rs

This crate provides a coroutine implementation based on Rust’s async/await syntax.

§Usage

use async_coroutine::{Coroutine, State};

let mut co = Coroutine::new(|handle, value| async move {
    assert_eq!(value, 1);
    assert_eq!(handle.yield_(true).await, 2);
    assert_eq!(handle.yield_(false).await, 3);
    "Bye"
});

assert_eq!(co.resume_with(1), State::Yield(true));
assert_eq!(co.resume_with(2), State::Yield(false));
assert_eq!(co.resume_with(3), State::Complete("Bye"));

Structs§

Coroutine
A coroutine that can yield values of type Y, can be resumed with a value of type R and completes with a value of type T.
YieldHandle
The yield handle can be used from within the coroutine to yield values and receive a resume value when the coroutine is resumed.

Enums§

State
Represents the state of a coroutine, which can either yield a value of type Y or complete with a value of type T.

Type Aliases§

Generator
A generator is a coroutine that does not have a resume value.