error[E0308]: mismatched types
--> src/main.rs:19:5
|
19 | async { println!("World") }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^- help: consider using a semicolon here: `;`
| |
| expected `()`, found opaque type
|
= note: expected unit type `()`
found opaque type `impl futures::Future<Output = [async output]>`
To make this compile, the correct return type would be impl Future<Output = ()>
, but I do not think that it is what you want because the async
keyword on the function would then expand it to the following, with a "double future".
fn fn_name() -> impl Future<Output = impl Future<Output = ()>> {
async {
println!("Hello");
async { println!("World") }
}
}
error[E0618]: expected function, found `Pin<Box<dyn futures::Future<Output = (bool, Option<Box<(dyn Fn() + 'static)>>)>>>`
--> src/main.rs:24:31
|
23 | for func in f.func {
| ---- `func` has type `Pin<Box<dyn futures::Future<Output = (bool, Option<Box<(dyn Fn() + 'static)>>)>>>`
24 | let return_callback = func().await;
| ^^^^--
| |
| call expression requires function
The type of func
is a kind of future, which is a different thing from a function. You can only .await
a future — you can't call it with ()
. That would then be func.await
. Additionally, return_callback
is also not a function. You would need to unpack the boolean/function tuple before you can access it. (And the function does not return a future, so you wouldn't be able to .await
it even if you did that.)
error[E0277]: `fn() -> dyn futures::Future<Output = (bool, Option<Box<(dyn Fn() + 'static)>>)>` is not a future
--> src/main.rs:13:24
|
13 | self.func.push(Box::pin(callback));
| ^^^^^^^^^^^^^^^^^^ `fn() -> dyn futures::Future<Output = (bool, Option<Box<(dyn Fn() + 'static)>>)>` is not a future
|
= help: the trait `futures::Future` is not implemented for `fn() -> dyn futures::Future<Output = (bool, Option<Box<(dyn Fn() + 'static)>>)>`
= note: required for the cast to the object type `dyn futures::Future<Output = (bool, Option<Box<(dyn Fn() + 'static)>>)>`
As the error said, functions and futures are different things. You cannot convert a function into a future. Additionally, a return value of dyn Future<...>
will never be possible, because a dyn Trait
can only ever exist inside a Box
(or another reference type), which it doesn't here. You should probably wrap it in Pin<Box<...>>
.