16 breaking releases

0.16.0 Mar 3, 2023
0.15.0 Nov 27, 2022
0.14.0 Nov 8, 2022
0.13.0 Jun 16, 2022
0.3.1 Mar 4, 2021

#2227 in Rust patterns

Download history 18/week @ 2023-12-17 38/week @ 2023-12-24 89/week @ 2024-01-07 35/week @ 2024-01-14 18/week @ 2024-01-21 17/week @ 2024-01-28 24/week @ 2024-02-04 15/week @ 2024-02-11 137/week @ 2024-02-18 79/week @ 2024-02-25 44/week @ 2024-03-03 63/week @ 2024-03-10 40/week @ 2024-03-17 91/week @ 2024-03-24 487/week @ 2024-03-31

689 downloads per month
Used in 8 crates (7 directly)

Apache-2.0…

15KB
51 lines

duplex

The Duplex trait: interactive streams

Github Actions CI Status crates.io page docs.rs docs

This crate defines the Duplex trait, for types that have logically independent input and output channels.

The Read and Write traits take their streams by &mut self and block, so they cannot be used on the same stream simultaneously. This crate provides and implements the HalfDuplex trait for any type which implements Duplex, Read, and Write.

The AsyncRead and AsyncWrite traits take their streams by &mut self but do not block, so they can be used on the same stream simultaneously, at least when they're connected to an endpoint which supports it. When the "futures-io" feature is enabled, this crate provides and implements the FullDuplex trait for any type which implements Duplex, AsyncRead, and AsyncWrite.

Tokio uses its own AsyncRead, and AsyncWrite. When the "tokio" feature is enabled, this crate also provides and implements TokioFullDuplex for any type which implements Duplex, tokio::io::AsyncRead, and tokio::io::AsyncWrite.

Normal Files are not duplex devices, because even though they support input and output, the input and output are not logically independent since they share a current-position pointer in the OS. Character devices are often unified with files in OS APIs, however they may represent duplex devices. So while File does not implement Duplex, CharDevice does.

The following are some notable types for which Duplex is implemented:

Type cfg Notes
std::net::TcpStream
io_streams::StreamDuplexer
nameless::DuplexByteStream
nameless::DuplexTextStream
char_device::CharDevice feature = char-device
socketpair::SocketpairStream feature = socketpair
ssh2::Stream feature = ssh2
ssh2::Channel feature = ssh2
serialport::TTYPort all(unix, feature = serialport) serialport dependencies
readwrite::ReadWrite feature = readwrite
duplexify::Duplexify feature = duplexify
socket2::Socket feature = socket2

Support for async-std and tokio in char-device and socketpair is temporarily disabled until those crates contain the needed implementations of the I/O safety traits.

Dependencies

~0–13MB
~144K SLoC