multipart_write/write/
map_err.rs

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