duplex/duplex.rs
1#![allow(clippy::module_name_repetitions)]
2
3/*
4#[cfg(all(feature = "char-device", feature = "futures-io"))]
5use char_device::AsyncStdCharDevice;
6*/
7#[cfg(feature = "char-device")]
8use char_device::CharDevice;
9/*
10#[cfg(all(feature = "char-device", feature = "tokio"))]
11use char_device::TokioCharDevice;
12*/
13#[cfg(feature = "futures-io")]
14use futures_io::{AsyncRead, AsyncWrite};
15#[cfg(all(feature = "socketpair", features = "futures-io"))]
16use socketpair::AsyncStdSocketpairStream;
17#[cfg(feature = "socketpair")]
18use socketpair::SocketpairStream;
19/*
20#[cfg(all(feature = "socketpair", features = "tokio"))]
21use socketpair::TokioSocketpairStream;
22*/
23use std::io::{Read, Write};
24use std::net::TcpStream;
25
26/// A trait which indicates a type represents a duplex communication channel,
27/// meaning it's bidirectional and may be used in an interactive manner.
28///
29/// For example, [`TcpStream`] is `Duplex`, however [`File`] is not, because
30/// even though `File` is readable and writable, normal files do not have
31/// independent input and output channels; they share a current-position
32/// pointer. [`CharDevice`] is a special kind of file which is `Duplex`.
33///
34/// Types should implment this, and implementations of [`HalfDuplex`] and
35/// `FullDuplex` (enabled with the "futures-io" cargo feature) will be
36/// provided by blanket implementations.
37///
38/// [`File`]: std::fs::File
39/// [`CharDevice`]: https://docs.rs/char-device/latest/char_device/struct.CharDevice.html
40pub trait Duplex {}
41
42/// A combination of [`std::io::Read`] and [`std::io::Write`] intended for use
43/// in interactive I/O (as opposed to normal file I/O).
44///
45/// [`std::io::Read`]: https://doc.rust-lang.org/std/io/trait.Read.html
46/// [`std::io::Write`]: https://doc.rust-lang.org/std/io/trait.Write.html
47pub trait HalfDuplex: Duplex + Read + Write {}
48
49/// A combination of [`AsyncRead`] and [`AsyncWrite`] intended for use in
50/// interactive I/O (as opposed to normal file I/O).
51///
52/// Note that this only guarantees that the stream handle itself is
53/// full-duplex, and not necessarily the stream transport or the endpoint the
54/// stream is attached to. For example, `TcpStream` implements `FullDuplex` but
55/// may be connected to a server which is unable to send and receive data at
56/// the same time.
57///
58/// [`futures_io::AsyncRead`]: https://docs.rs/futures-io/latest/futures_io/trait.AsyncRead.html
59/// [`futures_io::AsyncWrite`]: https://docs.rs/futures-io/latest/futures_io/trait.AsynWrite.html
60#[cfg(feature = "futures-io")]
61pub trait FullDuplex: Duplex + AsyncRead + AsyncWrite {}
62
63/// A combination of [`tokio::io::AsyncRead`] and [`tokio::io::AsyncWrite`]
64/// intended for use in interactive I/O (as opposed to normal file I/O).
65///
66/// This is the same as `FullDuplex` except using tokio's `AsyncRead` and
67/// `AsyncWrite` traits in place of futures-io's.
68#[cfg(feature = "tokio")]
69pub trait TokioFullDuplex: Duplex + tokio::io::AsyncRead + tokio::io::AsyncWrite {}
70
71// Blanket implemenmtations for types that implement `Duplex`.
72
73impl<T: Duplex + Read + Write> HalfDuplex for T {}
74
75#[cfg(feature = "futures-io")]
76impl<T: Duplex + AsyncRead + AsyncWrite> FullDuplex for T {}
77
78#[cfg(feature = "tokio")]
79impl<T: Duplex + tokio::io::AsyncRead + tokio::io::AsyncWrite> TokioFullDuplex for T {}
80
81// Implementations for various types.
82
83impl Duplex for TcpStream {}
84
85#[cfg(feature = "socket2")]
86impl Duplex for socket2::Socket {}
87
88#[cfg(unix)]
89impl Duplex for std::os::unix::net::UnixStream {}
90
91#[cfg(feature = "char-device")]
92impl Duplex for CharDevice {}
93
94/*
95#[cfg(all(feature = "char-device", feature = "futures-io"))]
96impl Duplex for AsyncStdCharDevice {}
97*/
98
99/*
100#[cfg(all(feature = "char-device", feature = "tokio"))]
101impl Duplex for TokioCharDevice {}
102*/
103
104#[cfg(feature = "socketpair")]
105impl Duplex for SocketpairStream {}
106
107#[cfg(all(feature = "socketpair", features = "futures-io"))]
108impl Duplex for AsyncStdSocketpairStream {}
109
110/*
111#[cfg(all(feature = "socketpair", features = "tokio"))]
112impl Duplex for TokioSocketpairStream {}
113*/
114
115#[cfg(feature = "ssh2")]
116impl Duplex for ssh2::Stream {}
117
118#[cfg(feature = "ssh2")]
119impl Duplex for ssh2::Channel {}
120
121#[cfg(all(unix, feature = "serialport"))]
122impl Duplex for serialport::TTYPort {}
123
124#[cfg(feature = "readwrite")]
125impl<R: Read, W: Write> Duplex for readwrite::ReadWrite<R, W> {}
126
127#[cfg(feature = "duplexify")]
128impl<R: Read, W: Write> Duplex for duplexify::Duplex<R, W> {}