use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::io::{AsyncRead, 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 {
io::Error::from_raw_os_error(error.0)
}
impl AsyncRead for NagoyaStream {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buffer: &mut [u8],
) -> Poll<io::Result<usize>> {
self.get_mut().0.poll_read(cx, buffer).map_err(io_error)
}
}
impl AsyncWrite for NagoyaStream {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buffer: &[u8],
) -> Poll<io::Result<usize>> {
self.get_mut().0.poll_write(cx, buffer).map_err(io_error)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.get_mut().0.poll_flush(cx).map_err(io_error)
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.poll_flush(cx)
}
}