I'm trying to store different async
functions in a Vec
within a struct.
I've tried different things like using Box<Fn -> Future<Output = ...>>
, to fall into dynamic dispatch and also wrap Output
into Box<dyn>
. But I get this error all the time: expected opaque type, found a different opaque type
.
Here's my last tentative solution to the problem:
use futures::Future;
//use futures::prelude::TryFuture;
struct Failure<A> {
data: A,
}
async fn add(data: u8) -> Result<u8, Failure<u8>> {
Ok(data + 1)
}
async fn add2(data: u8) -> Result<u8, Failure<u8>> {
Ok(data + 2)
}
struct Transaction<A, T>
where
T: Future<Output = Result<A, Failure<A>>>,
{
steps: Vec<Step<A, T>>,
}
struct Step<A, T>
where
T: Future<Output = Result<A, Failure<A>>>,
{
c: fn(A) -> T,
}
fn main() {
Transaction {
steps: vec![
Step { c: add },
Step { c: add2 }
],
};
}
This code fails to compile:
--> src/main.rs:34:23
|
34 | Step { c: add2 }
| ^^^^ expected opaque type, found a different opaque type
|
= note: expected fn pointer `fn(_) -> impl core::future::future::Future`
found fn item `fn(_) -> impl core::future::future::Future {add2}`
= note: distinct uses of `impl Trait` result in different opaque types
How do I make it compile?