Skip to main content

trillium_http/
after_send.rs

1#[derive(Debug, Clone, Copy, Eq, PartialEq)]
2pub enum SendStatus {
3    Success,
4    Failure,
5}
6impl From<bool> for SendStatus {
7    fn from(success: bool) -> Self {
8        if success {
9            Self::Success
10        } else {
11            Self::Failure
12        }
13    }
14}
15
16impl SendStatus {
17    pub fn is_success(self) -> bool {
18        SendStatus::Success == self
19    }
20}
21
22#[derive(Default)]
23pub(crate) struct AfterSend(Option<Box<dyn FnOnce(SendStatus) + Send + Sync + 'static>>);
24
25impl AfterSend {
26    pub(crate) fn call(&mut self, send_status: SendStatus) {
27        if let Some(after_send) = self.0.take() {
28            after_send(send_status);
29        }
30    }
31
32    pub(crate) fn append<F>(&mut self, after_send: F)
33    where
34        F: FnOnce(SendStatus) + Send + Sync + 'static,
35    {
36        self.0 = Some(match self.0.take() {
37            Some(existing_after_send) => Box::new(move |ss| {
38                existing_after_send(ss);
39                after_send(ss);
40            }),
41            None => Box::new(after_send),
42        });
43    }
44}
45
46impl Drop for AfterSend {
47    fn drop(&mut self) {
48        self.call(SendStatus::Failure);
49    }
50}