1 //! Join two values implementing `AsyncRead` and `AsyncWrite` into a single one.
2 
3 use crate::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
4 
5 use std::io;
6 use std::pin::Pin;
7 use std::task::{Context, Poll};
8 
9 /// Join two values implementing `AsyncRead` and `AsyncWrite` into a
10 /// single handle.
join<R, W>(reader: R, writer: W) -> Join<R, W> where R: AsyncRead, W: AsyncWrite,11 pub fn join<R, W>(reader: R, writer: W) -> Join<R, W>
12 where
13     R: AsyncRead,
14     W: AsyncWrite,
15 {
16     Join { reader, writer }
17 }
18 
19 pin_project_lite::pin_project! {
20     /// Joins two values implementing `AsyncRead` and `AsyncWrite` into a
21     /// single handle.
22     #[derive(Debug)]
23     pub struct Join<R, W> {
24         #[pin]
25         reader: R,
26         #[pin]
27         writer: W,
28     }
29 }
30 
31 impl<R, W> Join<R, W>
32 where
33     R: AsyncRead,
34     W: AsyncWrite,
35 {
36     /// Splits this `Join` back into its `AsyncRead` and `AsyncWrite`
37     /// components.
into_inner(self) -> (R, W)38     pub fn into_inner(self) -> (R, W) {
39         (self.reader, self.writer)
40     }
41 
42     /// Returns a reference to the inner reader.
reader(&self) -> &R43     pub fn reader(&self) -> &R {
44         &self.reader
45     }
46 
47     /// Returns a reference to the inner writer.
writer(&self) -> &W48     pub fn writer(&self) -> &W {
49         &self.writer
50     }
51 
52     /// Returns a mutable reference to the inner reader.
reader_mut(&mut self) -> &mut R53     pub fn reader_mut(&mut self) -> &mut R {
54         &mut self.reader
55     }
56 
57     /// Returns a mutable reference to the inner writer.
writer_mut(&mut self) -> &mut W58     pub fn writer_mut(&mut self) -> &mut W {
59         &mut self.writer
60     }
61 
62     /// Returns a pinned mutable reference to the inner reader.
reader_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R>63     pub fn reader_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R> {
64         self.project().reader
65     }
66 
67     /// Returns a pinned mutable reference to the inner writer.
writer_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W>68     pub fn writer_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W> {
69         self.project().writer
70     }
71 }
72 
73 impl<R, W> AsyncRead for Join<R, W>
74 where
75     R: AsyncRead,
76 {
poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_>, ) -> Poll<Result<(), io::Error>>77     fn poll_read(
78         self: Pin<&mut Self>,
79         cx: &mut Context<'_>,
80         buf: &mut ReadBuf<'_>,
81     ) -> Poll<Result<(), io::Error>> {
82         self.project().reader.poll_read(cx, buf)
83     }
84 }
85 
86 impl<R, W> AsyncWrite for Join<R, W>
87 where
88     W: AsyncWrite,
89 {
poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize, io::Error>>90     fn poll_write(
91         self: Pin<&mut Self>,
92         cx: &mut Context<'_>,
93         buf: &[u8],
94     ) -> Poll<Result<usize, io::Error>> {
95         self.project().writer.poll_write(cx, buf)
96     }
97 
poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>>98     fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
99         self.project().writer.poll_flush(cx)
100     }
101 
poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>>102     fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
103         self.project().writer.poll_shutdown(cx)
104     }
105 
poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[io::IoSlice<'_>], ) -> Poll<Result<usize, io::Error>>106     fn poll_write_vectored(
107         self: Pin<&mut Self>,
108         cx: &mut Context<'_>,
109         bufs: &[io::IoSlice<'_>],
110     ) -> Poll<Result<usize, io::Error>> {
111         self.project().writer.poll_write_vectored(cx, bufs)
112     }
113 
is_write_vectored(&self) -> bool114     fn is_write_vectored(&self) -> bool {
115         self.writer.is_write_vectored()
116     }
117 }
118 
119 impl<R, W> AsyncBufRead for Join<R, W>
120 where
121     R: AsyncBufRead,
122 {
poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>>123     fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
124         self.project().reader.poll_fill_buf(cx)
125     }
126 
consume(self: Pin<&mut Self>, amt: usize)127     fn consume(self: Pin<&mut Self>, amt: usize) {
128         self.project().reader.consume(amt)
129     }
130 }
131