use std::fmt::{self, Debug, Formatter};
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures_core::ready;
use crate::{FusedMultipartWrite, MultipartWrite};
pin_project_lite::pin_project! {
#[must_use = "futures do nothing unless polled"]
pub struct ReadyPart<Wr, Part, P, Fut, F> {
#[pin]
writer: Wr,
f: F,
#[pin]
future: Option<Fut>,
buffered: Option<Part>,
_p: PhantomData<fn(P) -> Part>,
}
}
impl<Wr, Part, P, Fut, F> ReadyPart<Wr, Part, P, Fut, F> {
pub(super) fn new(writer: Wr, f: F) -> Self {
Self { writer, f, future: None, buffered: None, _p: PhantomData }
}
pub fn into_inner(self) -> Wr {
self.writer
}
pub fn get_ref(&self) -> &Wr {
&self.writer
}
pub fn get_mut(&mut self) -> &mut Wr {
&mut self.writer
}
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
self.project().writer
}
fn poll(
self: Pin<&mut Self>,
cx: &mut Context,
) -> Poll<Result<(), Wr::Error>>
where
Wr: MultipartWrite<Part>,
F: FnMut(P) -> Fut,
Fut: Future<Output = Result<Part, Wr::Error>>,
{
let mut this = self.project();
if this.buffered.is_some() {
match this.writer.as_mut().poll_ready(cx)? {
Poll::Ready(()) => {
this.writer.start_send(this.buffered.take().unwrap())?;
},
Poll::Pending => return Poll::Pending,
}
}
if let Some(fut) = this.future.as_mut().as_pin_mut() {
let part = ready!(fut.poll(cx))?;
*this.buffered = Some(part);
this.future.set(None);
}
Poll::Ready(Ok(()))
}
}
impl<Wr, Part, P, Fut, F> FusedMultipartWrite<P>
for ReadyPart<Wr, Part, P, Fut, F>
where
Wr: FusedMultipartWrite<Part>,
F: FnMut(P) -> Fut,
Fut: Future<Output = Result<Part, Wr::Error>>,
{
fn is_terminated(&self) -> bool {
self.writer.is_terminated()
}
}
impl<Wr, Part, P, Fut, F> MultipartWrite<P> for ReadyPart<Wr, Part, P, Fut, F>
where
Wr: MultipartWrite<Part>,
F: FnMut(P) -> Fut,
Fut: Future<Output = Result<Part, Wr::Error>>,
{
type Error = Wr::Error;
type Output = Wr::Output;
type Recv = ();
fn poll_ready(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
ready!(self.as_mut().poll(cx))?;
self.project().writer.poll_ready(cx)
}
fn start_send(
self: Pin<&mut Self>,
part: P,
) -> Result<Self::Recv, Self::Error> {
let mut this = self.project();
this.future.set(Some((this.f)(part)));
Ok(())
}
fn poll_flush(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
ready!(self.as_mut().poll(cx))?;
ready!(self.project().writer.poll_flush(cx)?);
Poll::Ready(Ok(()))
}
fn poll_complete(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Self::Output, Self::Error>> {
ready!(self.as_mut().poll(cx))?;
let out = ready!(self.project().writer.poll_complete(cx))?;
Poll::Ready(Ok(out))
}
}
impl<Wr, Part, P, Fut, F> Debug for ReadyPart<Wr, Part, P, Fut, F>
where
Wr: Debug,
Fut: Debug,
Part: Debug,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("ReadyPart")
.field("writer", &self.writer)
.field("future", &self.future)
.field("buffered", &self.buffered)
.finish()
}
}