feat: tokio stream
This commit is contained in:
10
__concurrent/async_study/examples/tokio_stream.rs
Normal file
10
__concurrent/async_study/examples/tokio_stream.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
29
__concurrent/async_study/examples/tokio_stream2.rs
Normal file
29
__concurrent/async_study/examples/tokio_stream2.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user