use super::Subscription;
pub struct TupleSubscription<U1, U2> {
unsub1: U1,
unsub2: U2,
}
impl<U1, U2> TupleSubscription<U1, U2> {
pub fn new(unsub1: U1, unsub2: U2) -> Self { TupleSubscription { unsub1, unsub2 } }
}
impl<U1, U2> Subscription for TupleSubscription<U1, U2>
where
U1: Subscription,
U2: Subscription,
{
fn unsubscribe(self) {
self.unsub1.unsubscribe();
self.unsub2.unsubscribe();
}
fn is_closed(&self) -> bool {
self.unsub1.is_closed() && self.unsub2.is_closed()
}
}
#[cfg(test)]
mod tests {
use std::{cell::RefCell, rc::Rc};
use super::*;
struct MockSubscription {
closed: Rc<RefCell<bool>>,
}
impl MockSubscription {
fn new() -> (Self, Rc<RefCell<bool>>) {
let closed = Rc::new(RefCell::new(false));
(Self { closed: closed.clone() }, closed)
}
}
impl Subscription for MockSubscription {
fn unsubscribe(self) { *self.closed.borrow_mut() = true; }
fn is_closed(&self) -> bool { *self.closed.borrow() }
}
#[rxrust_macro::test]
fn test_tuple_subscription() {
let (mock1, closed1) = MockSubscription::new();
let (mock2, closed2) = MockSubscription::new();
let tuple_sub = TupleSubscription::new(mock1, mock2);
assert!(!tuple_sub.is_closed());
tuple_sub.unsubscribe();
assert!(*closed1.borrow());
assert!(*closed2.borrow());
}
}