Skip to main content

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