use std::sync::Arc;
use whasher::GxPapayaMap;
use super::subscriber::PubSubSink;
pub type PatternSubscriberSet = GxPapayaMap<u64, Arc<dyn PubSubSink>>;
pub struct PatternSubscriptionEntry {
pub pattern: Box<[u8]>,
pub subscriptions: PatternSubscriberSet,
}
impl PatternSubscriptionEntry {
pub fn new(pattern: Box<[u8]>) -> Self {
Self {
pattern,
subscriptions: GxPapayaMap::default(),
}
}
pub fn equals(&self, other: &Self) -> bool {
self.pattern == other.pattern
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use super::*;
#[test]
fn entry_holds_pattern_and_set() {
let entry = PatternSubscriptionEntry::new(Box::from(b"a*".as_slice()));
assert!(entry.equals(&PatternSubscriptionEntry::new(b"a*".as_slice().into())));
assert!(entry.subscriptions.is_empty());
let sink: Arc<dyn PubSubSink> = Arc::new(super::super::subscriber::PubSubMailbox::new(1));
assert!(entry.subscriptions.pin().insert(1, sink).is_none());
assert_eq!(entry.subscriptions.len(), 1);
}
}