Is join all function does not work on struct methods?

is join all function does not work on struct methods ?

Code

let incoming = join_all(vec![
    context.request.get_method(),
    context.request.get_path(),
])
.await;

Error

mismatched types
    expected type `impl futures::Future<Output = String>` (opaque type at <src/structs/request.rs:49:43>)
found opaque type `impl futures::Future<Output = String>` (opaque type at <src/structs/request.rs:61:41>)
consider `await`ing on both `Future`s
distinct uses of `impl Trait` result in different opaque typesrustcE0308
request.rs(61, 41): while checking the return type of the `async fn`

Static functions are working fine with same code but not working with struct methods.
Thank you.

Problem is not join_all, but vec creation.

Each async function has its own anonymous return type, the only thing we can know about which is that it implements Future with the specified output type. However, Vec is a homogeneous container - it can't store values of different types.
The usual way to do this is either using macros like tokio::join instead of functions, or type-erasing the futures by calling futures::FutureExt::boxed, so that join_all receives a Vec<Pin<Box<dyn Future>>>.

1 Like

Just use the tokio::join! macro instead.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.