4

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
benjumanji
  • 1,395
  • 7
  • 15
  • What do you mean by "how"? Almost all traits are implemented for a type in the same way: `impl Trait for Type { ... }`. Sometimes that code is generated by a macro or whatever, but it's ultimately the same. – Shepmaster May 28 '19 at 20:10
  • [The documentation shows](https://docs.rs/tokio/0.1.20/tokio/net/struct.TcpStream.html#impl-AsyncRead) what traits are implemented for a type and what methods they provide. – Shepmaster May 28 '19 at 20:12
  • You can test if a type implements a trait: [How to enforce that a type implements a trait at compile time?](https://stackoverflow.com/q/32764797/155423) – Shepmaster May 28 '19 at 20:15
  • Perhaps my edits will help you understand what I am asking. I know how traits are defined. My question is that given that there isn't an obvious like between the two how do I establish that link? Is it some chain of blanket implementations? I have no idea. – benjumanji May 28 '19 at 21:43
  • When I linked to [the documentation for `TcpStream`](https://docs.rs/tokio/0.1.20/tokio/net/struct.TcpStream.html#impl-AsyncRead), did you search on the page for the method `split`? – Shepmaster May 29 '19 at 11:52
  • Well color me embarrassed. I swear I spent an age reading that + ctrl-f split. Shame! – benjumanji May 29 '19 at 12:10

1 Answers1

3

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.

Matt Harrison
  • 13,381
  • 6
  • 48
  • 66
  • It would probably be good if you explicitly laid out the exact steps you took to figure this so OP can apply the logic for other tasks. – Shepmaster May 29 '19 at 11:51