Add Rust example to prior art

Using the no-op `Waker`, we can express generators and coroutines in
Rust.  Let's close our list of prior art examples with that.
This commit is contained in:
Travis Cross 2024-03-28 19:17:44 +00:00
parent 7eacd063df
commit a02190547f
1 changed files with 25 additions and 0 deletions

View File

@ -582,6 +582,31 @@ Note that there is no library being used here and that `yield` is not a keyword
[koka]: https://koka-lang.github.io/
## Rust
In Rust, `async` blocks are built on top of the coroutine transformation. Using a no-op `Waker`, it's possible to expose this transformation. With that, we can build generators. Without the assistance of macros, the result looks like this:
```rust
let odd_dup = |xs| {
Gen::new(async move |mut y| {
for x in xs {
if x % 2 == 1 {
y.r#yield(x * 2).await;
}
}
})
};
let odd_dup = pin!(odd_dup(1u8..20));
let odd_dup = odd_dup.init();
for (i, x) in odd_dup.enumerate() {
assert_eq!((i as u8 * 2 + 1) * 2, x);
}
```
Crates such as [`genawaiter`][] use this technique.
# Unresolved questions
[unresolved-questions]: #unresolved-questions