Skip to main content

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