multipart_write/write/
fuse.rs

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