Skip to main content

multipart_write/write/
lift.rs

1use std::fmt::{self, Debug, Formatter};
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use futures_core::ready;
6
7use crate::{FusedMultipartWrite, MultipartWrite};
8
9pin_project_lite::pin_project! {
10    /// `MultipartWrite` for [`lift`](super::MultipartWriteExt::lift).
11    #[must_use = "futures do nothing unless polled"]
12    pub struct Lift<Wr, U, Part> {
13        #[pin]
14        inner: Wr,
15        #[pin]
16        writer: U,
17        buffered: Option<Part>,
18    }
19}
20
21impl<Wr, U, Part> Lift<Wr, U, Part> {
22    pub(super) fn new(inner: Wr, writer: U) -> Self {
23        Self { inner, writer, buffered: None }
24    }
25
26    /// Consumes `Lift`, returning the underlying writer.
27    pub fn into_inner(self) -> Wr {
28        self.inner
29    }
30
31    /// Acquires a reference to the underlying writer.
32    pub fn get_ref(&self) -> &Wr {
33        &self.inner
34    }
35
36    /// Acquires a mutable reference to the underlying writer.
37    ///
38    /// It is inadvisable to directly write to the underlying writer.
39    pub fn get_mut(&mut self) -> &mut Wr {
40        &mut self.inner
41    }
42
43    /// Acquires a pinned mutable reference to the underlying writer.
44    ///
45    /// It is inadvisable to directly write to the underlying writer.
46    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
47        self.project().inner
48    }
49
50    /// Acquires a reference to the outermost writer.
51    pub fn get_outer_ref(&self) -> &U {
52        &self.writer
53    }
54
55    /// Acquires a mutable reference to the outermost writer.
56    ///
57    /// It is inadvisable to directly write to the outermost writer.
58    pub fn get_outer_mut(&mut self) -> &mut U {
59        &mut self.writer
60    }
61
62    /// Acquires a pinned mutable reference to the outermost writer.
63    ///
64    /// It is inadvisable to directly write to the outermost writer.
65    pub fn get_outer_pin_mut(self: Pin<&mut Self>) -> Pin<&mut U> {
66        self.project().writer
67    }
68
69    fn poll_send_inner<T>(
70        self: Pin<&mut Self>,
71        cx: &mut Context<'_>,
72    ) -> Poll<Result<(), Wr::Error>>
73    where
74        U: MultipartWrite<T, Output = Part>,
75        Wr: MultipartWrite<Part>,
76        Wr::Error: From<U::Error>,
77    {
78        let mut this = self.project();
79
80        if this.buffered.is_none() {
81            let part = ready!(this.writer.as_mut().poll_complete(cx))?;
82            *this.buffered = Some(part);
83        }
84        ready!(this.inner.as_mut().poll_ready(cx))?;
85        let _ =
86            this.inner.as_mut().start_send(this.buffered.take().unwrap())?;
87
88        Poll::Ready(Ok(()))
89    }
90}
91
92impl<T, Wr, U, Part> FusedMultipartWrite<T> for Lift<Wr, U, Part>
93where
94    U: FusedMultipartWrite<T, Output = Part>,
95    Wr: FusedMultipartWrite<Part>,
96    Wr::Error: From<U::Error>,
97{
98    fn is_terminated(&self) -> bool {
99        self.inner.is_terminated() || self.writer.is_terminated()
100    }
101}
102
103impl<T, Wr, U, Part> MultipartWrite<T> for Lift<Wr, U, Part>
104where
105    U: MultipartWrite<T, Output = Part>,
106    Wr: MultipartWrite<Part>,
107    Wr::Error: From<U::Error>,
108{
109    type Error = Wr::Error;
110    type Output = Wr::Output;
111    type Recv = U::Recv;
112
113    fn poll_ready(
114        self: Pin<&mut Self>,
115        cx: &mut Context<'_>,
116    ) -> Poll<Result<(), Self::Error>> {
117        self.project().writer.as_mut().poll_ready(cx).map_err(Wr::Error::from)
118    }
119
120    fn start_send(
121        self: Pin<&mut Self>,
122        part: T,
123    ) -> Result<Self::Recv, Self::Error> {
124        self.project().writer.as_mut().start_send(part).map_err(Wr::Error::from)
125    }
126
127    fn poll_flush(
128        mut self: Pin<&mut Self>,
129        cx: &mut Context<'_>,
130    ) -> Poll<Result<(), Self::Error>> {
131        ready!(self.as_mut().poll_send_inner(cx))?;
132        self.project().inner.poll_flush(cx)
133    }
134
135    fn poll_complete(
136        mut self: Pin<&mut Self>,
137        cx: &mut Context<'_>,
138    ) -> Poll<Result<Self::Output, Self::Error>> {
139        ready!(self.as_mut().poll_send_inner(cx))?;
140        self.project().inner.poll_complete(cx)
141    }
142}
143
144impl<Wr, U, Part> Debug for Lift<Wr, U, Part>
145where
146    Wr: Debug,
147    U: Debug,
148    Part: Debug,
149{
150    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
151        f.debug_struct("Lift")
152            .field("inner", &self.inner)
153            .field("writer", &self.writer)
154            .field("buffered", &self.buffered)
155            .finish()
156    }
157}