multipart_write/write/
map_ret.rs

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