Skip to main content

multipart_write/write/
map_err.rs

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