У меня есть следующий код
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.
Теперь это действительно странно, потому что ясно говорит, что он получает то, что ожидает. Я не понимаю, какой тип более общий, чем какой. Я недостаточно понимаю время жизни, чтобы отлаживать этот код. Может ли кто-нибудь рассказать об этом поподробнее?
Введите сообщение об ошибке, которое может ввести в заблуждение: ваша асинхронная функция эквивалентна
pub fn my_func<'r>(&'r mut self) -> impl Future<Output = ()> + 'r
.Я нашел кое-что, что компилируется, но не уверен, что мне нужно: play.rust-lang.org/…