multipart_write/write/
map_part.rs

1use 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    /// `MultipartWrite` for [`map_part`](super::MultipartWriteExt::map_part).
9    #[must_use = "futures do nothing unless polled"]
10    pub struct MapPart<Wr, F> {
11        #[pin]
12        writer: Wr,
13        f: F,
14    }
15}
16
17impl<Wr, F> MapPart<Wr, F> {
18    pub(super) fn new(writer: Wr, f: F) -> Self {
19        Self { writer, f }
20    }
21
22    /// Acquires a reference to the underlying writer.
23    pub fn get_ref(&self) -> &Wr {
24        &self.writer
25    }
26
27    /// Acquires a mutable reference to the underlying writer.
28    ///
29    /// It is inadvisable to directly write to the underlying writer.
30    pub fn get_mut(&mut self) -> &mut Wr {
31        &mut self.writer
32    }
33
34    /// Acquires a pinned mutable reference to the underlying writer.
35    ///
36    /// It is inadvisable to directly write to the underlying writer.
37    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<U> for MapPart<Wr, F>
43where
44    Wr: FusedMultipartWrite<Part>,
45    F: FnMut(U) -> Result<Part, Wr::Error>,
46{
47    fn is_terminated(&self) -> bool {
48        self.writer.is_terminated()
49    }
50}
51
52impl<U, Wr, F, Part> MultipartWrite<U> for MapPart<Wr, F>
53where
54    Wr: MultipartWrite<Part>,
55    F: FnMut(U) -> Result<Part, Wr::Error>,
56{
57    type Ret = Wr::Ret;
58    type Output = Wr::Output;
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>, it: U) -> Result<Self::Ret, Self::Error> {
66        let this = self.project();
67        let part = (this.f)(it)?;
68        this.writer.start_send(part)
69    }
70
71    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
72        self.project().writer.poll_flush(cx)
73    }
74
75    fn poll_complete(
76        mut self: Pin<&mut Self>,
77        cx: &mut Context<'_>,
78    ) -> Poll<Result<Self::Output, Self::Error>> {
79        self.as_mut().project().writer.poll_complete(cx)
80    }
81}
82
83impl<Wr: Debug, F> Debug for MapPart<Wr, F> {
84    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
85        f.debug_struct("MapPart")
86            .field("writer", &self.writer)
87            .field("f", &"F")
88            .finish()
89    }
90}