I am trying to create a function that takes as arguments a mutable reference to a vector and an element. The function first transforms the element and then pushes it to the vector.
The problem arises from the types. The element is a future, and transforming it creates a new type that can not be named specifically.
Here is a simplified code example:
extern crate futures;
use futures::{Future, future};
fn push_to_vec<F,T>(vec: &mut Vec<T>, elem: F)
where
F: Future<Item=u32, Error=()>,
{
vec.push(elem.map(|x| x + 1));
}
fn main() {
let mut vec = Vec::new();
let my_fut = future::ok::<u32,()>(3);
push_to_vec(&mut vec, my_fut);
}
Compilation error:
error[E0308]: mismatched types
--> src/main.rs:8:14
|
8 | vec.push(elem.map(|x| x + 1));
| ^^^^^^^^^^^^^^^^^^^ expected type parameter, found struct `futures::Map`
|
= note: expected type `T`
found type `futures::Map<F, [closure@src/main.rs:8:23: 8:32]>`
Is there anything I can do to tell the compiler to pick the right type for the contents of the Vector argument? (type T)
My current workaround for this problem is not using functions. This way I don't have to specify types. I inlined much of my logic code into one big function. It is still manageable, but it gets more and more difficult to add code.
I know that I can put some things into boxes, but I don't feel comfortable using this tradeoff when it seems like I don't really need a Box abstraction here. The Vector contents should have exactly the same type all the time.
Any ideas are appreciated!
real.