use std::io::{self};
use std::{
pin::Pin,
task::{Context, Poll},
};
use futures_io::{AsyncRead, AsyncWrite};
use tokio::io::ReadBuf;
#[pin_project::pin_project] pub struct TokioIO<T>(#[pin] pub T)
where
T: AsyncRead + AsyncWrite + Unpin;
impl<T> tokio::io::AsyncWrite for TokioIO<T>
where
T: AsyncRead + AsyncWrite + Unpin,
{
fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
let this = self.project();
this.0.poll_write(cx, buf)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
self.project().0.poll_flush(cx)
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
self.project().0.poll_close(cx)
}
}
impl<T> tokio::io::AsyncRead for TokioIO<T>
where
T: AsyncRead + AsyncWrite + Unpin,
{
fn poll_read(self: Pin<&mut Self>, cx: &mut Context, buf: &mut ReadBuf<'_>) -> Poll<io::Result<()>> {
self.project().0.poll_read(cx, buf.initialize_unfilled()).map(|n| {
if let Ok(n) = n {
buf.advance(n);
}
Ok(())
})
}
}