|
| 1 | +use super::{AsyncRead, AsyncWrite, IoSlice, PeerAddr, Poll, ReadBuf, Result}; |
| 2 | +use pin_project::pin_project; |
| 3 | +use std::{pin::Pin, task::Context}; |
| 4 | + |
| 5 | +#[pin_project(project = EitherIoProj)] |
| 6 | +#[derive(Debug)] |
| 7 | +pub enum EitherIo<L, R> { |
| 8 | + Left(#[pin] L), |
| 9 | + Right(#[pin] R), |
| 10 | +} |
| 11 | + |
| 12 | +impl<L: PeerAddr, R: PeerAddr> PeerAddr for EitherIo<L, R> { |
| 13 | + #[inline] |
| 14 | + fn peer_addr(&self) -> Result<std::net::SocketAddr> { |
| 15 | + match self { |
| 16 | + Self::Left(l) => l.peer_addr(), |
| 17 | + Self::Right(r) => r.peer_addr(), |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl<L: AsyncRead, R: AsyncRead> AsyncRead for EitherIo<L, R> { |
| 23 | + #[inline] |
| 24 | + fn poll_read(self: Pin<&mut Self>, cx: &mut Context, buf: &mut ReadBuf<'_>) -> Poll<()> { |
| 25 | + match self.project() { |
| 26 | + EitherIoProj::Left(l) => l.poll_read(cx, buf), |
| 27 | + EitherIoProj::Right(r) => r.poll_read(cx, buf), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl<L: AsyncWrite, R: AsyncWrite> AsyncWrite for EitherIo<L, R> { |
| 33 | + #[inline] |
| 34 | + fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> { |
| 35 | + match self.project() { |
| 36 | + EitherIoProj::Left(l) => l.poll_shutdown(cx), |
| 37 | + EitherIoProj::Right(r) => r.poll_shutdown(cx), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + #[inline] |
| 42 | + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> { |
| 43 | + match self.project() { |
| 44 | + EitherIoProj::Left(l) => l.poll_flush(cx), |
| 45 | + EitherIoProj::Right(r) => r.poll_flush(cx), |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + #[inline] |
| 50 | + fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<usize> { |
| 51 | + match self.project() { |
| 52 | + EitherIoProj::Left(l) => l.poll_write(cx, buf), |
| 53 | + EitherIoProj::Right(r) => r.poll_write(cx, buf), |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + #[inline] |
| 58 | + fn poll_write_vectored( |
| 59 | + self: Pin<&mut Self>, |
| 60 | + cx: &mut Context, |
| 61 | + buf: &[IoSlice<'_>], |
| 62 | + ) -> Poll<usize> { |
| 63 | + match self.project() { |
| 64 | + EitherIoProj::Left(l) => l.poll_write_vectored(cx, buf), |
| 65 | + EitherIoProj::Right(r) => r.poll_write_vectored(cx, buf), |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + #[inline] |
| 70 | + fn is_write_vectored(&self) -> bool { |
| 71 | + match self { |
| 72 | + EitherIo::Left(l) => l.is_write_vectored(), |
| 73 | + EitherIo::Right(r) => r.is_write_vectored(), |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments