multipart_write/write/
then.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 [`then`](super::MultipartWriteExt::then).
10    #[must_use = "futures do nothing unless polled"]
11    pub struct Then<Wr, Fut, F> {
12        #[pin]
13        writer: Wr,
14        #[pin]
15        future: Option<Fut>,
16        f: F,
17    }
18}
19
20impl<Wr, Fut, F> Then<Wr, Fut, F> {
21    pub(super) fn new(writer: Wr, f: F) -> Self {
22        Self {
23            writer,
24            future: None,
25            f,
26        }
27    }
28
29    /// Consumes `Then`, returning the underlying writer.
30    pub fn into_inner(self) -> Wr {
31        self.writer
32    }
33
34    /// Acquires a reference to the underlying writer.
35    pub fn get_ref(&self) -> &Wr {
36        &self.writer
37    }
38
39    /// Acquires a mutable reference to the underlying writer.
40    ///
41    /// It is inadvisable to directly write to the underlying writer.
42    pub fn get_mut(&mut self) -> &mut Wr {
43        &mut self.writer
44    }
45
46    /// Acquires a pinned mutable reference to the underlying writer.
47    ///
48    /// It is inadvisable to directly write to the underlying writer.
49    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
50        self.project().writer
51    }
52}
53
54impl<Wr, Fut, F, Part, T, E> FusedMultipartWrite<Part> for Then<Wr, Fut, F>
55where
56    Wr: FusedMultipartWrite<Part>,
57    F: FnMut(Result<Wr::Output, Wr::Error>) -> Fut,
58    Fut: Future<Output = Result<T, E>>,
59    E: From<Wr::Error>,
60{
61    fn is_terminated(&self) -> bool {
62        self.writer.is_terminated()
63    }
64}
65
66impl<Wr, Fut, F, Part, T, E> MultipartWrite<Part> for Then<Wr, Fut, F>
67where
68    Wr: MultipartWrite<Part>,
69    F: FnMut(Result<Wr::Output, Wr::Error>) -> Fut,
70    Fut: Future<Output = Result<T, E>>,
71    E: From<Wr::Error>,
72{
73    type Ret = Wr::Ret;
74    type Output = T;
75    type Error = E;
76
77    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
78        self.project().writer.poll_ready(cx).map_err(E::from)
79    }
80
81    fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
82        self.project().writer.start_send(part).map_err(E::from)
83    }
84
85    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
86        self.project().writer.poll_flush(cx).map_err(E::from)
87    }
88
89    fn poll_complete(
90        self: Pin<&mut Self>,
91        cx: &mut Context<'_>,
92    ) -> Poll<Result<Self::Output, Self::Error>> {
93        let mut this = self.project();
94        if this.future.is_none() {
95            let res = ready!(this.writer.poll_complete(cx));
96            let fut = (this.f)(res);
97            this.future.set(Some(fut));
98        }
99        let fut = this
100            .future
101            .as_mut()
102            .as_pin_mut()
103            .expect("polled Then after completion");
104        let out = ready!(fut.poll(cx));
105        this.future.set(None);
106        Poll::Ready(out)
107    }
108}
109
110impl<Wr, Fut, F> Debug for Then<Wr, Fut, F>
111where
112    Wr: Debug,
113    Fut: Debug,
114{
115    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
116        f.debug_struct("Then")
117            .field("writer", &self.writer)
118            .field("future", &self.future)
119            .field("f", &"impl FnMut(Result<Wr::Output, Wr::Error>) -> Fut")
120            .finish()
121    }
122}