tokio::net::TcpStream 如何实现 tokio::prelude::Stream? [英] How does tokio::net::TcpStream implement tokio::prelude::Stream?
问题描述
在 tokio.rs 文档中,我们看到以下片段
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);
我假设 split
确实是 Stream::split
,但我无法弄清楚这个特性如何应用于 TcpStream
鉴于流页面没有提到 TcpStream
和副反之.
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.
推荐答案
tokio::net::TcpStream
实现了 AsyncRead
.
AsyncRead
提供的方法之一是 split()
:
One of the provided methods from AsyncRead
is split()
:
fn split(self) -> (ReadHalf<Self>, WriteHalf<Self>)
where
Self: AsyncWrite,
所以在这种情况下,它不是 Stream::split
作为您的问题所建议的,因为根据您的观察 tokio::net::TcpStream
不是实现者Stream
.
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
.
这篇关于tokio::net::TcpStream 如何实现 tokio::prelude::Stream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!