use crate::single_threaded::inner::InnerStore;
use std::rc::{Rc as Arc, Weak};
pub type SubscriberId = usize;
pub type SubscriberCallback<T> = Arc<dyn Fn(&T) + 'static>;
#[derive(Clone)]
pub struct Subscription<T: 'static> {
pub(crate) store: Weak<InnerStore<T>>,
pub(crate) id: SubscriberId,
}
impl<T: 'static> PartialEq for Subscription<T> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id && Weak::ptr_eq(&self.store, &other.store)
}
}
impl<T: 'static> Eq for Subscription<T> {}
impl<T: 'static> std::fmt::Debug for Subscription<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Subscription")
.field("id", &self.id)
.finish_non_exhaustive()
}
}
impl<T: 'static> Drop for Subscription<T> {
fn drop(&mut self) {
if let Some(store) = self.store.upgrade() {
store.unsubscribe(self.id);
}
}
}