I have a collection of futures which I want to combine into a single future that gets them executed sequentially.
I looked into the futures_ordered
function. It seems to return the results sequentially but the futures get executed concurrently.
I tried to fold
the futures, combining them with and_then
. However, that is tricky with the type system.
let tasks = vec![ok(()), ok(()), ok(())];
let combined_task = tasks.into_iter().fold(
ok(()), // seed
|acc, task| acc.and_then(|_| task), // accumulator
);
This gives the following error:
error[E0308]: mismatched types
--> src/main.rs:10:21
|
10 | |acc, task| acc.and_then(|_| task), // accumulator
| ^^^^^^^^^^^^^^^^^^^^^^ expected struct `futures::FutureResult`, found struct `futures::AndThen`
|
= note: expected type `futures::FutureResult<_, _>`
found type `futures::AndThen<futures::FutureResult<_, _>, futures::FutureResult<(), _>, [closure@src/main.rs:10:34: 10:42 task:_]>`
I'm probably approaching this wrong but I've run out of ideas.
ok(())
- which returns aFutureResult
- as the initial value,fold
expects you to return aFutureResult
from each iteration of the closure. In other words, the inferred types are too specific. – Drowse