Files
simple-rust-tests/__concurrent/async_study/examples/tokio_stream2.rs
2022-03-22 00:43:39 +08:00

29 lines
590 B
Rust

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)
}
}
}