โโโ tokio v0.2.13
โ โโโ tokio-macros v0.2.5
โโโ tokio-util v0.3.1
โโโ tokio v0.2.13 (*)
Linux 9b022d4f6aac 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64 GNU/Linux
tokio-util
I am trying to implement a Codec but get the following error message
error[E0599]: no method named `send` found for struct `tokio_util::codec::framed::Framed<tokio::net::tcp::stream::TcpStream, Codec>` in the current scope
when running this application:
use tokio::net::TcpListener;
use tokio_util::codec::Framed;
use bytes::BytesMut;
use tokio_util::codec::{Decoder, Encoder};
#[derive(Default)]
pub struct Codec;
impl<P> Encoder<P> for Codec
{
type Error = std::io::Error;
fn encode(&mut self, item: P, dst: &mut BytesMut) -> Result<(), Self::Error> {
Ok(())
}
}
impl Decoder for Codec {
type Item = u8;
type Error = std::io::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
Ok(Some(0))
}
}
#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut listener = TcpListener::bind("127.0.0.1:1234").await?;
loop {
let (stream, _) = listener.accept().await?;
tokio::spawn(async {
let mut framed = Framed::new(stream, Codec::default());
framed.send(0);
});
}
}
Link to Rust Playground
You need to import the SinkExt trait.
use futures::sink::SinkExt;
Wouldn't it make sense to make the SinkExt trait be available through tokio similar to tokio::stream::StreamExt?
Now I have to declare futures extra crate in cargo.toml to use this.
Most helpful comment
You need to import the
SinkExttrait.