diff options
| author | Brad Clawsie <brad@b7j0c.org> | 2026-11-30 12:29:49 -0600 |
|---|---|---|
| committer | Brad Clawsie <brad@b7j0c.org> | 2026-11-30 12:29:49 -0600 |
| commit | eb9708940fac496b358828c24360441419c65932 (patch) | |
| tree | 2f92f291d870fa13e7363b2cde52bbfa276863cd /learn_22/src | |
| parent | d05cfa7872d6edd0a1b6bd6a9119f7d0e9899660 (diff) | |
fix: learn_22
commit 9065327f3b0773bb0309eae13c004aade2242f2b
Author: Brad Clawsie <brad@b7j0c.org>
Date: Mon Nov 30 12:28:57 2026 -0600
fix: record event state
commit a726ae04699b78a48a27e26999112f5d2c0cdaef
Author: Brad Clawsie <brad@b7j0c.org>
Date: Mon Nov 30 11:38:13 2026 -0600
fix: learn_22 SubscriptionId
commit 70bcd610ff5eaa3684aaea97938344c53d3eb3e1
Author: Brad Clawsie <brad@b7j0c.org>
Date: Mon Nov 30 10:53:34 2026 -0600
feat: learn_22
commit b6edc59df72940dd7490febb50c69e0b10e19b54
Author: Brad Clawsie <brad@b7j0c.org>
Date: Mon Nov 30 09:36:24 2026 -0600
wip: learn_22
commit fdc8b9e606688cac964822220c8610347b05c644
Author: Brad Clawsie <brad@b7j0c.org>
Date: Sun Nov 29 14:46:10 2026 -0600
wip: learn_22
commit 73fa6541d47995adf25a2cbc82f2b2feae69488d
Author: Brad Clawsie <brad@b7j0c.org>
Date: Sun Nov 29 14:30:29 2026 -0600
wip: learn_22
commit 57398e51bbd90024ebd1f7e6a25ca4432fd2e074
Author: Brad Clawsie <brad@b7j0c.org>
Date: Wed Nov 25 15:32:21 2026 -0600
wip: learn_22
Diffstat (limited to 'learn_22/src')
| -rw-r--r-- | learn_22/src/event.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/learn_22/src/event.rs b/learn_22/src/event.rs index b9fb3bc..57ca43a 100644 --- a/learn_22/src/event.rs +++ b/learn_22/src/event.rs @@ -1,3 +1,4 @@ +<<<<<<< HEAD use std::collections::BTreeMap; type SubscriptionId = u32; @@ -51,3 +52,76 @@ impl EventBus { self.listeners.len() } } +||||||| dfab0ca +======= +use std::collections::BTreeMap; +use std::ops::AddAssign; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub struct SubscriptionId(u32); + +impl AddAssign for SubscriptionId { + fn add_assign(&mut self, other: Self) { + *self = Self(self.0 + other.0); + } +} + +impl From<u32> for SubscriptionId { + fn from(item: u32) -> Self { + Self(item) + } +} + +type Listener = Box<dyn FnMut(&Event) + 'static>; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Event { + UserCreated { id: u64, name: String }, + UserDeleted { id: u64 }, + MessageReceived { from: u64, text: String }, +} + +pub struct EventBus { + listeners: BTreeMap<SubscriptionId, Listener>, + subscription_id: SubscriptionId, +} + +impl EventBus { + pub fn new() -> Self { + Self { + listeners: BTreeMap::new(), + subscription_id: SubscriptionId(0), + } + } + + pub fn subscribe<F>(&mut self, callback: F) -> SubscriptionId + where + F: FnMut(&Event) + 'static, + { + if self.subscription_id == SubscriptionId::from(u32::MAX) { + panic!("subscription id overflow"); + } + let subscription_id = self.subscription_id; + self.listeners.insert(subscription_id, Box::new(callback)); + self.subscription_id += SubscriptionId::from(1); + subscription_id + } + + pub fn unsubscribe(&mut self, id: SubscriptionId) -> bool { + match self.listeners.remove(&id) { + Some(_) => true, + _ => false, + } + } + + pub fn publish(&mut self, event: Event) { + for (_, callback) in &mut self.listeners { + callback(&event); + } + } + + pub fn subscriber_count(&self) -> usize { + self.listeners.len() + } +} +>>>>>>> learn_22 |
