我有以下代码
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) -> () {
()
}
}
此外,您可以在Rust Playground上在线运行它。
出现以下错误:
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.
现在,这真的很奇怪,因为它清楚地表明它正在得到它所期望的。我不明白哪种类型比哪种更通用。我对生命周期的了解不足以调试此代码。有人可以对此有更多的了解吗?