Gus
Gus

Reputation: 171

How do I use TcpStream split across 2 threads with async?

I am trying to use the read and write of a tcp stream in different threads. This is what I currently have:

use tokio::prelude::*;
use tokio::net::TcpStream;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut stream = TcpStream::connect("localhost:8080").await?;
    let (mut read, mut write) = stream.split();

    tokio::spawn(async move {
        loop {
            let mut buf = [0u8; 32];
            read.read(&mut buf).await.unwrap();
            println!("{}", std::str::from_utf8(&buf));
        }
    });

    Ok(())
}

Im going to use another thread for the write. My problem is that I get the error that 'stream' is dropped while still borrowed.

Upvotes: 6

Views: 5841

Answers (2)

Albert Bos
Albert Bos

Reputation: 2062

Reading https://users.rust-lang.org/t/why-i-have-to-use-tokio-tcpstream-split-for-concurrent-read-writes/47755/3 And it suggests to use into_split it is more efficient.

tokio::io::split uses a Mutex which into_split apparently does not have (specific to TcpStream)

Upvotes: 0

Kitsu
Kitsu

Reputation: 3435

That happens due to the method signature of Tokio::split, as you can see it takes &mut self, so its part cannot be used in a tokio::spawn future argument due to the 'static bound. So, this is exactly what the error says.

What you are searching is tokio::io::split. Playground

use tokio::prelude::*;
use tokio::net::TcpStream;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut stream = TcpStream::connect("localhost:8080").await?;
    let (mut read, mut write) = tokio::io::split(stream);

    tokio::spawn(async move {
        loop {
            let mut buf = [0u8; 32];
            read.read(&mut buf).await.unwrap();
            println!("{:?}", std::str::from_utf8(&buf));
        }
    });

    Ok(())
}

Upvotes: 7

Related Questions