multipart_write/write/
map_ok.rs1use 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 #[must_use = "futures do nothing unless polled"]
11 pub struct MapOk<Wr, Part, T, F> {
12 #[pin]
13 writer: Wr,
14 f: F,
15 _p: PhantomData<fn(Part) -> T>,
16 }
17}
18
19impl<Wr, Part, T, F> MapOk<Wr, Part, T, F> {
20 pub(super) fn new(writer: Wr, f: F) -> Self {
21 Self { writer, f, _p: PhantomData }
22 }
23
24 pub fn into_inner(self) -> Wr {
26 self.writer
27 }
28
29 pub fn get_ref(&self) -> &Wr {
31 &self.writer
32 }
33
34 pub fn get_mut(&mut self) -> &mut Wr {
38 &mut self.writer
39 }
40
41 pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
45 self.project().writer
46 }
47}
48
49impl<Wr, Part, T, F> FusedMultipartWrite<Part> for MapOk<Wr, Part, T, F>
50where
51 Wr: FusedMultipartWrite<Part>,
52 F: FnMut(Wr::Output) -> T,
53{
54 fn is_terminated(&self) -> bool {
55 self.writer.is_terminated()
56 }
57}
58
59impl<Wr, Part, T, F> MultipartWrite<Part> for MapOk<Wr, Part, T, F>
60where
61 Wr: MultipartWrite<Part>,
62 F: FnMut(Wr::Output) -> T,
63{
64 type Error = Wr::Error;
65 type Output = T;
66 type Recv = Wr::Recv;
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 self: Pin<&mut Self>,
77 part: Part,
78 ) -> Result<Self::Recv, Self::Error> {
79 self.project().writer.start_send(part)
80 }
81
82 fn poll_flush(
83 self: Pin<&mut Self>,
84 cx: &mut Context<'_>,
85 ) -> Poll<Result<(), Self::Error>> {
86 self.project().writer.poll_flush(cx)
87 }
88
89 fn poll_complete(
90 mut self: Pin<&mut Self>,
91 cx: &mut Context<'_>,
92 ) -> Poll<Result<Self::Output, Self::Error>> {
93 self.as_mut()
94 .project()
95 .writer
96 .poll_complete(cx)
97 .map_ok(self.as_mut().project().f)
98 }
99}
100
101impl<Wr: Debug, Part, T, F> Debug for MapOk<Wr, Part, T, F> {
102 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
103 f.debug_struct("MapOk").field("writer", &self.writer).finish()
104 }
105}