Tokio: Framed::send method not found

Created on 1 Apr 2020  ยท  2Comments  ยท  Source: tokio-rs/tokio

Version

โ”œโ”€โ”€ tokio v0.2.13
โ”‚   โ””โ”€โ”€ tokio-macros v0.2.5
โ””โ”€โ”€ tokio-util v0.3.1
    โ””โ”€โ”€ tokio v0.2.13 (*)

Platform

Linux 9b022d4f6aac 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64 GNU/Linux

Subcrates

tokio-util

Description

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

C-question

Most helpful comment

You need to import the SinkExt trait.

use futures::sink::SinkExt;

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings