Skip to main content

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