multipart_write/write/
map.rs

1use crate::{FusedMultipartWrite, MultipartWrite};
2
3use std::pin::Pin;
4use std::task::{Context, Poll};
5
6/// `MultipartWrite` for [`map`](super::MultipartWriteExt::map).
7#[derive(Debug)]
8#[must_use = "futures do nothing unless polled"]
9#[pin_project::pin_project]
10pub struct Map<Wr, F> {
11    #[pin]
12    writer: Wr,
13    f: F,
14}
15
16impl<Wr, F> Map<Wr, F> {
17    pub(super) fn new(writer: Wr, f: F) -> Self {
18        Self { writer, f }
19    }
20
21    /// Acquires a reference to the underlying writer.
22    pub fn get_ref(&self) -> &Wr {
23        &self.writer
24    }
25
26    /// Acquires a mutable reference to the underlying writer.
27    ///
28    /// It is inadvisable to directly write to the underlying writer.
29    pub fn get_mut(&mut self) -> &mut Wr {
30        &mut self.writer
31    }
32
33    /// Acquires a pinned mutable reference to the underlying writer.
34    ///
35    /// It is inadvisable to directly write to the underlying writer.
36    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
37        self.project().writer
38    }
39}
40
41impl<U, Wr, F, Part> FusedMultipartWrite<Part> for Map<Wr, F>
42where
43    Wr: FusedMultipartWrite<Part>,
44    F: FnMut(Wr::Output) -> U,
45{
46    fn is_terminated(&self) -> bool {
47        self.writer.is_terminated()
48    }
49}
50
51impl<U, Wr, F, Part> MultipartWrite<Part> for Map<Wr, F>
52where
53    Wr: MultipartWrite<Part>,
54    F: FnMut(Wr::Output) -> U,
55{
56    type Ret = Wr::Ret;
57    type Output = U;
58    type Error = Wr::Error;
59
60    fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
61        self.project().writer.poll_ready(cx)
62    }
63
64    fn start_send(self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
65        self.project().writer.start_send(part)
66    }
67
68    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
69        self.project().writer.poll_flush(cx)
70    }
71
72    fn poll_complete(
73        mut self: Pin<&mut Self>,
74        cx: &mut Context<'_>,
75    ) -> Poll<Result<Self::Output, Self::Error>> {
76        self.as_mut()
77            .project()
78            .writer
79            .poll_complete(cx)
80            .map_ok(self.as_mut().project().f)
81    }
82}