use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use crate::io::traits::{AsyncRead, AsyncWrite};
pub trait AsyncReadExt: AsyncRead {
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>
where
Self: Unpin,
{
Read { reader: self, buf }
}
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>
where
Self: Unpin,
{
ReadExact {
reader: self,
buf,
filled: 0,
}
}
}
impl<R: AsyncRead + ?Sized> AsyncReadExt for R {}
pub struct Read<'a, R: ?Sized> {
reader: &'a mut R,
buf: &'a mut [u8],
}
impl<'a, R: AsyncRead + ?Sized + Unpin> Future for Read<'a, R> {
type Output = io::Result<usize>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
Pin::new(&mut *this.reader).poll_read(cx, this.buf)
}
}
pub struct ReadExact<'a, R: ?Sized> {
reader: &'a mut R,
buf: &'a mut [u8],
filled: usize,
}
impl<'a, R: AsyncRead + ?Sized + Unpin> Future for ReadExact<'a, R> {
type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
while this.filled < this.buf.len() {
let filled = this.filled;
match Pin::new(&mut *this.reader).poll_read(cx, &mut this.buf[filled..]) {
Poll::Ready(Ok(0)) => {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer",
)));
}
Poll::Ready(Ok(n)) => {
this.filled += n;
}
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Poll::Pending => return Poll::Pending,
}
}
Poll::Ready(Ok(()))
}
}
pub trait AsyncWriteExt: AsyncWrite {
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>
where
Self: Unpin,
{
Write { writer: self, buf }
}
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> WriteAll<'a, Self>
where
Self: Unpin,
{
WriteAll {
writer: self,
buf,
written: 0,
}
}
fn flush(&mut self) -> Flush<'_, Self>
where
Self: Unpin,
{
Flush { writer: self }
}
fn shutdown(&mut self) -> Shutdown<'_, Self>
where
Self: Unpin,
{
Shutdown { writer: self }
}
}
impl<W: AsyncWrite + ?Sized> AsyncWriteExt for W {}
pub struct Write<'a, W: ?Sized> {
writer: &'a mut W,
buf: &'a [u8],
}
impl<'a, W: AsyncWrite + ?Sized + Unpin> Future for Write<'a, W> {
type Output = io::Result<usize>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
Pin::new(&mut *this.writer).poll_write(cx, this.buf)
}
}
pub struct WriteAll<'a, W: ?Sized> {
writer: &'a mut W,
buf: &'a [u8],
written: usize,
}
impl<'a, W: AsyncWrite + ?Sized + Unpin> Future for WriteAll<'a, W> {
type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
while this.written < this.buf.len() {
match Pin::new(&mut *this.writer).poll_write(cx, &this.buf[this.written..]) {
Poll::Ready(Ok(0)) => {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::WriteZero,
"failed to write whole buffer",
)));
}
Poll::Ready(Ok(n)) => {
this.written += n;
}
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Poll::Pending => return Poll::Pending,
}
}
Poll::Ready(Ok(()))
}
}
pub struct Flush<'a, W: ?Sized> {
writer: &'a mut W,
}
impl<'a, W: AsyncWrite + ?Sized + Unpin> Future for Flush<'a, W> {
type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
Pin::new(&mut *this.writer).poll_flush(cx)
}
}
pub struct Shutdown<'a, W: ?Sized> {
writer: &'a mut W,
}
impl<'a, W: AsyncWrite + ?Sized + Unpin> Future for Shutdown<'a, W> {
type Output = io::Result<()>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
Pin::new(&mut *this.writer).poll_shutdown(cx)
}
}