multipart_write/write/
map_err.rs1use std::fmt::{self, Debug, Formatter};
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use crate::{FusedMultipartWrite, MultipartWrite};
6
7pin_project_lite::pin_project! {
8 #[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 pub fn into_inner(self) -> Wr {
24 self.writer
25 }
26
27 pub fn get_ref(&self) -> &Wr {
29 &self.writer
30 }
31
32 pub fn get_mut(&mut self) -> &mut Wr {
36 &mut self.writer
37 }
38
39 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 Error = E;
63 type Output = Wr::Output;
64 type Recv = Wr::Recv;
65
66 fn poll_ready(
67 mut self: Pin<&mut Self>,
68 cx: &mut Context<'_>,
69 ) -> Poll<Result<(), Self::Error>> {
70 self.as_mut()
71 .project()
72 .writer
73 .poll_ready(cx)
74 .map_err(self.as_mut().project().f)
75 }
76
77 fn start_send(
78 mut self: Pin<&mut Self>,
79 part: Part,
80 ) -> Result<Self::Recv, Self::Error> {
81 self.as_mut()
82 .project()
83 .writer
84 .start_send(part)
85 .map_err(self.as_mut().project().f)
86 }
87
88 fn poll_flush(
89 mut self: Pin<&mut Self>,
90 cx: &mut Context<'_>,
91 ) -> Poll<Result<(), Self::Error>> {
92 self.as_mut()
93 .project()
94 .writer
95 .poll_flush(cx)
96 .map_err(self.as_mut().project().f)
97 }
98
99 fn poll_complete(
100 mut self: Pin<&mut Self>,
101 cx: &mut Context<'_>,
102 ) -> Poll<Result<Self::Output, Self::Error>> {
103 self.as_mut()
104 .project()
105 .writer
106 .poll_complete(cx)
107 .map_err(self.as_mut().project().f)
108 }
109}
110
111impl<Wr: Debug, F> Debug for MapErr<Wr, F> {
112 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
113 f.debug_struct("MapErr").field("writer", &self.writer).finish()
114 }
115}