预期的XYZ已找到() [英] Expected XYZ found ()

查看:80
本文介绍了预期的XYZ已找到()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

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 ,但编译器却显示了相反的内容.

The code requires (), got RecvError instead, but the compiler print the opposite.

这是编译器中的错误,还是我错过了什么?

Is this a bug in the compiler, or have I missed something?

推荐答案

在表面上, tokio :: run 期望一个 Future 关联的 Error 类型为(),但是 Receiver actual Future impl已关联 Error 类型 RecvError .

On the surface, tokio::run expects a Future with associated Error type (), but the actual Future impl of Receiver has associated Error type RecvError.

但是,Rust的类型推断在两个方向上都可以起作用,有时可以从相反的方向看到预期的类型和实际的类型.通常消息的措词符合您的期望,但是在这种情况下,这种感觉会倒退.当然,即使没有以最佳方式报告,也不难弄清正在发生的事情并知道发生类型不匹配的地方.

However, Rust's type inference works in both directions, and the expected and actual types can sometimes be seen the other way around. Usually the wording of the message fits your expectations, but there are situations like this one where it feels backwards. Of course, it isn't too difficult to work out what is going on and know where the type mis-match is happening even if it isn't reported in the best way.

整理人类对哪种类型的解释是实际" 和哪种期望的" 可能不是一个容易解决的问题,但我同意该错误消息使您提供的代码令人困惑.

Codifying the human interpretation of which type is "actual" and which is "expected" might not be an easy problem to solve in the general case, but I agree that this error message is confusing in the code you provided.

我找不到与此相关的问题,但是我敢肯定,我之前已经看过几次.如果以前已经报告过,那么再次报告不会有太大危害,所以我就这样做.

I couldn't find an issue about this, but I'm sure I've seen this talked about a few times before. If it's been reported before, it won't do much harm to report it again, so I would just do that.

这篇关于预期的XYZ已找到()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