use std::io::Error;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::{AsyncRead, AsyncWrite};
use pin_project::pin_project;
#[pin_project]
pub struct JoinReadWrite<R: AsyncRead, W: AsyncWrite> {
#[pin]
r: R,
#[pin]
w: W,
}
impl<R: AsyncRead, W: AsyncWrite> JoinReadWrite<R, W> {
pub fn new(r: R, w: W) -> Self {
JoinReadWrite { r, w }
}
pub fn into_parts(self) -> (R, W) {
let JoinReadWrite { r, w } = self;
(r, w)
}
}
impl<R: AsyncRead, W: AsyncWrite> AsyncRead for JoinReadWrite<R, W> {
fn poll_read(
self: Pin<&mut Self>,
c: &mut Context,
out: &mut [u8],
) -> Poll<Result<usize, Error>> {
self.project().r.poll_read(c, out)
}
}
impl<R: AsyncRead, W: AsyncWrite> AsyncWrite for JoinReadWrite<R, W> {
fn poll_write(
self: Pin<&mut Self>,
c: &mut Context,
data: &[u8],
) -> Poll<Result<usize, Error>> {
self.project().w.poll_write(c, data)
}
fn poll_flush(self: Pin<&mut Self>, c: &mut Context) -> Poll<Result<(), Error>> {
self.project().w.poll_flush(c)
}
fn poll_close(self: Pin<&mut Self>, c: &mut Context) -> Poll<Result<(), Error>> {
self.project().w.poll_close(c)
}
}