feat: tokio stream

This commit is contained in:
2022-03-22 00:43:39 +08:00
parent 3f38ba99f7
commit 8fa4284e6c
4 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
use tokio_stream::{self as stream, StreamExt};
#[tokio::main]
async fn main() {
let mut stream = stream::iter(vec![0, 1, 2]);
while let Some(value) = stream.next().await {
println!("Got {}", value);
}
}

View File

@@ -0,0 +1,29 @@
use std::pin::Pin;
use std::task::{Context, Poll};
use futures_core::Stream;
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() {
let mut stream = MyStream(0);
while let Some(value) = stream.next().await {
println!("Got {}", value);
}
}
struct MyStream(i32);
impl Stream for MyStream {
type Item = i32;
fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if self.0 < 10 {
self.0 += 1;
Poll::Ready(Some(self.0))
} else {
Poll::Ready(None)
}
}
}