Skip to content

Commit 6635ab7

Browse files
committed
get tests passing
1 parent b31b917 commit 6635ab7

File tree

3 files changed

+109
-7
lines changed

3 files changed

+109
-7
lines changed

async-stream-impl/src/lib.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ use syn::parse::{Parse, ParseStream, Result};
88
use syn::visit_mut::VisitMut;
99

1010
struct AsyncStreamImpl {
11+
yielder: syn::Ident,
1112
stmts: Vec<syn::Stmt>,
13+
num_yield: u32,
1214
}
1315

1416
struct Scrub {
1517
yielder: syn::Ident,
1618
unit: Box<syn::Expr>,
19+
num_yield: u32,
1720
}
1821

1922
impl Parse for AsyncStreamImpl {
@@ -25,6 +28,7 @@ impl Parse for AsyncStreamImpl {
2528
let mut scrub = Scrub {
2629
yielder,
2730
unit: syn::parse_quote!(()),
31+
num_yield: 0,
2832
};
2933

3034
while !input.is_empty() {
@@ -33,14 +37,22 @@ impl Parse for AsyncStreamImpl {
3337
stmts.push(stmt);
3438
}
3539

36-
Ok(AsyncStreamImpl { stmts })
40+
let Scrub { yielder, num_yield, .. } = scrub;
41+
42+
Ok(AsyncStreamImpl {
43+
yielder,
44+
stmts,
45+
num_yield,
46+
})
3747
}
3848
}
3949

4050
impl VisitMut for Scrub {
4151
fn visit_expr_mut(&mut self, i: &mut syn::Expr) {
4252
match i {
4353
syn::Expr::Yield(expr) => {
54+
self.num_yield += 1;
55+
4456
let value_expr = if let Some(ref e) = expr.expr {
4557
e
4658
} else {
@@ -58,9 +70,22 @@ impl VisitMut for Scrub {
5870
#[proc_macro_hack]
5971
pub fn async_stream_impl(input: TokenStream) -> TokenStream {
6072
let AsyncStreamImpl {
73+
yielder,
6174
stmts,
75+
num_yield,
6276
} = syn::parse_macro_input!(input as AsyncStreamImpl);
6377

64-
quote!(#(#stmts)*).into()
65-
// quote!(#input).into()
78+
if num_yield == 0 {
79+
quote!({
80+
if false {
81+
#yielder.send(()).await;
82+
}
83+
84+
#(#stmts)*
85+
}).into()
86+
} else {
87+
quote!({
88+
#(#stmts)*
89+
}).into()
90+
}
6691
}

async-stream/src/yielder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct Send<T> {
4343
impl<T: Unpin + 'static> Future for Send<T> {
4444
type Output = ();
4545

46-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
46+
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {
4747
if self.value.is_none() {
4848
return Poll::Ready(());
4949
}

async-stream/tests/basic.rs

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,83 @@
1+
#![feature(async_await)]
2+
13
use async_stream::stream;
24

3-
#[test]
4-
fn smoke() {
5-
stream!();
5+
use tokio::prelude::*;
6+
use futures_util::pin_mut;
7+
8+
#[tokio::test]
9+
async fn noop_stream() {
10+
let s = stream! {};
11+
pin_mut!(s);
12+
13+
while let Some(_) = s.next().await {
14+
unreachable!();
15+
}
16+
}
17+
18+
#[tokio::test]
19+
async fn empty_stream() {
20+
let mut ran = false;
21+
22+
{
23+
let r = &mut ran;
24+
let s = stream! {
25+
*r = true;
26+
println!("hello world!");
27+
};
28+
pin_mut!(s);
29+
30+
while let Some(_) = s.next().await {
31+
unreachable!();
32+
}
33+
}
34+
35+
assert!(ran);
36+
}
37+
38+
#[tokio::test]
39+
async fn yield_single_value() {
40+
let s = stream! {
41+
yield "hello";
42+
};
43+
44+
let values: Vec<_> = s.collect().await;
45+
46+
assert_eq!(1, values.len());
47+
assert_eq!("hello", values[0]);
48+
}
49+
50+
#[tokio::test]
51+
async fn yield_multi_value() {
52+
let s = stream! {
53+
yield "hello";
54+
yield "world";
55+
yield "dizzy";
56+
};
57+
58+
let values: Vec<_> = s.collect().await;
59+
60+
assert_eq!(3, values.len());
61+
assert_eq!("hello", values[0]);
62+
assert_eq!("world", values[1]);
63+
assert_eq!("dizzy", values[2]);
64+
}
65+
66+
#[tokio::test]
67+
async fn return_stream() {
68+
fn build_stream() -> impl Stream<Item = u32> {
69+
stream! {
70+
yield 1;
71+
yield 2;
72+
yield 3;
73+
}
74+
}
75+
76+
let s = build_stream();
77+
78+
let values: Vec<_> = s.collect().await;
79+
assert_eq!(3, values.len());
80+
assert_eq!(1, values[0]);
81+
assert_eq!(2, values[1]);
82+
assert_eq!(3, values[2]);
683
}

0 commit comments

Comments
 (0)