I want to write something like this:
while has_data_in_stream && !time_to_stop {
do_something_with_data_in_stream()
}
use tokio::{
stream::{self, StreamExt},
sync::oneshot,
};
#[tokio::main]
async fn main() {
let mut stream1 = stream::iter(vec![1, 2, 3]);
let (stop_read, time_to_stop): (oneshot::Sender<()>, _) = oneshot::channel();
loop {
let next = tokio::select! {
v = stream1.next() => v.unwrap(),
_ = time_to_stop => break,
else => break,
};
println!("next: {}", next);
}
}
but as result I got error about:
19 | let (stop_read, time_to_stop): (oneshot::Sender<()>, _) = oneshot::channel();
| ------------ move occurs because `time_to_stop` has type `tokio::sync::oneshot::Receiver<()>`, which does not implement the `Copy` trait
...
23 | _ = time_to_stop => break,
| ^^^^^^^^^^^^ value moved here, in previous iteration of loop
How can I fix this without removing loop ?
I know I can fix it via select!(stream.for_each, time_to_stop)
but this has another problem,
so I want to use loop
, is it possible?