Is there some native way of iterating over .await-able calls?
Or some collection to produce Stream for it's T like
while let Some(r) = async_vec.next_async().await {
// do stuff with r
}
For example lets consider this:
let mut a_vec: Vec<HasAwaitableMethod> = ...
// this will block on each run
for v in a_vec {
//...
v.awaitable_method.await;
//...
}
let mut b_vec: AsyncVec<ImplNextAsync> = ...
// this should continue to next iteration asynchronously
while let Some(v) = async_vec.next_async().await {
// do stuff with result when done
}
Here is a simplified version of the problem you are seeing:
fn main() {
let mut vec: Vec<&str> = Vec::new();
for i in 0..5 {
let s = format!("{}", i);
vec.push(&s);
// s goes out of scope here
}
println!("{:?}", vec); // oops, s is used here
}
The ways to make it work are:
Ensure that s is stored outside the loop.
Make the thing in the collection take ownership of s.
In this case solution two is easiest, and there is a pretty easy way to do it:
for i in 0..10 {
let mut s = S { v: 0 };
futs.push(async move { s.sleep(10 * i).await });
}