4Manuals

  • PDF Cloud HOME

受约束的特征`tokio :: net :: tcp :: stream :: TcpStream:tokio_io :: async_read :: AsyncRead`不满意 Download

    从文件初始化类的最佳方法(异步)? 货物建造未在wasm项目上生成pkg目录 此声明在dart中的Future类中意味着什么:FutureOr <R>函数(动态)onValue 如何为Rustdoc上下文解释“在此范围内找不到派生宏`Cacheable`” 如何在Dart中顺序执行异步功能 如何调用回调函数? 当我尝试运行已编译的python代码时出现错误 C2440编译器错误仅适用于新项目,而不适用于现有项目 我需要在异步函数中的每个语句之前等待吗? 最佳实践是,API调用应在React的什么地方进行?

我无法编译一个简单的应用程序来测试tokio编解码器。 tokio :: net :: tcp :: stream :: TcpStream实现AsyncRead和-Write。 但是,当我尝试编译以下代码时,出现以下错误。 我仍然对Rust和Tokio还是陌生的,所以毫无疑问我错过了一些明显的东西(我希望如此)...

main.rs:

use tokio::net::TcpListener;
use tokio::prelude::*;
use tokio_codec::{ Framed, LinesCodec };


#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let mut listener = TcpListener::bind("127.0.0.1:12321").await?;

  loop {
    let (socket, _addr) = listener.accept().await?;

    tokio::spawn(async move {
      let (_sink, mut stream) = Framed::new(socket, LinesCodec::new()).split();

      while let Some(Ok(line)) = stream.next().await {
        println!("{:?}", line);
      }
    });
  }
}

Cargo.toml:

[dependencies]
tokio = { version = "0.2.6", features = ["full"] }
tokio-codec = "0.1.1"

输出:

error[E0277]: the trait bound `tokio::net::tcp::stream::TcpStream: tokio_io::async_read::AsyncRead` is not satisfied
  --> src\main.rs:14:36
   |
14 |       let (mut sink, mut stream) = Framed::new(socket, LinesCodec::new()).split();
   |                                    ^^^^^^^^^^^ the trait `tokio_io::async_read::AsyncRead` is not implemented for `tokio::net::tcp::stream::TcpStream`
   |
   = note: required by `tokio_io::_tokio_codec::framed::Framed::<T, U>::new`

error[E0277]: the trait bound `tokio::net::tcp::stream::TcpStream: tokio_io::async_write::AsyncWrite` is not satisfied
  --> src\main.rs:14:36
   |
14 |       let (mut sink, mut stream) = Framed::new(socket, LinesCodec::new()).split();
   |                                    ^^^^^^^^^^^ the trait `tokio_io::async_write::AsyncWrite` is not implemented for `tokio::net::tcp::stream::TcpStream`
   |
   = note: required by `tokio_io::_tokio_codec::framed::Framed::<T, U>::new`

error[E0599]: no method named `split` found for type `tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec>` in the current scope
  --> src\main.rs:14:75
   |
14 |       let (mut sink, mut stream) = Framed::new(socket, LinesCodec::new()).split();
   |                                                                           ^^^^^ method not found in `tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec>`
   |
   = note: the method `split` exists but the following trait bounds were not satisfied:
           `&mut tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec> : tokio::io::util::async_buf_read_ext::AsyncBufReadExt`
           `&tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec> : tokio::io::util::async_buf_read_ext::AsyncBufReadExt`
           `tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec> : tokio::io::util::async_buf_read_ext::AsyncBufReadExt`

我该如何解决?

1 个答案:

答案 0 :(得分:2)

tokio-codec是一个过时的板条箱,具体取决于Tokio(0.1.7)的预异步/等待版本

取决于Tokio 0.2的编解码器seem to have been moved到tokio-util,因此您应该对此感到更幸运。

通常,当编译器告诉您类型不能实现特征时,但是在文档中看到它确实可以实现特征,这意味着您有两个不同版本的板条箱定义了项目中的特征(Tokio 0.1和0.2)。这种情况)。



Similar searches
    如何通过Heroku获取DNS服务器的Type A 以分布式模式启动OrientDB时,仅OrientDB Enterprise Edition支持数据中心配置 有没有最好的方法来检查用户是否已登录,然后在单个页面应用程序中显示相关内容? 如何将一个查询的输出提供给下一个查询 ItemDecoration-不确定为什么未绘制最后的分隔线