multipart_write/write/
then.rs

1use crate::{FusedMultipartWrite, MultipartWrite};
2
3use futures::ready;
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7/// `MultipartWrite` for [`then`](super::MultipartWriteExt::then).
8#[derive(Debug)]
9#[must_use = "futures do nothing unless polled"]
10#[pin_project::pin_project]
11pub struct Then<Wr, F, Fut> {
12    #[pin]
13    writer: Wr,
14    #[pin]
15    output: Option<Fut>,
16    f: F,
17}
18
19impl<Wr, F, Fut> Then<Wr, F, Fut> {
20    pub(super) fn new(writer: Wr, f: F) -> Self {
21        Self {
22            writer,
23            output: None,
24            f,
25        }
26    }
27
28    /// Acquires a reference to the underlying writer.
29    pub fn get_ref(&self) -> &Wr {
30        &self.writer
31    }
32
33    /// Acquires a mutable reference to the underlying writer.
34    ///
35    /// It is inadvisable to directly write to the underlying writer.
36    pub fn get_mut(&mut self) -> &mut Wr {
37        &mut self.writer
38    }
39
40    /// Acquires a pinned mutable reference to the underlying writer.
41    ///
42    /// It is inadvisable to directly write to the underlying writer.
43    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
44        self.project().writer
45    }
46}
47
48impl<Wr, F, Fut, Part> FusedMultipartWrite<Part> for Then<Wr, F, Fut>
49where
50    Wr: FusedMultipartWrite<Part>,
51    F: FnMut(Wr::Output) -> Fut,
52    Fut: Future,
53{
54    fn is_terminated(&self) -> bool {
55        self.writer.is_terminated()
56    }
57}
58
59impl<Wr, F, Fut, Part> MultipartWrite<Part> for Then<Wr, F, Fut>
60where
61    Wr: MultipartWrite<Part>,
62    F: FnMut(Wr::Output) -> Fut,
63    Fut: Future,
64{
65    type Ret = Wr::Ret;
66    type Output = Fut::Output;
67    type Error = Wr::Error;
68
69    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
70        self.project().writer.poll_ready(cx)
71    }
72
73    fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
74        self.project().writer.start_send(part)
75    }
76
77    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
78        self.project().writer.poll_flush(cx)
79    }
80
81    fn poll_complete(
82        self: Pin<&mut Self>,
83        cx: &mut Context<'_>,
84    ) -> Poll<Result<Self::Output, Self::Error>> {
85        let mut this = self.project();
86
87        if this.output.is_none() {
88            let ret = ready!(this.writer.poll_complete(cx))?;
89            let fut = (this.f)(ret);
90            this.output.set(Some(fut));
91        }
92
93        let fut = this
94            .output
95            .as_mut()
96            .as_pin_mut()
97            .expect("polled Then after completion");
98        let ret = ready!(fut.poll(cx));
99        this.output.set(None);
100
101        Poll::Ready(Ok(ret))
102    }
103}