File tree Expand file tree Collapse file tree 4 files changed +38
-22
lines changed Expand file tree Collapse file tree 4 files changed +38
-22
lines changed Original file line number Diff line number Diff line change 1
1
This library provides a way to create asynchronous generator using the ` async/await ` feature in stable Rust.
2
2
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
+
3
7
# Installation
4
8
5
9
Add it as a dependency to your Rust project by adding the following line to your ` Cargo.toml ` file:
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -39,6 +39,19 @@ fn yield_single_value() {
39
39
} )
40
40
}
41
41
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
+
42
55
#[ test]
43
56
fn yield_multi_value ( ) {
44
57
block_on ( async {
@@ -178,24 +191,3 @@ fn yield_with_select() {
178
191
assert_eq ! ( values, vec![ "hey" ] ) ;
179
192
} )
180
193
}
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
- // }
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ fn yield_then_err() {
23
23
let mut s = pin ! ( gen ! {
24
24
yield "hello" ;
25
25
Err ( "world" ) ?;
26
- Result :: <_ , & str > :: Ok ( ( ) )
26
+ Ok ( ( ) )
27
27
} ) ;
28
28
assert_eq ! ( s. resume( ) . await , GeneratorState :: Yielded ( "hello" ) ) ;
29
29
assert_eq ! ( s. resume( ) . await , GeneratorState :: Complete ( Err ( "world" ) ) ) ;
You can’t perform that action at this time.
0 commit comments