Skip to main content

duplex

Function duplex 

Source
pub fn duplex(capacity: usize) -> (Duplex, Duplex)
Expand description

Creates two connected streams with capacity bytes of buffering per direction.

Reads wait for data and writes wait for buffer space, bounded by the deadlines installed by Wire. Partial progress never refreshes a deadline. A timeout leaves the connection reusable and preserves any bytes already accepted. Flush checks its deadline but does not wait for the peer to consume output. Reads deliver available bytes, EOF and empty reads even after their deadline; the deadline only limits waiting for input. Writes and flushes refuse expired deadlines even if buffer space is available. Wire enforces its own deadlines before calling either half.

Closing or dropping an endpoint wakes blocked I/O on both sides. Its unread input is discarded; the peer can drain its accepted output before receiving EOF. Further nonempty writes fail with io::ErrorKind::BrokenPipe. A flush fails the same way only if the peer closed with accepted output still unread. Output the peer had consumed before closing flushes fine afterwards.

Both peers must run concurrently when exchanging data. Allow enough capacity for the handshake’s initial output; 64 * 1024 is a useful starting point. Small buffers can cause handshake backpressure, just as a real stream can.

§Panics

Panics if capacity is zero.