| 1 | use futures::io::{AsyncRead, AsyncWrite}; |
| 2 | use pin_project::pin_project; |
| 3 | use std::io::Error; |
| 4 | use std::marker::Unpin; |
| 5 | use std::ops::{Deref, DerefMut}; |
| 6 | use std::pin::Pin; |
| 7 | use std::task::{Context, Poll}; |
| 8 | |
| 9 | #[pin_project] |
| 10 | #[derive(Debug)] |
| 11 | pub(crate) struct Fuse<T, U> { |
| 12 | #[pin] |
| 13 | pub t: T, |
| 14 | pub u: U, |
| 15 | } |
| 16 | |
| 17 | impl<T, U> Fuse<T, U> { |
| 18 | pub(crate) fn new(t: T, u: U) -> Self { |
| 19 | Self { t, u } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl<T, U> Deref for Fuse<T, U> { |
| 24 | type Target = T; |
| 25 | |
| 26 | fn deref(&self) -> &T { |
| 27 | &self.t |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl<T, U> DerefMut for Fuse<T, U> { |
| 32 | fn deref_mut(&mut self) -> &mut T { |
| 33 | &mut self.t |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl<T: AsyncRead + Unpin, U> AsyncRead for Fuse<T, U> { |
| 38 | fn poll_read( |
| 39 | self: Pin<&mut Self>, |
| 40 | cx: &mut Context<'_>, |
| 41 | buf: &mut [u8], |
| 42 | ) -> Poll<Result<usize, Error>> { |
| 43 | self.project().t.poll_read(cx, buf) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl<T: AsyncWrite + Unpin, U> AsyncWrite for Fuse<T, U> { |
| 48 | fn poll_write( |
| 49 | self: Pin<&mut Self>, |
| 50 | cx: &mut Context, |
| 51 | buf: &[u8], |
| 52 | ) -> Poll<Result<usize, Error>> { |
| 53 | self.project().t.poll_write(cx, buf) |
| 54 | } |
| 55 | fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Error>> { |
| 56 | self.project().t.poll_flush(cx) |
| 57 | } |
| 58 | fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Error>> { |
| 59 | self.project().t.poll_close(cx) |
| 60 | } |
| 61 | } |