Skip to main content

multipart_write/write/
then.rs

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