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    /// Consumes `MapRet`, returning the underlying writer.
23    pub fn into_inner(self) -> Wr {
24        self.writer
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<U, Wr, F, Part> FusedMultipartWrite<Part> for MapRet<Wr, F>
48where
49    Wr: FusedMultipartWrite<Part>,
50    F: FnMut(Wr::Ret) -> U,
51{
52    fn is_terminated(&self) -> bool {
53        self.writer.is_terminated()
54    }
55}
56
57impl<U, Wr, F, Part> MultipartWrite<Part> for MapRet<Wr, F>
58where
59    Wr: MultipartWrite<Part>,
60    F: FnMut(Wr::Ret) -> U,
61{
62    type Ret = U;
63    type Output = 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(mut self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
71        self.as_mut()
72            .project()
73            .writer
74            .start_send(part)
75            .map(self.as_mut().project().f)
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        self.project().writer.poll_complete(cx)
87    }
88}
89
90impl<Wr: Debug, F> Debug for MapRet<Wr, F> {
91    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
92        f.debug_struct("MapRet")
93            .field("writer", &self.writer)
94            .field("f", &"F")
95            .finish()
96    }
97}