Is it possible (logical?) to convert a result containing a future into a future that resolves to a result?
The following function is a bit broken, but hopefully makes what I am trying to achieve more clear:
use std::future::Future;
fn transpose<T,U,E>(result: Result<T,E>) -> impl Future<Output = Result<U, E>>
where T: Future<Output = Result<U,E>> /* not sure if this is the correct trait bound */ {
match result {
Ok(inner) => /* a future that eventually resolves to a Result<U, E> */
Err(error) => /* a future that immediately resolves to a Result<U, E>::Err(error) */
}
}
To give some context: I found myself needing to do this after calling a async
function from a closure passed to Result::map
, so perhaps that was my first mistake.
.await
ing is actually resolving the future, which is more than a conversion. – Jahncke