Skip to main content

multipart_write/write/
map_ok.rs

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