multipart_write/write/
map_ok.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 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 pub fn into_inner(self) -> Wr {
24 self.writer
25 }
26
27 pub fn get_ref(&self) -> &Wr {
29 &self.writer
30 }
31
32 pub fn get_mut(&mut self) -> &mut Wr {
36 &mut self.writer
37 }
38
39 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 Ret = Wr::Ret;
63 type Output = U;
64 type Error = Wr::Error;
65
66 fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
67 self.project().writer.poll_ready(cx)
68 }
69
70 fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
71 self.project().writer.start_send(part)
72 }
73
74 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
75 self.project().writer.poll_flush(cx)
76 }
77
78 fn poll_complete(
79 mut self: Pin<&mut Self>,
80 cx: &mut Context<'_>,
81 ) -> Poll<Result<Self::Output, Self::Error>> {
82 self.as_mut()
83 .project()
84 .writer
85 .poll_complete(cx)
86 .map_ok(self.as_mut().project().f)
87 }
88}
89
90impl<Wr: Debug, F> Debug for MapOk<Wr, F> {
91 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
92 f.debug_struct("MapOk")
93 .field("writer", &self.writer)
94 .field("f", &"impl FnMut(Wr::Output) -> U")
95 .finish()
96 }
97}