Skip to main content

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