Skip to main content

multipart_write/write/
fold_sent.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 [`fold_sent`].
11    ///
12    ///[`fold_sent`]: super::MultipartWriteExt::fold_sent
13    #[must_use = "futures do nothing unless polled"]
14    pub struct FoldSent<Wr, T, F> {
15        #[pin]
16        writer: Wr,
17        acc: Option<T>,
18        f: F,
19    }
20}
21
22impl<Wr, T, F> FoldSent<Wr, T, F> {
23    pub(super) fn new(writer: Wr, id: T, f: F) -> Self {
24        Self { writer, acc: Some(id), f }
25    }
26
27    /// Consumes `FoldSent`, returning the underlying writer.
28    pub fn into_inner(self) -> Wr {
29        self.writer
30    }
31
32    /// Acquires a reference to the underlying writer.
33    pub fn get_ref(&self) -> &Wr {
34        &self.writer
35    }
36
37    /// Acquires a mutable reference to the underlying writer.
38    ///
39    /// It is inadvisable to directly write to the underlying writer.
40    pub fn get_mut(&mut self) -> &mut Wr {
41        &mut self.writer
42    }
43
44    /// Acquires a pinned mutable reference to the underlying writer.
45    ///
46    /// It is inadvisable to directly write to the underlying writer.
47    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
48        self.project().writer
49    }
50}
51
52impl<Wr, T, F, Part> FusedMultipartWrite<Part> for FoldSent<Wr, T, F>
53where
54    Wr: FusedMultipartWrite<Part>,
55    F: FnMut(T, &Wr::Recv) -> T,
56{
57    fn is_terminated(&self) -> bool {
58        self.writer.is_terminated()
59    }
60}
61
62impl<Wr, T, F, Part> MultipartWrite<Part> for FoldSent<Wr, T, F>
63where
64    Wr: MultipartWrite<Part>,
65    F: FnMut(T, &Wr::Recv) -> T,
66{
67    type Error = Wr::Error;
68    type Output = (T, Wr::Output);
69    type Recv = Wr::Recv;
70
71    fn poll_ready(
72        self: Pin<&mut Self>,
73        cx: &mut Context<'_>,
74    ) -> Poll<Result<(), Self::Error>> {
75        self.project().writer.poll_ready(cx)
76    }
77
78    fn start_send(
79        self: Pin<&mut Self>,
80        part: Part,
81    ) -> Result<Self::Recv, Self::Error> {
82        let mut this = self.project();
83        let ret = this.writer.as_mut().start_send(part)?;
84        let new_acc = this.acc.take().map(|acc| (this.f)(acc, &ret));
85        *this.acc = new_acc;
86        Ok(ret)
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        let out = ready!(this.writer.as_mut().poll_complete(cx))?;
102        let acc = this.acc.take().expect("polled FoldSent after completion");
103        Poll::Ready(Ok((acc, out)))
104    }
105}
106
107impl<Wr, T, F> Debug for FoldSent<Wr, T, F>
108where
109    Wr: Debug,
110    T: Debug,
111{
112    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
113        f.debug_struct("FoldSent")
114            .field("writer", &self.writer)
115            .field("acc", &self.acc)
116            .finish()
117    }
118}