What does it mean when Rust complains that two equal types don't match?
The following error appears to be comparing the type...
<for<'_> fn(&u32) -> impl futures::Future<Output = u32> {f} as FnOnce<(&u32,)>>::Output
...to itself.
error[E0308]: mismatched types
--> src/main.rs:31:18
|
31 | let output = map(f, input);
| ^^^ lifetime mismatch
|
= note: expected associated type `<for<'_> fn(&u32) -> impl futures::Future<Output = u32> {f} as FnOnce<(&u32,)>>::Output`
found associated type `<for<'_> fn(&u32) -> impl futures::Future<Output = u32> {f} as FnOnce<(&u32,)>>::Output`
= note: the required lifetime does not necessarily outlive the empty lifetime
note: the lifetime requirement is introduced here
--> src/main.rs:6:39
|
6 | pub fn map<U, V, W>(f: impl Fn(&U) -> W, items: Vec<U>) -> impl futures::Stream<Item = V>
| ^
I've reduced it to the following minimal example:
use futures::stream::{FuturesUnordered, StreamExt};
use async_stream::stream;
pub fn map<U, V, W>(f: impl Fn(&U) -> W, items: Vec<U>) -> impl futures::Stream<Item = V>
where V: Send, W: futures::Future<Output = V> + Send
{
stream! {
let mut futures = FuturesUnordered::new();
let mut i = 2;
if 2 <= items.len() {
futures.push(tokio::spawn(f(&items[0])));
futures.push(tokio::spawn(f(&items[1])));
while let Some(result) = futures.next().await {
let y = result.unwrap();
yield y;
futures.push(tokio::spawn(f(&items[i])));
i += 1
}
}
}
}
#[tokio::main]
async fn main() {
async fn f(x: &u32) -> u32 {
x + 1
}
let input = vec![1, 2, 3];
let output = map(f, input);
futures::pin_mut!(output);
while let Some(x) = output.next().await {
println!("{:?}", x);
}
}