Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does tokio::net::TcpStream implement tokio::prelude::Stream?

In the tokio.rs docs we see the following snippet

// split the socket stream into readable and writable parts
let (reader, writer) = socket.split();
// copy bytes from the reader into the writer
let amount = io::copy(reader, writer);

I am assuming that split is indeed Stream::split, but I can't figure out how this trait applies to TcpStream given that the stream page doesn't mention TcpStream and vice versa.

like image 424
benjumanji Avatar asked Sep 13 '25 05:09

benjumanji


1 Answers

tokio::net::TcpStream implements AsyncRead.

One of the provided methods from AsyncRead is split():

fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)
where
    Self: AsyncWrite, 

So in this case it isn't Stream::split as your question suggested because as per your observation tokio::net::TcpStream isn't an implementor of Stream.

like image 116
Matt Harrison Avatar answered Sep 15 '25 21:09

Matt Harrison