rust-lang / futures-rs

Zero-cost asynchronous programming in Rust
https://rust-lang.github.io/futures-rs/
Apache License 2.0
5.34k stars 616 forks source link

feature flag for debugging to make join functions #2767

Open anatawa12 opened 1 year ago

anatawa12 commented 1 year ago

When I use debugger to debug my tool with try_join_all or other join functions in futures, I want to replace try_join_all with non-parallel versions for easy debugging.

I wrote helper functions like below but I wonder if some option to make join functions non-parallel.

async fn try_join_all<I>(iter: I) -> Result<Vec<<<I as IntoIterator>::Item as TryFuture>::Ok>, <<I as IntoIterator>::Item as TryFuture>::Error>
    where
        I: IntoIterator,
        I::Item: TryFuture {
    let mut vec = Vec::new();
    for mut fut in iter {
        let mut pinned = pin!(fut);
        vec.push(std::future::poll_fn(|c| pinned.as_mut().try_poll(c)).await?);
    }
    Ok(vec)
}

async fn join_all<I>(iter: I) -> Vec<<<I as IntoIterator>::Item as Future>::Output>
    where
        I: IntoIterator,
        I::Item: Future {
    let mut vec = Vec::new();
    for mut fut in iter {
        let mut pinned = pin!(fut);
        vec.push(pinned.as_mut().await);
    }
    vec
}

async fn join<A, B>(a: A, b: B) -> (<A as Future>::Output, <B as Future>::Output)
    where A: Future, B: Future {
    (a.await, b.await)
}