不满足特征绑定`tokio::net::tcp::stream::TcpStream: tokio_io::async_read::AsyncRead`
the trait bound `tokio::net::tcp::stream::TcpStream: tokio_io::async_read::AsyncRead` is not satisfied
我无法编译一个简单的应用程序来测试 tokio-codec。
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`
我该如何解决这个问题?
tokio-codec
是一个过时的 crate,它依赖于 pre-async/await 版本的 Tokio (0.1.7)
依赖于 Tokio 0.2 的编解码器 seem to have been moved to tokio-util,所以你应该更幸运。
通常,当编译器告诉你一个类型没有实现一个特征,但在文档中你看到它实现了,这意味着你有两个不同版本的包装箱来定义你的项目中的特征(Tokio 0.1 和在这种情况下为 0.2)。
我无法编译一个简单的应用程序来测试 tokio-codec。 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`
我该如何解决这个问题?
tokio-codec
是一个过时的 crate,它依赖于 pre-async/await 版本的 Tokio (0.1.7)
依赖于 Tokio 0.2 的编解码器 seem to have been moved to tokio-util,所以你应该更幸运。
通常,当编译器告诉你一个类型没有实现一个特征,但在文档中你看到它实现了,这意味着你有两个不同版本的包装箱来定义你的项目中的特征(Tokio 0.1 和在这种情况下为 0.2)。