use std::io::Result;
use std::pin::Pin;
use std::task::{
Context,
Poll,
};
use crate::traits::{
normalize_async_error,
validate_write_count,
};
use crate::{
FlushFuture,
WriteFullyFuture,
WriteFuture,
};
pub trait AsyncOutput {
type Item;
#[inline(always)]
#[must_use]
fn is_buffered(&self) -> bool {
false
}
unsafe fn poll_write_unchecked(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
input: &[Self::Item],
index: usize,
count: usize,
) -> Poll<Result<usize>>;
#[inline]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
input: &[Self::Item],
) -> Poll<Result<usize>> {
if input.is_empty() {
return Poll::Ready(Ok(0));
}
let requested = input.len();
match unsafe { self.poll_write_unchecked(cx, input, 0, requested) } {
Poll::Ready(Ok(written)) => Poll::Ready(
validate_write_count(written, requested).map(|()| written),
),
Poll::Ready(Err(error)) => {
Poll::Ready(Err(normalize_async_error(error)))
}
Poll::Pending => Poll::Pending,
}
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<()>>;
#[inline(always)]
fn write_async<'a>(
&'a mut self,
input: &'a [Self::Item],
) -> WriteFuture<'a, Self>
where
Self: Sized + Unpin,
{
WriteFuture::new(Pin::new(self), input)
}
#[inline(always)]
fn write_fully_async<'a>(
&'a mut self,
input: &'a [Self::Item],
) -> WriteFullyFuture<'a, Self>
where
Self: Sized + Unpin,
{
WriteFullyFuture::new(Pin::new(self), input)
}
#[inline(always)]
fn flush_async(&mut self) -> FlushFuture<'_, Self>
where
Self: Sized + Unpin,
{
FlushFuture::new(Pin::new(self))
}
}