例如:
use futures::future::Future;
fn main() {
let (stop_tokio, time_to_stop) = tokio::sync::oneshot::channel::<()>();
let handler = std::thread::spawn(|| {
tokio::run(
time_to_stop, // .map_err(|_| ())
);
});
handler.join().expect("join failed");
}
编译器打印错误:
error[E0271]: type mismatch resolving `<tokio_sync::oneshot::Receiver<()> as futures::future::Future>::Error == ()`
--> src/main.rs:6:9
|
6 | tokio::run(
| ^^^^^^^^^^ expected struct `tokio_sync::oneshot::error::RecvError`, found ()
|
= note: expected type `tokio_sync::oneshot::error::RecvError`
found type `()`
= note: required by `tokio::runtime::threadpool::run`
代码需要()
,而不是RecvError
,但编译器打印相反。
这是编译器中的错误,还是我错过了什么?
在表面上,tokio::run
期望Future
与Error
类型()
相关联,但Future
的实际Receiver
impl与Error
类型RecvError
相关联。
但是,Rust的类型推断在两个方向上都有效,并且有时可以反过来看待预期和实际类型。通常,消息的措辞符合您的期望,但有些情况会让您感到倒退。当然,要弄清楚发生了什么并且知道类型不匹配发生在哪里,即使没有以最佳方式报告也不是很困难。
在一般情况下,对人类解释哪种类型为“实际”以及哪种“预期”可能不是一个容易解决的问题,但我同意这个错误信息在您提供的代码中令人困惑。
我找不到这方面的问题,但我相信我之前已经看过几次。如果之前已经报告过,那么再次报告也不会有太大的危害,所以我会这样做。