Skip to content

Commit 7f38236

Browse files
committed
improve lifetimes
1 parent 6635ab7 commit 7f38236

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

async-stream/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ proc-macro-hack = "0.5.8"
1111

1212
[dev-dependencies]
1313
tokio = "=0.2.0-alpha.1"
14+
tokio-test = "=0.2.0-alpha.1"
1415
futures-util-preview = "=0.3.0-alpha.17"

async-stream/src/yielder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ thread_local!(static STORE: Cell<*mut ()> = Cell::new(ptr::null_mut()));
3030

3131
// ===== impl Sender =====
3232

33-
impl<T: Unpin + 'static> Sender<T> {
33+
impl<T: Unpin> Sender<T> {
3434
pub fn send(&mut self, value: T) -> impl Future<Output = ()> {
3535
Send { value: Some(value) }
3636
}
@@ -40,7 +40,7 @@ struct Send<T> {
4040
value: Option<T>,
4141
}
4242

43-
impl<T: Unpin + 'static> Future for Send<T> {
43+
impl<T: Unpin> Future for Send<T> {
4444
type Output = ();
4545

4646
fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<()> {

async-stream/tests/basic.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
use async_stream::stream;
44

5-
use tokio::prelude::*;
65
use futures_util::pin_mut;
6+
use tokio::prelude::*;
7+
use tokio::sync::mpsc;
8+
use tokio_test::assert_ok;
79

810
#[tokio::test]
911
async fn noop_stream() {
@@ -81,3 +83,43 @@ async fn return_stream() {
8183
assert_eq!(2, values[1]);
8284
assert_eq!(3, values[2]);
8385
}
86+
87+
#[tokio::test]
88+
async fn consume_channel() {
89+
let (mut tx, mut rx) = mpsc::channel(10);
90+
91+
let s = stream! {
92+
while let Some(v) = rx.recv().await {
93+
yield v;
94+
}
95+
};
96+
97+
pin_mut!(s);
98+
99+
for i in 0..3 {
100+
assert_ok!(tx.send(i).await);
101+
assert_eq!(Some(i), s.next().await);
102+
}
103+
104+
drop(tx);
105+
assert_eq!(None, s.next().await);
106+
}
107+
108+
#[tokio::test]
109+
async fn borrow_self() {
110+
struct Data(String);
111+
112+
impl Data {
113+
fn stream<'a>(&'a self) -> impl Stream<Item = &str> + 'a {
114+
stream! {
115+
yield &self.0[..];
116+
}
117+
}
118+
}
119+
120+
let data = Data("hello".to_string());
121+
let s = data.stream();
122+
pin_mut!(s);
123+
124+
assert_eq!(Some("hello"), s.next().await);
125+
}

0 commit comments

Comments
 (0)