multipart_write/write/
lift.rs

1use crate::{FusedMultipartWrite, MultipartWrite};
2
3use futures_core::ready;
4use std::fmt::{self, Debug, Formatter};
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8pin_project_lite::pin_project! {
9    /// `MultipartWrite` for [`lift`](super::MultipartWriteExt::lift).
10    #[must_use = "futures do nothing unless polled"]
11    pub struct Lift<Wr, U, Part> {
12        #[pin]
13        inner: Wr,
14        #[pin]
15        writer: U,
16        buffered: Option<Part>,
17    }
18}
19
20impl<Wr, U, Part> Lift<Wr, U, Part> {
21    pub(super) fn new(inner: Wr, writer: U) -> Self {
22        Self {
23            inner,
24            writer,
25            buffered: None,
26        }
27    }
28
29    fn poll_send_inner<T>(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Wr::Error>>
30    where
31        U: MultipartWrite<T, Output = Part>,
32        Wr: MultipartWrite<Part>,
33        Wr::Error: From<U::Error>,
34    {
35        let mut this = self.project();
36
37        if this.buffered.is_none() {
38            let part = ready!(this.writer.as_mut().poll_complete(cx))?;
39            *this.buffered = Some(part);
40        }
41        ready!(this.inner.as_mut().poll_ready(cx))?;
42        let _ = this
43            .inner
44            .as_mut()
45            .start_send(this.buffered.take().unwrap())?;
46
47        Poll::Ready(Ok(()))
48    }
49}
50
51impl<T, Wr, U, Part> FusedMultipartWrite<T> for Lift<Wr, U, Part>
52where
53    U: FusedMultipartWrite<T, Output = Part>,
54    Wr: FusedMultipartWrite<Part>,
55    Wr::Error: From<U::Error>,
56{
57    fn is_terminated(&self) -> bool {
58        self.inner.is_terminated() || self.writer.is_terminated()
59    }
60}
61
62impl<T, Wr, U, Part> MultipartWrite<T> for Lift<Wr, U, Part>
63where
64    U: MultipartWrite<T, Output = Part>,
65    Wr: MultipartWrite<Part>,
66    Wr::Error: From<U::Error>,
67{
68    type Ret = U::Ret;
69    type Error = Wr::Error;
70    type Output = Wr::Output;
71
72    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
73        self.project()
74            .writer
75            .as_mut()
76            .poll_ready(cx)
77            .map_err(Wr::Error::from)
78    }
79
80    fn start_send(self: Pin<&mut Self>, part: T) -> Result<Self::Ret, Self::Error> {
81        self.project()
82            .writer
83            .as_mut()
84            .start_send(part)
85            .map_err(Wr::Error::from)
86    }
87
88    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
89        ready!(self.as_mut().poll_send_inner(cx))?;
90        self.project().inner.poll_flush(cx)
91    }
92
93    fn poll_complete(
94        mut self: Pin<&mut Self>,
95        cx: &mut Context<'_>,
96    ) -> Poll<Result<Self::Output, Self::Error>> {
97        ready!(self.as_mut().poll_send_inner(cx))?;
98        self.project().inner.poll_complete(cx)
99    }
100}
101
102impl<Wr, U, Part> Debug for Lift<Wr, U, Part>
103where
104    Wr: Debug,
105    U: Debug,
106    Part: Debug,
107{
108    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
109        f.debug_struct("Lift")
110            .field("inner", &self.inner)
111            .field("writer", &self.writer)
112            .field("buffered", &self.buffered)
113            .finish()
114    }
115}