multipart_write/write/
fold_ret.rs

1use crate::{FusedMultipartWrite, MultipartWrite};
2
3use futures_core::ready;
4use std::fmt::{self, Debug, Formatter};
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
8pin_project_lite::pin_project! {
9    /// `MultipartWrite` for [`fold_ret`](super::MultipartWriteExt::fold_ret).
10    #[must_use = "futures do nothing unless polled"]
11    pub struct FoldRet<Wr, T, F> {
12        #[pin]
13        writer: Wr,
14        acc: Option<T>,
15        f: F,
16    }
17}
18
19impl<Wr, T, F> FoldRet<Wr, T, F> {
20    pub(super) fn new(writer: Wr, id: T, f: F) -> Self {
21        Self {
22            writer,
23            acc: Some(id),
24            f,
25        }
26    }
27
28    /// Consumes `FoldRet`, 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, T, F, Part> FusedMultipartWrite<Part> for FoldRet<Wr, T, F>
54where
55    Wr: FusedMultipartWrite<Part>,
56    F: FnMut(T, &Wr::Ret) -> T,
57{
58    fn is_terminated(&self) -> bool {
59        self.writer.is_terminated()
60    }
61}
62
63impl<Wr, T, F, Part> MultipartWrite<Part> for FoldRet<Wr, T, F>
64where
65    Wr: MultipartWrite<Part>,
66    F: FnMut(T, &Wr::Ret) -> T,
67{
68    type Ret = Wr::Ret;
69    type Output = (T, Wr::Output);
70    type Error = Wr::Error;
71
72    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
73        self.project().writer.poll_ready(cx)
74    }
75
76    fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
77        let mut this = self.project();
78        let ret = this.writer.as_mut().start_send(part)?;
79        let new_acc = this.acc.take().map(|acc| (this.f)(acc, &ret));
80        *this.acc = new_acc;
81        Ok(ret)
82    }
83
84    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
85        self.project().writer.poll_flush(cx)
86    }
87
88    fn poll_complete(
89        self: Pin<&mut Self>,
90        cx: &mut Context<'_>,
91    ) -> Poll<Result<Self::Output, Self::Error>> {
92        let mut this = self.project();
93        let out = ready!(this.writer.as_mut().poll_complete(cx))?;
94        let acc = this.acc.take().expect("polled FoldRet after completion");
95        Poll::Ready(Ok((acc, out)))
96    }
97}
98
99impl<Wr, T, F> Debug for FoldRet<Wr, T, F>
100where
101    Wr: Debug,
102    T: Debug,
103{
104    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
105        f.debug_struct("FoldRet")
106            .field("writer", &self.writer)
107            .field("acc", &self.acc)
108            .field("f", &"impl FnMut(T, &Wr::Ret) -> T")
109            .finish()
110    }
111}