Skip to main content

Crate async_gen

Crate async_gen 

Source
Expand description

This library provides a way to create asynchronous generator using the async/await feature in stable Rust.

It is similar to async-stream, But closely mimics the Coroutine API, Allowing the generator also return a value upon completion, in addition to yielding intermediate values.

§Installation

Add it as a dependency to your Rust project by adding the following line to your Cargo.toml file:

[dependencies]
async-gen = "0.3"

§Examples

use std::pin::pin;
use async_gen::{gen, GeneratorState};

#[nio::main]
async fn main() {
    let g = gen! {
        yield 42;
        return "42"
    };
    let mut g = pin!(g);
    assert_eq!(g.resume().await, GeneratorState::Yielded(42));
    assert_eq!(g.resume().await, GeneratorState::Complete("42"));
}

Re-exports§

pub use futures_core;

Macros§

gen
A macro for creating generator.
stream
Asynchronous stream

Structs§

AsyncGen
Represent an asyncronus generator. It implementations AsyncGenerator trait.
AsyncIter
An async iterator over the values yielded by an underlying generator.
Return
The return value produced by an async coroutine.

Enums§

GeneratorState
The result of a generator resumption.

Traits§

AsyncGenerator
Generators, also commonly referred to as coroutines.

Functions§

async_iter_from
Converts an AsyncGenerator into an async iterator.
gen
Creates a new generator, which implements the AsyncGenerator trait.