multipart_write/write/
fold_ret.rs

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