multipart_write/write/
map_ret.rs

1use crate::{FusedMultipartWrite, MultipartWrite};
2
3use std::pin::Pin;
4use std::task::{Context, Poll};
5
6/// `MultipartWrite` for [`map_ret`](super::MultipartWriteExt::map_ret).
7#[derive(Debug)]
8#[must_use = "futures do nothing unless polled"]
9#[pin_project::pin_project]
10pub struct MapRet<Wr, F> {
11    #[pin]
12    writer: Wr,
13    f: F,
14}
15
16impl<Wr, F> MapRet<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 MapRet<Wr, F>
42where
43    Wr: FusedMultipartWrite<Part>,
44    F: FnMut(Wr::Ret) -> U,
45{
46    fn is_terminated(&self) -> bool {
47        self.writer.is_terminated()
48    }
49}
50
51impl<U, Wr, F, Part> MultipartWrite<Part> for MapRet<Wr, F>
52where
53    Wr: MultipartWrite<Part>,
54    F: FnMut(Wr::Ret) -> U,
55{
56    type Ret = U;
57    type Output = Wr::Output;
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(mut self: Pin<&mut Self>, part: Part) -> Result<Self::Ret, Self::Error> {
65        self.as_mut()
66            .project()
67            .writer
68            .start_send(part)
69            .map(self.as_mut().project().f)
70    }
71
72    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
73        self.project().writer.poll_flush(cx)
74    }
75
76    fn poll_complete(
77        self: Pin<&mut Self>,
78        cx: &mut Context<'_>,
79    ) -> Poll<Result<Self::Output, Self::Error>> {
80        self.project().writer.poll_complete(cx)
81    }
82}