#![cfg_attr(docsrs, feature(doc_cfg))]
use std::collections::VecDeque;
use std::convert::Infallible as Never;
use std::ops::DerefMut;
use std::pin::Pin;
use std::task::{Context, Poll};
pub mod io;
pub mod stream;
#[doc(inline)]
pub use stream::MultipartStreamExt;
pub mod write;
#[doc(inline)]
pub use write::MultipartWriteExt;
pub trait MultipartWrite<Part> {
type Recv;
type Output;
type Error;
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>;
fn start_send(
self: Pin<&mut Self>,
part: Part,
) -> Result<Self::Recv, Self::Error>;
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>>;
fn poll_complete(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Self::Output, Self::Error>>;
}
pub type BoxMultipartWrite<'a, Part, R, T, E> = Pin<
Box<dyn MultipartWrite<Part, Recv = R, Output = T, Error = E> + Send + 'a>,
>;
pub type LocalBoxMultipartWrite<'a, Part, R, T, E> =
Pin<Box<dyn MultipartWrite<Part, Recv = R, Output = T, Error = E> + 'a>>;
pub trait FusedMultipartWrite<Part>: MultipartWrite<Part> {
fn is_terminated(&self) -> bool;
}
pub type BoxFusedMultipartWrite<'a, Part, R, T, E> = Pin<
Box<
dyn FusedMultipartWrite<Part, Recv = R, Output = T, Error = E>
+ Send
+ 'a,
>,
>;
pub type LocalBoxFusedMultipartWrite<'a, Part, R, T, E> = Pin<
Box<dyn FusedMultipartWrite<Part, Recv = R, Output = T, Error = E> + 'a>,
>;
impl<W: ?Sized + MultipartWrite<Part> + Unpin, Part> MultipartWrite<Part>
for &mut W
{
type Error = W::Error;
type Output = W::Output;
type Recv = W::Recv;
fn poll_ready(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut **self).poll_ready(cx)
}
fn start_send(
mut self: Pin<&mut Self>,
part: Part,
) -> Result<Self::Recv, Self::Error> {
Pin::new(&mut **self).start_send(part)
}
fn poll_flush(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut **self).poll_flush(cx)
}
fn poll_complete(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Self::Output, Self::Error>> {
Pin::new(&mut **self).poll_complete(cx)
}
}
impl<W: ?Sized + FusedMultipartWrite<Part> + Unpin, Part>
FusedMultipartWrite<Part> for &mut W
{
fn is_terminated(&self) -> bool {
<W as FusedMultipartWrite<Part>>::is_terminated(&**self)
}
}
impl<W: ?Sized + MultipartWrite<Part> + Unpin, Part> MultipartWrite<Part>
for Box<W>
{
type Error = W::Error;
type Output = W::Output;
type Recv = W::Recv;
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Pin::new(self.get_mut().as_mut()).poll_ready(cx)
}
fn start_send(
self: Pin<&mut Self>,
part: Part,
) -> Result<Self::Recv, Self::Error> {
Pin::new(self.get_mut().as_mut()).start_send(part)
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Pin::new(self.get_mut().as_mut()).poll_flush(cx)
}
fn poll_complete(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Self::Output, Self::Error>> {
Pin::new(self.get_mut().as_mut()).poll_complete(cx)
}
}
impl<W: ?Sized + FusedMultipartWrite<Part> + Unpin, Part>
FusedMultipartWrite<Part> for Box<W>
{
fn is_terminated(&self) -> bool {
W::is_terminated(self)
}
}
impl<P, Part> MultipartWrite<Part> for Pin<P>
where
P: DerefMut + Unpin,
P::Target: MultipartWrite<Part>,
{
type Error = <P::Target as MultipartWrite<Part>>::Error;
type Output = <P::Target as MultipartWrite<Part>>::Output;
type Recv = <P::Target as MultipartWrite<Part>>::Recv;
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
self.get_mut().as_mut().poll_ready(cx)
}
fn start_send(
self: Pin<&mut Self>,
part: Part,
) -> Result<Self::Recv, Self::Error> {
self.get_mut().as_mut().start_send(part)
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
self.get_mut().as_mut().poll_flush(cx)
}
fn poll_complete(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Self::Output, Self::Error>> {
self.get_mut().as_mut().poll_complete(cx)
}
}
impl<P, Part> FusedMultipartWrite<Part> for Pin<P>
where
P: DerefMut + Unpin,
P::Target: FusedMultipartWrite<Part>,
{
fn is_terminated(&self) -> bool {
<P::Target as FusedMultipartWrite<Part>>::is_terminated(&**self)
}
}
impl<T> MultipartWrite<T> for Vec<T> {
type Error = Never;
type Output = Self;
type Recv = ();
fn poll_ready(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(
self: Pin<&mut Self>,
part: T,
) -> Result<Self::Recv, Self::Error> {
unsafe { self.get_unchecked_mut() }.push(part);
Ok(())
}
fn poll_flush(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_complete(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<Self::Output, Self::Error>> {
let this: &mut Vec<T> = unsafe { self.get_unchecked_mut() };
let out = std::mem::take(this);
Poll::Ready(Ok(out))
}
}
impl<T> MultipartWrite<T> for VecDeque<T> {
type Error = Never;
type Output = Self;
type Recv = ();
fn poll_ready(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(
self: Pin<&mut Self>,
part: T,
) -> Result<Self::Recv, Self::Error> {
unsafe { self.get_unchecked_mut() }.push_back(part);
Ok(())
}
fn poll_flush(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_complete(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<Self::Output, Self::Error>> {
let this: &mut VecDeque<T> = unsafe { self.get_unchecked_mut() };
let out = std::mem::take(this);
Poll::Ready(Ok(out))
}
}