this is what I looking for:
use std::time::Duration;
use tokio::{
stream::{self, StreamExt},
sync::oneshot,
time,
};
#[tokio::main]
async fn main() {
let mut stream1 = stream::iter(vec![1, 2, 3]);
let (stop_read, mut time_to_stop): (oneshot::Sender<()>, _) = oneshot::channel();
tokio::spawn(async move {
time::delay_for(Duration::from_millis(100)).await;
if let Err(_) = stop_read.send(()) {
eprintln!("somthing goes wrong");
}
});
loop {
let next = tokio::select! {
v = stream1.next() => {
time::delay_for(Duration::from_millis(50)).await;
v.unwrap()
}
_ = &mut time_to_stop => {
println!("time_to_stop trigger");
break;
}
else => break,
};
println!("next: {}", next);
}
}
@kornel, thanks a lot!