Skip to main content

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