multipart_write/write/
map_err.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_err`](super::MultipartWriteExt::map_err).
9    #[must_use = "futures do nothing unless polled"]
10    pub struct MapErr<Wr, F> {
11        #[pin]
12        writer: Wr,
13        f: F,
14    }
15}
16
17impl<Wr, F> MapErr<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<Wr, F, Part, E> FusedMultipartWrite<Part> for MapErr<Wr, F>
43where
44    Wr: FusedMultipartWrite<Part>,
45    F: FnMut(Wr::Error) -> E,
46{
47    fn is_terminated(&self) -> bool {
48        self.writer.is_terminated()
49    }
50}
51
52impl<Wr, F, Part, E> MultipartWrite<Part> for MapErr<Wr, F>
53where
54    Wr: MultipartWrite<Part>,
55    F: FnMut(Wr::Error) -> E,
56{
57    type Ret = Wr::Ret;
58    type Output = Wr::Output;
59    type Error = E;
60
61    fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
62        self.as_mut()
63            .project()
64            .writer
65            .poll_ready(cx)
66            .map_err(self.as_mut().project().f)
67    }
68
69    fn start_send(mut self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
70        self.as_mut()
71            .project()
72            .writer
73            .start_send(part)
74            .map_err(self.as_mut().project().f)
75    }
76
77    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
78        self.as_mut()
79            .project()
80            .writer
81            .poll_flush(cx)
82            .map_err(self.as_mut().project().f)
83    }
84
85    fn poll_complete(
86        mut self: Pin<&mut Self>,
87        cx: &mut Context<'_>,
88    ) -> Poll<Result<Self::Output, Self::Error>> {
89        self.as_mut()
90            .project()
91            .writer
92            .poll_complete(cx)
93            .map_err(self.as_mut().project().f)
94    }
95}
96
97impl<Wr: Debug, F> Debug for MapErr<Wr, F> {
98    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
99        f.debug_struct("MapErr")
100            .field("writer", &self.writer)
101            .field("f", &"F")
102            .finish()
103    }
104}