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    /// Acquires a reference to the underlying writer.
30    pub fn get_ref(&self) -> &Wr {
31        &self.writer
32    }
33
34    /// Acquires a mutable reference to the underlying writer.
35    ///
36    /// It is inadvisable to directly write to the underlying writer.
37    pub fn get_mut(&mut self) -> &mut Wr {
38        &mut self.writer
39    }
40
41    /// Acquires a pinned mutable reference to the underlying writer.
42    ///
43    /// It is inadvisable to directly write to the underlying writer.
44    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
45        self.project().writer
46    }
47}
48
49impl<Wr, Fut, F, Part, T, E> FusedMultipartWrite<Part> for AndThen<Wr, Fut, F>
50where
51    Wr: FusedMultipartWrite<Part>,
52    F: FnMut(Wr::Output) -> Fut,
53    Fut: Future<Output = Result<T, E>>,
54    E: From<Wr::Error>,
55{
56    fn is_terminated(&self) -> bool {
57        self.writer.is_terminated()
58    }
59}
60
61impl<Wr, Fut, F, Part, T, E> MultipartWrite<Part> for AndThen<Wr, Fut, F>
62where
63    Wr: MultipartWrite<Part>,
64    F: FnMut(Wr::Output) -> Fut,
65    Fut: Future<Output = Result<T, E>>,
66    E: From<Wr::Error>,
67{
68    type Ret = Wr::Ret;
69    type Output = T;
70    type Error = E;
71
72    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
73        ready!(self.project().writer.poll_ready(cx))?;
74        Poll::Ready(Ok(()))
75    }
76
77    fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
78        Ok(self.project().writer.start_send(part)?)
79    }
80
81    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
82        ready!(self.project().writer.poll_flush(cx))?;
83        Poll::Ready(Ok(()))
84    }
85
86    fn poll_complete(
87        self: Pin<&mut Self>,
88        cx: &mut Context<'_>,
89    ) -> Poll<Result<Self::Output, Self::Error>> {
90        let mut this = self.project();
91        if this.future.is_none() {
92            let ret = ready!(this.writer.poll_complete(cx))?;
93            let fut = (this.f)(ret);
94            this.future.set(Some(fut));
95        }
96        let fut = this
97            .future
98            .as_mut()
99            .as_pin_mut()
100            .expect("polled AndThen after completion");
101        let out = ready!(fut.poll(cx));
102        this.future.set(None);
103        Poll::Ready(out)
104    }
105}
106
107impl<Wr, Fut, F> Debug for AndThen<Wr, Fut, F>
108where
109    Wr: Debug,
110    Fut: Debug,
111{
112    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
113        f.debug_struct("AndThen")
114            .field("writer", &self.writer)
115            .field("future", &self.future)
116            .field("f", &"impl FnMut(Wr::Output) -> Fut")
117            .finish()
118    }
119}