io: bring back split utility (#1521)

Bring back `split` utility as a free fn instead of a method on
`AsyncRead`. This utility wraps the `stream` in an `Arc` and uses mutual
exclusion to ensure correct access.

Additionally, the specialized `split_mut` fn on TcpStream and UdsStream
is promoted to `split`.
This commit is contained in:
Carl Lerche
2019-08-30 20:46:07 -07:00
committed by GitHub
parent 951827229a
commit 2f91c85ad8
14 changed files with 284 additions and 325 deletions
+2 -1
View File
@@ -93,7 +93,8 @@ mod tcp {
stdin: impl Stream<Item = Result<Vec<u8>, io::Error>> + Unpin,
mut stdout: impl Sink<Vec<u8>, Error = io::Error> + Unpin,
) -> Result<(), Box<dyn Error>> {
let (r, w) = TcpStream::connect(addr).await?.split();
let mut stream = TcpStream::connect(addr).await?;
let (r, w) = stream.split();
let sink = FramedWrite::new(w, codec::Bytes);
let mut stream = FramedRead::new(r, codec::Bytes).filter_map(|i| match i {
Ok(i) => future::ready(Some(i)),
+2 -2
View File
@@ -52,8 +52,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
Ok(())
}
async fn transfer(inbound: TcpStream, proxy_addr: String) -> Result<(), Box<dyn Error>> {
let outbound = TcpStream::connect(proxy_addr).await?;
async fn transfer(mut inbound: TcpStream, proxy_addr: String) -> Result<(), Box<dyn Error>> {
let mut outbound = TcpStream::connect(proxy_addr).await?;
let (mut ri, mut wi) = inbound.split();
let (mut ro, mut wo) = outbound.split();
+1
View File
@@ -39,6 +39,7 @@
// standard input, output, and error
#[cfg(feature = "fs")]
pub use tokio_fs::{stderr, stdin, stdout, Stderr, Stdin, Stdout};
pub use tokio_io::split::split;
pub use tokio_io::{
AsyncBufRead, AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader,
BufWriter,