expression
Owned halves and reuniting
Transfer the two directions into owned handles. Reunite only halves from the same original stream; mismatched halves fail. Ownership does not remove the need for task lifecycle management. Tokio is an external crate. Authorized isolated IPv4 loopback fixture; no public network or DNS. Expected stdout (not executed): true Common mistake: Reuniting halves from unrelated sockets.
Formal Syntax & Structure
rust syntaxrust
stream.into_split(); read_half.reunite(write_half)Working Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { tokio::time::timeout(std::time::Duration::from_secs(3), async { use tokio::net::{TcpListener, TcpStream}; let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let addr = listener.local_addr().unwrap(); let (client, accepted) = tokio::join!(TcpStream::connect(addr), listener.accept()); let mut client = client.unwrap(); let (mut server, _) = accepted.unwrap(); let (rd, wr) = client.into_split(); let restored = rd.reunite(wr).unwrap(); println!("{}", restored.peer_addr().unwrap() == server.local_addr().unwrap()); }).await.expect("fixture deadline");}