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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#![deny(missing_docs)]
#![forbid(unsafe_code)]

//! Given two things, one of which implements `std::io::Read` and other implements `std::io::Write`, make a single socket-like object which implmenets `Read + Write`. Note that you can't write to it while waiting for data to come from read part.
//!
//! There is also AsyncRead / AsyncWrite analogue, see `ReadWriteAsync` struct.

use std::io::{Read, Result, Write};

/// Combined reader and writer
pub struct ReadWrite<R: Read, W: Write>(pub R, pub W);

impl<R: Read, W: Write> From<(R, W)> for ReadWrite<R, W> {
    fn from((r, w): (R, W)) -> Self {
        ReadWrite(r, w)
    }
}
impl<R: Read, W: Write> ReadWrite<R, W> {
    /// Bundle separate reader and writer into a combined pseudo-socket
    pub fn new(r: R, w: W) -> Self {
        ReadWrite(r, w)
    }
    /// Borrow inner objects
    pub fn borrow(&self) -> (&R, &W) {
        (&self.0, &self.1)
    }
    /// Borrow the reader
    pub fn borrow_read(&self) -> &R {
        &self.0
    }
    /// Borrow the writer
    pub fn borrow_write(&self) -> &W {
        &self.1
    }
    /// Mutably borrow inner objects
    pub fn borrow_mut(&mut self) -> (&mut R, &mut W) {
        (&mut self.0, &mut self.1)
    }
    /// Mutably borrow the reader
    pub fn borrow_mut_read(&mut self) -> &mut R {
        &mut self.0
    }
    /// Mutably borrow the writer
    pub fn borrow_mut_write(&mut self) -> &mut W {
        &mut self.1
    }
    /// Convert ReadWrite back into individual reader and writer pair
    pub fn into_inner(self) -> (R, W) {
        (self.0, self.1)
    }
    /// Convert ReadWrite back into the reader, dropping the writer
    pub fn into_reader(self) -> R {
        self.0
    }
    /// Convert ReadWrite back into the writer, dropping the reader
    pub fn into_writer(self) -> W {
        self.1
    }
}

impl<R: Read, W: Write> Read for ReadWrite<R, W> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        self.0.read(buf)
    }

    fn read_vectored(&mut self, bufs: &mut [std::io::IoSliceMut<'_>]) -> Result<usize> {
        self.0.read_vectored(bufs)
    }
}
impl<R: Read, W: Write> Write for ReadWrite<R, W> {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        self.1.write(buf)
    }
    fn flush(&mut self) -> Result<()> {
        self.1.flush()
    }

    fn write_vectored(&mut self, bufs: &[std::io::IoSlice<'_>]) -> Result<usize> {
        self.1.write_vectored(bufs)
    }
}

#[cfg(all(feature = "tokio"))]
mod tokio {
    use tokio_dep::io::{AsyncRead, AsyncWrite};

    use std::pin::Pin;

    pin_project_lite::pin_project! {
        /// Combined async reader and writer, `tokio 1` version.
        /// Note that this struct is only present in `readwrite` if "tokio" Cargo feature is enabled.
        pub struct ReadWriteTokio<R, W> {
            #[pin]
            r: R,
            #[pin]
            w: W,
        }
    }

    impl<R: AsyncRead, W: AsyncWrite> From<(R, W)> for ReadWriteTokio<R, W> {
        fn from((r, w): (R, W)) -> Self {
            ReadWriteTokio { r, w }
        }
    }
    impl<R: AsyncRead, W: AsyncWrite> ReadWriteTokio<R, W> {
        /// Bundle separate async reader and writer into a combined pseudo-socket
        pub fn new(r: R, w: W) -> Self {
            ReadWriteTokio { r, w }
        }
        /// Borrow inner objects
        pub fn borrow(&self) -> (&R, &W) {
            (&self.r, &self.w)
        }
        /// Borrow the reader
        pub fn borrow_read(&self) -> &R {
            &self.r
        }
        /// Borrow the writer
        pub fn borrow_write(&self) -> &W {
            &self.w
        }
        /// Mutably borrow inner objects
        pub fn borrow_mut(&mut self) -> (&mut R, &mut W) {
            (&mut self.r, &mut self.w)
        }
        /// Mutably borrow the reader
        pub fn borrow_mut_read(&mut self) -> &mut R {
            &mut self.r
        }
        /// Mutably borrow the writer
        pub fn borrow_mut_write(&mut self) -> &mut W {
            &mut self.w
        }
        /// Convert ReadWrite back into individual reader and writer pair
        pub fn into_inner(self) -> (R, W) {
            (self.r, self.w)
        }
        /// Convert ReadWrite back into the reader, dropping the writer
        pub fn into_reader(self) -> R {
            self.r
        }
        /// Convert ReadWrite back into the writer, dropping the reader
        pub fn into_writer(self) -> W {
            self.w
        }

        /// Borrow pinned reader and writer
        pub fn borrow_pin(self: Pin<&mut Self>) -> (Pin<&mut R>, Pin<&mut W>) {
            let p = self.project();
            (p.r, p.w)
        }
        /// Borrow pinned reader
        pub fn borrow_pin_read(self: Pin<&mut Self>) -> Pin<&mut R> {
            self.project().r
        }
        /// Borrow pinned writer
        pub fn borrow_pin_write(self: Pin<&mut Self>) -> Pin<&mut W> {
            self.project().w
        }
    }

