Skip to main content

multipart_write/write/
fuse.rs

1use std::fmt::{self, Debug, Formatter};
2use std::marker::PhantomData;
3use std::pin::Pin;
4use std::task::{Context, Poll};
5
6use futures_core::ready;
7
8use crate::{FusedMultipartWrite, MultipartWrite};
9
10pin_project_lite::pin_project! {
11    /// `MultipartWrite` for [`fuse`](super::MultipartWriteExt::fuse).
12    #[must_use = "futures do nothing unless polled"]
13    pub struct Fuse<Wr, Part, F> {
14        #[pin]
15        writer: Wr,
16        f: F,
17        is_terminated: bool,
18        _p: PhantomData<Part>,
19    }
20}
21
22impl<Wr, Part, F> Fuse<Wr, Part, F> {
23    pub(super) fn new(writer: Wr, f: F) -> Self {
24        Self { writer, f, is_terminated: false, _p: PhantomData }
25    }
26
27    /// Consumes `Fuse`, returning the underlying writer.
28    pub fn into_inner(self) -> Wr {
29        self.writer
30    }
31
32    /// Acquires a reference to the underlying writer.
33    pub fn get_ref(&self) -> &Wr {
34        &self.writer
35    }
36
37    /// Acquires a mutable reference to the underlying writer.
38    ///
39    /// It is inadvisable to directly write to the underlying writer.
40    pub fn get_mut(&mut self) -> &mut Wr {
41        &mut self.writer
42    }
43
44    /// Acquires a pinned mutable reference to the underlying writer.
45    ///
46    /// It is inadvisable to directly write to the underlying writer.
47    pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut Wr> {
48        self.project().writer
49    }
50}
51
52impl<Wr, Part, F> FusedMultipartWrite<Part> for Fuse<Wr, Part, F>
53where
54    Wr: MultipartWrite<Part>,
55    F: FnMut(&Wr::Output) -> bool,
56{
57    fn is_terminated(&self) -> bool {
58        self.is_terminated
59    }
60}
61
62impl<Wr, Part, F> MultipartWrite<Part> for Fuse<Wr, Part, F>
63where
64    Wr: MultipartWrite<Part>,
65    F: FnMut(&Wr::Output) -> bool,
66{
67    type Error = Wr::Error;
68    type Output = Option<Wr::Output>;
69    type Recv = Option<Wr::Recv>;
70
71    fn poll_ready(
72        self: Pin<&mut Self>,
73        cx: &mut Context<'_>,
74    ) -> Poll<Result<(), Self::Error>> {
75        if self.is_terminated {
76            return Poll::Ready(Ok(()));
77        }
78        self.project().writer.as_mut().poll_ready(cx)
79    }
80
81    fn start_send(
82        self: Pin<&mut Self>,
83        part: Part,
84    ) -> Result<Self::Recv, Self::Error> {
85        if self.is_terminated {
86            return Ok(None);
87        }
88        self.project().writer.as_mut().start_send(part).map(Some)
89    }
90
91    fn poll_flush(
92        self: Pin<&mut Self>,
93        cx: &mut Context<'_>,
94    ) -> Poll<Result<(), Self::Error>> {
95        if self.is_terminated {
96            return Poll::Ready(Ok(()));
97        }
98        self.project().writer.as_mut().poll_flush(cx)
99    }
100
101    fn poll_complete(
102        self: Pin<&mut Self>,
103        cx: &mut Context<'_>,
104    ) -> Poll<Result<Self::Output, Self::Error>> {
105        let mut this = self.project();
106        if *this.is_terminated {
107            return Poll::Ready(Ok(None));
108        }
109        let res = ready!(this.writer.as_mut().poll_complete(cx))?;
110        if (this.f)(&res) {
111            *this.is_terminated = true;
112        }
113        Poll::Ready(Ok(Some(res)))
114    }
115}
116
117impl<Wr, Part, F> Debug for Fuse<Wr, Part, F>
118where
119    Wr: Debug,
120{
121    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
122        f.debug_struct("Fuse")
123            .field("writer", &self.writer)
124            .field("is_terminated", &self.is_terminated)
125            .finish()
126    }
127}