#yield #coroutine #iterator #async-await #generator

yield-return

Implement a coroutine like C#‘s yield return using Rust’s async, await

2 unstable releases

new 0.2.0 Jan 10, 2025
0.1.0 Apr 21, 2023

#426 in Rust patterns

Download history 8/week @ 2024-09-24 2/week @ 2024-10-01 1/week @ 2024-12-03 9/week @ 2024-12-10 135/week @ 2025-01-07

135 downloads per month

MIT/Apache

19KB
276 lines

yield-return-rs

Crates.io Docs.rs Actions Status

Implement a coroutine like C#'s yield return using Rust's async, await.

Example

use yield_return::Iter;
let iter = Iter::new(|mut y| async move {
    y.ret(1).await;
    y.ret(2).await;
});
let list: Vec<_> = iter.collect();
assert_eq!(list, vec![1, 2]);

Available Types

This crate provides several iterator types that differ based on two characteristics:

  • Whether they implement Iterator or Stream
  • Whether they require and implement Send

The following table shows the available types:

Send Not Send
Iterator Iter LocalIter
Stream AsyncIter LocalAsyncIter

Compare with other crates

While async-stream and genawaiter serve similar purposes, yield-return focuses on usability over performance. This design philosophy is reflected in two key characteristics:

  • Type parameters only expose the external interface, not implementation details
  • Clean API using only types and functions (no macros)

License

This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~25KB