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    /// Consumes `MapErr`, 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<Wr, F, Part, E> FusedMultipartWrite<Part> for MapErr<Wr, F>
48where
49    Wr: FusedMultipartWrite<Part>,
50    F: FnMut(Wr::Error) -> E,
51{
52    fn is_terminated(&self) -> bool {
53        self.writer.is_terminated()
54    }
55}
56
57impl<Wr, F, Part, E> MultipartWrite<Part> for MapErr<Wr, F>
58where
59    Wr: MultipartWrite<Part>,
60    F: FnMut(Wr::Error) -> E,
61{
62    type Ret = Wr::Ret;
63    type Output = Wr::Output;
64    type Error = E;
65
66    fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
67        self.as_mut()
68            .project()
69            .writer
70            .poll_ready(cx)
71            .map_err(self.as_mut().project().f)
72    }
73
74    fn start_send(mut self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
75        self.as_mut()
76            .project()
77            .writer
78            .start_send(part)
79            .map_err(self.as_mut().project().f)
80    }
81
82    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
83        self.as_mut()
84            .project()
85            .writer
86            .poll_flush(cx)
87            .map_err(self.as_mut().project().f)
88    }
89
90    fn poll_complete(
91        mut self: Pin<&mut Self>,
92        cx: &mut Context<'_>,
93    ) -> Poll<Result<Self::Output, Self::Error>> {
94        self.as_mut()
95            .project()
96            .writer
97            .poll_complete(cx)
98            .map_err(self.as_mut().project().f)
99    }
100}
101
102impl<Wr: Debug, F> Debug for MapErr<Wr, F> {
103    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
104        f.debug_struct("MapErr")
105            .field("writer", &self.writer)
106            .field("f", &"impl FnMut(Wr::Error) -> E")
107            .finish()
108    }
109}