1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::fmt::Arguments;
use std::io::{self, Read, Write};
pub trait Stream: Read + Write {}
impl<S> Stream for S where S: Read + Write {}
pub struct ReadWritePair<R, W>(pub R, pub W)
where
R: Read,
W: Write;
impl<R, W> Read for ReadWritePair<R, W>
where
R: Read,
W: Write,
{
#[inline(always)]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
#[inline(always)]
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
self.0.read_to_end(buf)
}
#[inline(always)]
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
self.0.read_to_string(buf)
}
#[inline(always)]
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
self.0.read_exact(buf)
}
}
impl<R, W> Write for ReadWritePair<R, W>
where
R: Read,
W: Write,
{
#[inline(always)]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.1.write(buf)
}
#[inline(always)]
fn flush(&mut self) -> io::Result<()> {
self.1.flush()
}
#[inline(always)]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.1.write_all(buf)
}
#[inline(always)]
fn write_fmt(&mut self, fmt: Arguments) -> io::Result<()> {
self.1.write_fmt(fmt)
}
}
#[cfg(feature = "async")]
pub mod async {
pub use super::ReadWritePair;
use futures::Poll;
use std::io::{self, Read, Write};
pub use tokio_io::io::{ReadHalf, WriteHalf};
pub use tokio_io::{AsyncRead, AsyncWrite};
pub use tokio_tcp::TcpStream;
pub trait Stream: AsyncRead + AsyncWrite {}
impl<S> Stream for S where S: AsyncRead + AsyncWrite {}
impl<R, W> AsyncRead for ReadWritePair<R, W>
where
R: AsyncRead,
W: Write,
{
}
impl<R, W> AsyncWrite for ReadWritePair<R, W>
where
W: AsyncWrite,
R: Read,
{
fn shutdown(&mut self) -> Poll<(), io::Error> {
self.1.shutdown()
}
}
}
#[cfg(feature = "sync")]
pub mod sync {
pub use super::ReadWritePair;
#[cfg(feature = "sync-ssl")]
pub use native_tls::TlsStream;
use std::io::{self, Read, Write};
pub use std::net::Shutdown;
pub use std::net::TcpStream;
use std::ops::Deref;
pub use super::Stream;
pub trait NetworkStream: Read + Write + AsTcpStream {}
impl<S> NetworkStream for S where S: Read + Write + AsTcpStream {}
pub trait Splittable {
type Reader: Read;
type Writer: Write;
fn split(self) -> io::Result<(Self::Reader, Self::Writer)>;
}
impl<R, W> Splittable for ReadWritePair<R, W>
where
R: Read,
W: Write,
{
type Reader = R;
type Writer = W;
fn split(self) -> io::Result<(R, W)> {
Ok((self.0, self.1))
}
}
impl Splittable for TcpStream {
type Reader = TcpStream;
type Writer = TcpStream;
fn split(self) -> io::Result<(TcpStream, TcpStream)> {
self.try_clone().map(|s| (s, self))
}
}
pub trait AsTcpStream {
fn as_tcp(&self) -> &TcpStream;
}
impl AsTcpStream for TcpStream {
fn as_tcp(&self) -> &TcpStream {
self
}
}
#[cfg(feature = "sync-ssl")]
impl AsTcpStream for TlsStream<TcpStream> {
fn as_tcp(&self) -> &TcpStream {
self.get_ref()
}
}
impl<T> AsTcpStream for Box<T>
where
T: AsTcpStream + ?Sized,
{
fn as_tcp(&self) -> &TcpStream {
self.deref().as_tcp()
}
}
}