use core::{
pin::Pin,
task::{Context, Poll},
};
use hyper_util::{
client::legacy::connect::{Connected, Connection},
rt::TokioIo,
};
use std::io;
use tokio::io::{AsyncRead, AsyncWrite};
pub trait AsyncStream: AsyncRead + AsyncWrite + Send + Unpin {}
impl<T: AsyncRead + AsyncWrite + Send + Unpin> AsyncStream for T {}
pub struct ProxyStream {
io: TokioIo<Box<dyn AsyncStream>>,
}
impl ProxyStream {
pub fn new(stream: impl AsyncStream + 'static) -> Self {
Self {
io: TokioIo::new(Box::new(stream)),
}
}
}
impl Connection for ProxyStream {
fn connected(&self) -> Connected {
Connected::new()
}
}
impl hyper::rt::Read for ProxyStream {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: hyper::rt::ReadBufCursor<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.io).poll_read(cx, buf)
}
}
impl hyper::rt::Write for ProxyStream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.io).poll_write(cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.io).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.io).poll_shutdown(cx)
}
}