another_rxrust/
subscription.rs

1use crate::internals::function_wrapper::*;
2
3#[derive(Clone)]
4pub struct Subscription<'a> {
5  fn_unsubscribe: FunctionWrapper<'a, (), ()>,
6  fn_is_subscribed: FunctionWrapper<'a, (), bool>,
7}
8
9impl<'a> Subscription<'a> {
10  pub fn new<Unsub, Issub>(unsub: Unsub, issub: Issub) -> Subscription<'a>
11  where
12    Unsub: Fn() + Send + Sync + 'a,
13    Issub: Fn() -> bool + Send + Sync + 'a,
14  {
15    Subscription {
16      fn_unsubscribe: FunctionWrapper::new(move |_| unsub()),
17      fn_is_subscribed: FunctionWrapper::new(move |_| issub()),
18    }
19  }
20  pub fn unsubscribe(&self) {
21    self.fn_unsubscribe.call_and_clear_if_available(());
22  }
23  pub fn is_subscribed(&self) -> bool {
24    if let Some(x) = self.fn_is_subscribed.call_if_available(()) {
25      x
26    } else {
27      false
28    }
29  }
30}