use std::pin::Pin;
use std::task::{
Context,
Poll,
};
use crate::{
AsyncClose,
AsyncOutput,
};
#[must_use]
#[repr(transparent)]
pub struct BoxAsyncOutput<O>
where
O: AsyncOutput + ?Sized,
{
inner: Pin<Box<O>>,
}
impl<O> BoxAsyncOutput<O>
where
O: AsyncOutput + ?Sized,
{
#[inline(always)]
pub fn new(inner: Box<O>) -> Self {
Self {
inner: Box::into_pin(inner),
}
}
#[inline(always)]
#[must_use]
pub fn get_ref(&self) -> &O {
self.inner.as_ref().get_ref()
}
#[inline(always)]
#[must_use]
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut O> {
let this = unsafe { self.get_unchecked_mut() };
this.inner.as_mut()
}
#[inline(always)]
#[must_use]
pub fn into_inner(self) -> Pin<Box<O>> {
self.inner
}
}
impl<O> AsyncOutput for BoxAsyncOutput<O>
where
O: AsyncOutput + ?Sized,
{
type Item = O::Item;
#[inline(always)]
fn is_buffered(&self) -> bool {
self.get_ref().is_buffered()
}
#[inline(always)]
unsafe fn poll_write_unchecked(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
input: &[Self::Item],
index: usize,
count: usize,
) -> Poll<std::io::Result<usize>> {
unsafe {
self.get_pin_mut()
.poll_write_unchecked(cx, input, index, count)
}
}
#[inline(always)]
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::io::Result<()>> {
self.get_pin_mut().poll_flush(cx)
}
}
impl<O> AsyncClose for BoxAsyncOutput<O>
where
O: AsyncClose + ?Sized,
{
#[inline(always)]
fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::io::Result<()>> {
self.get_pin_mut().poll_close(cx)
}
}