summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrad Clawsie <brad@b7j0c.org>2026-11-30 10:58:44 -0600
committerBrad Clawsie <brad@b7j0c.org>2026-11-30 10:58:44 -0600
commitd05cfa7872d6edd0a1b6bd6a9119f7d0e9899660 (patch)
tree58d0983f028e534a799ae3b6fd554792602b85a9
parentdfab0cabdac6603176ab4a5ec58e56d535ceb93b (diff)
feat: learn_22
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
-rw-r--r--learn_22/Cargo.lock7
-rw-r--r--learn_22/Cargo.toml6
-rw-r--r--learn_22/src/event.rs53
-rw-r--r--learn_22/src/lib.rs1
-rw-r--r--learn_22/src/main.rs3
-rw-r--r--learn_22/tests/event.rs168
6 files changed, 238 insertions, 0 deletions
diff --git a/learn_22/Cargo.lock b/learn_22/Cargo.lock
new file mode 100644
index 0000000..e461009
--- /dev/null
+++ b/learn_22/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "learn_22"
+version = "0.1.0"
diff --git a/learn_22/Cargo.toml b/learn_22/Cargo.toml
new file mode 100644
index 0000000..170e7b8
--- /dev/null
+++ b/learn_22/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "learn_22"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
diff --git a/learn_22/src/event.rs b/learn_22/src/event.rs
new file mode 100644
index 0000000..b9fb3bc
--- /dev/null
+++ b/learn_22/src/event.rs
@@ -0,0 +1,53 @@
+use std::collections::BTreeMap;
+
+type SubscriptionId = u32;
+
+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: 0,
+ }
+ }
+
+ pub fn subscribe<F>(&mut self, callback: F) -> SubscriptionId
+ where
+ F: FnMut(&Event) + 'static,
+ {
+ let subscription_id = self.subscription_id;
+ self.listeners.insert(subscription_id, Box::new(callback));
+ self.subscription_id += 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()
+ }
+}
diff --git a/learn_22/src/lib.rs b/learn_22/src/lib.rs
new file mode 100644
index 0000000..53f1126
--- /dev/null
+++ b/learn_22/src/lib.rs
@@ -0,0 +1 @@
+pub mod event;
diff --git a/learn_22/src/main.rs b/learn_22/src/main.rs
new file mode 100644
index 0000000..db91fc6
--- /dev/null
+++ b/learn_22/src/main.rs
@@ -0,0 +1,3 @@
+fn main() {
+ ()
+}
diff --git a/learn_22/tests/event.rs b/learn_22/tests/event.rs
new file mode 100644
index 0000000..e34016e
--- /dev/null
+++ b/learn_22/tests/event.rs
@@ -0,0 +1,168 @@
+use std::cell::{Cell, RefCell};
+use std::rc::Rc;
+use learn_22::event::{Event, EventBus};
+
+#[test]
+fn test_new() {
+ let eb = EventBus::new();
+ assert_eq!(eb.subscriber_count(), 0);
+}
+
+#[test]
+fn test_subscribe() {
+ let mut eb = EventBus::new();
+ let mut count: u32 = 0;
+
+ let subscription_id0 = eb.subscribe(move |event| {
+ count += 1;
+ });
+ assert_eq!(subscription_id0, 0);
+ assert_eq!(eb.subscriber_count(), 1);
+
+ let subscription_id1 = eb.subscribe(move |event| {
+ count += 1;
+ });
+ assert_eq!(subscription_id1, 1);
+ assert_eq!(eb.subscriber_count(), 2);
+}
+
+#[test]
+fn test_unsubscribe() {
+ let mut eb = EventBus::new();
+ let mut count: u32 = 0;
+ let subscription_id0 = eb.subscribe(move |event| {
+ count += 1;
+ });
+ assert_eq!(subscription_id0, 0);
+ assert_eq!(eb.subscriber_count(), 1);
+
+ let subscription_id1 = eb.subscribe(move |event| {
+ count += 1;
+ });
+ assert_eq!(subscription_id1, 1);
+ assert_eq!(eb.subscriber_count(), 2);
+ assert!(!eb.unsubscribe(3)); // not found
+ assert_eq!(eb.subscriber_count(), 2);
+ assert!(eb.unsubscribe(0));
+ assert_eq!(eb.subscriber_count(), 1);
+ assert!(eb.unsubscribe(1));
+ assert_eq!(eb.subscriber_count(), 0);
+}
+
+#[test]
+fn test_publish() {
+ let mut eb = EventBus::new();
+ let count = Rc::new(Cell::new(0u32));
+
+ let c0 = Rc::clone(&count);
+ let subscription_id0 = eb.subscribe(move |event| {
+ c0.set(c0.get() + 1);
+ });
+ assert_eq!(subscription_id0, 0);
+ assert_eq!(eb.subscriber_count(), 1);
+
+ let c1 = Rc::clone(&count);
+ let subscription_id1 = eb.subscribe(move |event| {
+ c1.set(c1.get() + 2);
+ });
+ assert_eq!(subscription_id1, 1);
+ assert_eq!(eb.subscriber_count(), 2);
+
+ let ev0 = Event::UserCreated{id:0, name:"0".to_string()};
+ eb.publish(ev0);
+ assert_eq!(count.get(), 3);
+}
+
+#[test]
+fn test_accept() {
+ // new bus
+ let mut eb = EventBus::new();
+ assert_eq!(eb.subscriber_count(), 0);
+
+ let count = Rc::new(Cell::new(0u32));
+ let ordering = Rc::new(RefCell::new(Vec::<u32>::new()));
+
+ // subscribe one callback
+ let c0 = Rc::clone(&count);
+ let o0 = Rc::clone(&ordering);
+ let mut subscription_id0 = eb.subscribe(move |event| {
+ c0.set(c0.get() + 1);
+ o0.borrow_mut().push(0);
+ });
+ assert_eq!(subscription_id0, 0);
+ assert_eq!(eb.subscriber_count(), 1);
+
+ // publish one event
+ eb.publish(Event::UserCreated{id:0, name:"0".to_string()});
+ // before: count = 0, so count = 1 is proof of callback 0 execution
+ assert_eq!(count.get(), 1);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0]);
+
+ // publish multiple events
+ eb.publish(Event::UserCreated{id:1, name:"1".to_string()});
+ eb.publish(Event::UserCreated{id:2, name:"2".to_string()});
+ eb.publish(Event::UserCreated{id:3, name:"3".to_string()});
+ // before: count = 1, so count = 4 is proof of callback 0 execution
+ assert_eq!(count.get(), 4);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0]);
+
+ // subscribe another callback
+ let c1 = Rc::clone(&count);
+ let o1 = Rc::clone(&ordering);
+ let subscription_id1 = eb.subscribe(move |event| {
+ c1.set(c1.get() + 2);
+ o1.borrow_mut().push(1);
+ });
+ assert_eq!(subscription_id1, 1);
+ assert_eq!(eb.subscriber_count(), 2);
+
+ eb.publish(Event::UserCreated{id:4, name:"4".to_string()});
+ // before: count = 4, so count = 7 is proof of callbacks 0,1 execution
+ assert_eq!(count.get(), 7);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0,0,1]);
+
+ // unsubscribe existing callback id
+ assert!(eb.unsubscribe(subscription_id0));
+ eb.publish(Event::UserCreated{id:5, name:"5".to_string()});
+ // before: count = 7, so count = 9 is proof of callback 1 execution
+ assert_eq!(count.get(), 9);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0,0,1,1]);
+
+ // unsubscribe unknown id
+ assert!(!eb.unsubscribe(3));
+
+ // unsubscribe previously unsubscribed id
+ assert!(!eb.unsubscribe(subscription_id0));
+
+ // subscribe after unsubscribe
+ let c2 = Rc::clone(&count);
+ let o2 = Rc::clone(&ordering);
+ subscription_id0 = eb.subscribe(move |event| {
+ c2.set(c2.get() + 1);
+ o2.borrow_mut().push(2);
+ });
+ assert_eq!(subscription_id0, 2);
+ assert_eq!(eb.subscriber_count(), 2);
+
+ eb.publish(Event::UserCreated{id:6, name:"6".to_string()});
+ // before: count = 9, so count = 12 is proof of callbacks 1,2 execution
+ assert_eq!(count.get(), 12);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0,0,1,1,1,2]);
+
+ // unsubscribe all callbacks and demonstrate state unchanged
+ assert!(eb.unsubscribe(subscription_id0));
+ assert_eq!(eb.subscriber_count(), 1);
+ assert!(eb.unsubscribe(subscription_id1));
+ assert_eq!(eb.subscriber_count(), 0);
+
+ eb.publish(Event::UserCreated{id:7, name:"7".to_string()});
+ // before: count = 12, so count = 12 is proof of no callbacks
+ assert_eq!(count.get(), 12);
+ // order of registered callbacks is honored
+ assert_eq!(*ordering.borrow(), vec![0,0,0,0,0,1,1,1,2]);
+}