multipart_write/write/
and_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 [`and_then`](super::MultipartWriteExt::and_then).
10    #[must_use = "futures do nothing unless polled"]
11    pub struct AndThen<Wr, Fut, F> {
12        #[pin]
13        writer: Wr,
14        #[pin]
15        future: Option<Fut>,
16        f: F,
17    }
18}
19
20impl<Wr, Fut, F> AndThen<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 `AndThen`, 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 AndThen<Wr, Fut, F>
55where
56    Wr: FusedMultipartWrite<Part>,
57    F: FnMut(Wr::Output) -> 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 AndThen<Wr, Fut, F>
67where
68    Wr: MultipartWrite<Part>,
69    F: FnMut(Wr::Output) -> 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        ready!(self.project().writer.poll_ready(cx))?;
79        Poll::Ready(Ok(()))
80    }
81
82    fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
83        Ok(self.project().writer.start_send(part)?)
84    }
85
86    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
87        ready!(self.project().writer.poll_flush(cx))?;
88        Poll::Ready(Ok(()))
89    }
90
91    fn poll_complete(
92        self: Pin<&mut Self>,
93        cx: &mut Context<'_>,
94    ) -> Poll<Result<Self::Output, Self::Error>> {
95        let mut this = self.project();
96        if this.future.is_none() {
97            let ret = ready!(this.writer.poll_complete(cx))?;
98            let fut = (this.f)(ret);
99            this.future.set(Some(fut));
100        }
101        let fut = this
102            .future
103            .as_mut()
104            .as_pin_mut()
105            .expect("polled AndThen after completion");
106        let out = ready!(fut.poll(cx));
107        this.future.set(None);
108        Poll::Ready(out)
109    }
110}
111
112impl<Wr, Fut, F> Debug for AndThen<Wr, Fut, F>
113where
114    Wr: Debug,
115    Fut: Debug,
116{
117    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
118        f.debug_struct("AndThen")
119            .field("writer", &self.writer)
120            .field("future", &self.future)
121            .field("f", &"impl FnMut(Wr::Output) -> Fut")
122            .finish()
123    }
124}