use core::pin::Pin;
use core::task::{Context, Poll};
use nagoya::io::{self, ErrorKind, Read as AsyncRead, Write as AsyncWrite};
use nagoya::io::StreamError;
use nagoya::net::TcpStream;
#[derive(Debug)]
pub struct NagoyaStream(TcpStream);
impl NagoyaStream {
pub const fn new(stream: TcpStream) -> Self {
Self(stream)
}
pub fn into_inner(self) -> TcpStream {
self.0
}
pub const fn get_ref(&self) -> &TcpStream {
&self.0
}
pub const fn get_mut(&mut self) -> &mut TcpStream {
&mut self.0
}
}
impl From<TcpStream> for NagoyaStream {
fn from(stream: TcpStream) -> Self {
Self::new(stream)
}
}
fn io_error(error: StreamError) -> io::Error {
if error.interrupted() {
io::Error::new(ErrorKind::Interrupted)
} else {
io::Error::new(ErrorKind::Other)
}
}
impl AsyncRead for NagoyaStream {
async fn read(&mut self, buffer: &mut [u8]) -> Result<usize, io::Error> {
core::future::poll_fn(|cx| self.0.poll_read(cx, buffer).map_err(io_error)).await
}
}
impl AsyncWrite for NagoyaStream {
async fn write(&mut self, buffer: &[u8]) -> Result<usize, io::Error> {
core::future::poll_fn(|cx| self.0.poll_write(cx, buffer).map_err(io_error)).await
}
async fn flush(&mut self) -> Result<(), io::Error> {
core::future::poll_fn(|cx| self.0.poll_flush(cx).map_err(io_error)).await
}
}