mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-06 00:00:10 +02:00
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:
@@ -1,25 +1 @@
|
||||
use tokio_net::tcp::{TcpListener, TcpStream};
|
||||
|
||||
#[tokio::test]
|
||||
async fn split_reunite() -> std::io::Result<()> {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await?;
|
||||
let addr = listener.local_addr()?;
|
||||
let stream = TcpStream::connect(&addr).await?;
|
||||
|
||||
let (r, w) = stream.split();
|
||||
assert!(r.reunite(w).is_ok());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn split_reunite_error() -> std::io::Result<()> {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await?;
|
||||
let addr = listener.local_addr()?;
|
||||
let stream = TcpStream::connect(&addr).await?;
|
||||
let stream1 = TcpStream::connect(&addr).await?;
|
||||
|
||||
let (r, _) = stream.split();
|
||||
let (_, w) = stream1.split();
|
||||
assert!(r.reunite(w).is_err());
|
||||
Ok(())
|
||||
}
|
||||
// TODO: write tests using TcpStream::split()
|
||||
|
||||
Reference in New Issue
Block a user