multipart_write/write/
map.rs1use crate::{FusedMultipartWrite, MultipartWrite};
2
3use std::fmt::{self, Debug, Formatter};
4use std::pin::Pin;
5use std::task::{Context, Poll};
6
7pin_project_lite::pin_project! {
8 #[must_use = "futures do nothing unless polled"]
10 pub struct Map<Wr, F> {
11 #[pin]
12 writer: Wr,
13 f: F,
14 }
15}
16
17impl<Wr, F> Map<Wr, F> {
18 pub(super) fn new(writer: Wr, f: F) -> Self {
19 Self { writer, f }
20 }
21
22 pub fn get_ref(&self) -> &Wr {
24 &self.writer
25 }
26
27 pub fn get_mut(&mut self) -> &mut Wr {
31 &mut self.writer
32 }
33
34 pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
38 self.project().writer
39 }
40}
41
42impl<U, Wr, F, Part> FusedMultipartWrite<Part> for Map<Wr, F>
43where
44 Wr: FusedMultipartWrite<Part>,
45 F: FnMut(Wr::Output) -> U,
46{
47 fn is_terminated(&self) -> bool {
48 self.writer.is_terminated()
49 }
50}
51
52impl<U, Wr, F, Part> MultipartWrite<Part> for Map<Wr, F>
53where
54 Wr: MultipartWrite<Part>,
55 F: FnMut(Wr::Output) -> U,
56{
57 type Ret = Wr::Ret;
58 type Output = U;
59 type Error = Wr::Error;
60
61 fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
62 self.project().writer.poll_ready(cx)
63 }
64
65 fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
66 self.project().writer.start_send(part)
67 }
68
69 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
70 self.project().writer.poll_flush(cx)
71 }
72
73 fn poll_complete(
74 mut self: Pin<&mut Self>,
75 cx: &mut Context<'_>,
76 ) -> Poll<Result<Self::Output, Self::Error>> {
77 self.as_mut()
78 .project()
79 .writer
80 .poll_complete(cx)
81 .map_ok(self.as_mut().project().f)
82 }
83}
84
85impl<Wr: Debug, F> Debug for Map<Wr, F> {
86 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
87 f.debug_struct("Map")
88 .field("writer", &self.writer)
89 .field("f", &"F")
90 .finish()
91 }
92}