I have the following code
use std::future::Future;
fn main() {
handle(Test::my_func);
}
fn handle<Fut>(fun: for<'r> fn(&'r mut Test) -> Fut) -> bool
where
Fut: Future<Output = ()>,
{
true
}
struct Test {}
impl Test {
pub async fn my_func<'r>(&'r mut self) -> () {
()
}
}
Also, you can run it online on Rust Playground.
The following error appears:
error[E0308]: mismatched types
--> src/main.rs:4:12
|
4 | handle(Test::my_func);
| ^^^^^^^^^^^^^ one type is more general than the other
...
17 | pub async fn my_func<'r>(&'r mut self) -> () {
| -- checked the `Output` of this `async fn`, found opaque type
|
= note: while checking the return type of the `async fn`
= note: expected fn pointer `for<'r> fn(&'r mut Test) -> impl Future`
found fn pointer `for<'r> fn(&'r mut Test) -> impl Future`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`
To learn more, run the command again with --verbose.
Now, this is really weird because it clearly says that it's getting what it's expecting. I don't understand which type is more general than which. I don't understand lifetimes enough to debug this code. Could someone spare some more insight into this?
pub fn my_func<'r>(&'r mut self) -> impl Future<Output = ()> + 'r
– Machuca