Skip to content

Commit 8cb501f

Browse files
added example and updated README.md
1 parent 6a5a90d commit 8cb501f

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
This library provides a way to create asynchronous generator using the `async/await` feature in stable Rust.
22

3+
It is similar to [async-stream](https://docs.rs/async-stream/latest/async_stream/),
4+
But closely mimics the [Coroutine API](https://doc.rust-lang.org/std/ops/trait.Coroutine.html),
5+
Allowing the generator also return a value upon completion, in addition to yielding intermediate values.
6+
37
# Installation
48

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

examples/tcp_accept.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use async_gen::gen;
2+
use futures_util::StreamExt;
3+
use std::pin::pin;
4+
use tokio::net::TcpListener;
5+
6+
#[tokio::main]
7+
async fn main() {
8+
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
9+
10+
let mut incoming = pin!(gen! {
11+
loop {
12+
let (socket, _) = listener.accept().await.unwrap();
13+
yield socket;
14+
}
15+
});
16+
17+
while let Some(v) = incoming.next().await {
18+
println!("handle = {:?}", v);
19+
}
20+
}

tests/stream.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ fn yield_single_value() {
3939
})
4040
}
4141

42+
#[test]
43+
fn fused() {
44+
block_on(async {
45+
let s = pin!(gen! {
46+
yield "hello";
47+
});
48+
let mut s = s.fuse();
49+
assert_eq!(s.next().await, Some("hello"));
50+
assert_eq!(s.next().await, None);
51+
assert_eq!(s.next().await, None);
52+
});
53+
}
54+
4255
#[test]
4356
fn yield_multi_value() {
4457
block_on(async {
@@ -178,24 +191,3 @@ fn yield_with_select() {
178191
assert_eq!(values, vec!["hey"]);
179192
})
180193
}
181-
182-
// #[test]
183-
// fn inner_try_stream() {
184-
// use async_stream::try_stream;
185-
// use tokio::select;
186-
187-
// async fn do_stuff_async() {}
188-
189-
// let _ = stream! {
190-
// select! {
191-
// _ = do_stuff_async() => {
192-
// let another_s = try_stream! {
193-
// yield;
194-
// };
195-
// let _: Result<(), ()> = Box::pin(another_s).next().await.unwrap();
196-
// },
197-
// else => {},
198-
// }
199-
// yield
200-
// };
201-
// }

tests/try_stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn yield_then_err() {
2323
let mut s = pin!(gen! {
2424
yield "hello";
2525
Err("world")?;
26-
Result::<_, &str>::Ok(())
26+
Ok(())
2727
});
2828
assert_eq!(s.resume().await, GeneratorState::Yielded("hello"));
2929
assert_eq!(s.resume().await, GeneratorState::Complete(Err("world")));

0 commit comments

Comments
 (0)