    impl<R: AsyncRead, W> AsyncRead for ReadWriteTokio<R, W> {
        fn poll_read(
            self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &mut tokio_dep::io::ReadBuf<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            AsyncRead::poll_read(self.project().r, cx, buf)
        }
    }

    impl<R, W: AsyncWrite> AsyncWrite for ReadWriteTokio<R, W> {
        fn poll_write(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &[u8],
        ) -> std::task::Poll<Result<usize, std::io::Error>> {
            self.project().w.poll_write(cx, buf)
        }

        fn poll_flush(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Result<(), std::io::Error>> {
            self.project().w.poll_flush(cx)
        }

        fn poll_shutdown(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Result<(), std::io::Error>> {
            self.project().w.poll_shutdown(cx)
        }

        fn poll_write_vectored(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            bufs: &[std::io::IoSlice<'_>],
        ) -> std::task::Poll<Result<usize, std::io::Error>> {
            self.project().w.poll_write_vectored(cx, bufs)
        }

        fn is_write_vectored(&self) -> bool {
            self.w.is_write_vectored()
        }
    }
}
#[cfg(all(feature = "tokio"))]
pub use tokio::ReadWriteTokio;

#[cfg(all(feature = "asyncstd"))]
mod asyncstd {
    use futures::io::{AsyncRead, AsyncWrite};

    use std::pin::Pin;

    pin_project_lite::pin_project! {
        /// Combined async reader and writer, `futures 0.3` version.
        /// Note that this struct is only present in `readwrite` if "asyncstd" Cargo feature is enabled.
        pub struct ReadWriteAsyncstd<R, W> {
            #[pin]
            r: R,
            #[pin]
            w: W,
        }
    }

    impl<R: AsyncRead, W: AsyncWrite> From<(R, W)> for ReadWriteAsyncstd<R, W> {
        fn from((r, w): (R, W)) -> Self {
            ReadWriteAsyncstd { r, w }
        }
    }
    impl<R: AsyncRead, W: AsyncWrite> ReadWriteAsyncstd<R, W> {
        /// Bundle separate async reader and writer into a combined pseudo-socket
        pub fn new(r: R, w: W) -> Self {
            ReadWriteAsyncstd { r, w }
        }
        /// Borrow inner objects
        pub fn borrow(&self) -> (&R, &W) {
            (&self.r, &self.w)
        }
        /// Borrow the reader
        pub fn borrow_read(&self) -> &R {
            &self.r
        }
        /// Borrow the writer
        pub fn borrow_write(&self) -> &W {
            &self.w
        }
        /// Mutably borrow inner objects
        pub fn borrow_mut(&mut self) -> (&mut R, &mut W) {
            (&mut self.r, &mut self.w)
        }
        /// Mutably borrow the reader
        pub fn borrow_mut_read(&mut self) -> &mut R {
            &mut self.r
        }
        /// Mutably borrow the writer
        pub fn borrow_mut_write(&mut self) -> &mut W {
            &mut self.w
        }
        /// Convert ReadWrite back into individual reader and writer pair
        pub fn into_inner(self) -> (R, W) {
            (self.r, self.w)
        }
        /// Convert ReadWrite back into the reader, dropping the writer
        pub fn into_reader(self) -> R {
            self.r
        }
        /// Convert ReadWrite back into the writer, dropping the reader
        pub fn into_writer(self) -> W {
            self.w
        }

        /// Borrow pinned reader and writer
        pub fn borrow_pin(self: Pin<&mut Self>) -> (Pin<&mut R>, Pin<&mut W>) {
            let p = self.project();
            (p.r, p.w)
        }
        /// Borrow pinned reader
        pub fn borrow_pin_read(self: Pin<&mut Self>) -> Pin<&mut R> {
            self.project().r
        }
        /// Borrow pinned writer
        pub fn borrow_pin_write(self: Pin<&mut Self>) -> Pin<&mut W> {
            self.project().w
        }
    }

    impl<R: AsyncRead, W> AsyncRead for ReadWriteAsyncstd<R, W> {
        fn poll_read(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &mut [u8],
        ) -> std::task::Poll<std::io::Result<usize>> {
            self.project().r.poll_read(cx, buf)
        }

        fn poll_read_vectored(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            bufs: &mut [std::io::IoSliceMut<'_>],
        ) -> std::task::Poll<std::io::Result<usize>> {
            self.project().r.poll_read_vectored(cx, bufs)
        }
    }

    impl<R, W: AsyncWrite> AsyncWrite for ReadWriteAsyncstd<R, W> {
        fn poll_write(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &[u8],
        ) -> std::task::Poll<std::io::Result<usize>> {
            self.project().w.poll_write(cx, buf)
        }

        fn poll_flush(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            self.project().w.poll_flush(cx)
        }

        fn poll_close(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            self.project().w.poll_close(cx)
        }

        fn poll_write_vectored(
            self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            bufs: &[std::io::IoSlice<'_>],
        ) -> std::task::Poll<std::io::Result<usize>> {
            self.project().w.poll_write_vectored(cx, bufs)
        }
    }
}
#[cfg(all(feature = "asyncstd"))]
pub use asyncstd::ReadWriteAsyncstd;