use core::pin::Pin;
use core::task::{Context, Poll};
use std::io;
use futures_io::{AsyncBufRead, AsyncRead, AsyncWrite};
#[derive(Clone, Copy, Debug, Default)]
pub struct Empty {
_private: (),
}
pub fn empty() -> Empty {
Empty { _private: () }
}
impl AsyncRead for Empty {
fn poll_read(
self: Pin<&mut Self>,
_: &mut Context<'_>,
_: &mut [u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(0))
}
}
impl AsyncBufRead for Empty {
fn poll_fill_buf(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
Poll::Ready(Ok(&[]))
}
fn consume(self: Pin<&mut Self>, _: usize) {}
}
impl AsyncWrite for Empty {
fn poll_write(
self: Pin<&mut Self>,
_: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(Ok(buf.len()))
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}
#[derive(Clone, Copy, Debug)]
pub struct Repeat {
byte: u8,
}
pub fn repeat(byte: u8) -> Repeat {
Repeat { byte }
}
impl AsyncRead for Repeat {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
crate::coop::poll_budget(cx, |_| {
buf.fill(self.byte);
Poll::Ready(Ok(buf.len()))
})
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Sink {
_private: (),
}
pub fn sink() -> Sink {
Sink { _private: () }
}
impl AsyncWrite for Sink {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
crate::coop::poll_budget(cx, |_| Poll::Ready(Ok(buf.len())))
}
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
}