首页 > 解决方案 > tokio::net::TcpStream 是如何实现 tokio::prelude::Stream 的?

问题描述

tokio.rs文档中,我们看到以下片段

// 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,我无法弄清楚此特征如何适用,反之亦然。TcpStreamTcpStream

标签: rusttraitsrust-tokio

解决方案


tokio::net::TcpStream实现AsyncRead

提供的方法之一AsyncReadsplit()

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

因此,在这种情况下,它不像Stream::split您的问题所建议的那样,因为根据您的观察tokio::net::TcpStream,它不是Stream.


推荐阅读