use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
pub(crate) struct IrohStream {
send: iroh::endpoint::SendStream,
recv: iroh::endpoint::RecvStream,
}
impl IrohStream {
pub(crate) fn new(send: iroh::endpoint::SendStream, recv: iroh::endpoint::RecvStream) -> Self {
Self { send, recv }
}
}
impl AsyncRead for IrohStream {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.recv).poll_read(cx, buf)
}
}
impl AsyncWrite for IrohStream {
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.send)
.poll_write(cx, buf)
.map(|r| r.map_err(|e| io::Error::new(io::ErrorKind::BrokenPipe, e)))
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.send).poll_flush(cx)
}
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.send).poll_shutdown(cx)
}
}