Event Observer

Implementation of observer pattern by rust.

Examples

```rust use event_observer::*; use std::cell::RefCell; use std::rc::Rc; use std::sync::{Arc, Mutex, RwLock};

enum MyEvent { A, B, }

type MySubject = Subject;

struct MyObserver { name: String, }

impl Observer for MyObserver { fn on_notify(&self, event: &MyEvent) { match event { MyEvent::A => println!("Hello {}", self.name), MyEvent::B => println!("Good bye {}", self.name), } } }

let mut subject = MySubject::new();

let o1 = MyObserver { name: "Noah".tostring() }; let o2 = Rc::new(MyObserver { name: "Olivia".tostring() }); let o3 = Rc::new(RefCell::new(MyObserver { name: "Liam".tostring() })); let o4 = Arc::new(MyObserver { name: "Emma".tostring() }); let o5 = Arc::new(Mutex::new(MyObserver { name: "Elijah".tostring() })); let o6 = Arc::new(RwLock::new(MyObserver { name: "Ava".tostring() }));

subject.addobserver(o1); subject.addrcobserver(o2); subject.addrcrefcellobserver(o3); subject.addarcobserver(o4); subject.addarcmutexobserver(o5); subject.addarcrwlockobserver(o6);

subject.notify(&MyEvent::A); subject.notify(&MyEvent::B); ```

```rust use event_observer::*; use std::cell::RefCell; use std::rc::Rc; use std::sync::{Arc, Mutex, RwLock};

enum MyEvent { X(usize), Y(usize), }

type MySubject = Subject;

struct MyState { x: usize, y: usize, }

impl MutObserver for MyState { fn on_notify(&mut self, event: &MyEvent) { match event { MyEvent::X(i) => self.x += i, MyEvent::Y(i) => self.y += i, } } }

let mut subject = MySubject::new();

let o1 = Rc::new(RefCell::new(MyState { x: 0, y: 0 })); let o2 = Arc::new(Mutex::new(MyState { x: 0, y: 0 })); let o3 = Arc::new(RwLock::new(MyState { x: 0, y: 0 }));

subject.addrcrefcellmutobserver(o1.clone()); subject.addarcmutexmutobserver(o2.clone()); subject.addarcrwlockmutobserver(o3.clone());

subject.notify(&MyEvent::X(1)); subject.notify(&MyEvent::Y(2));

asserteq!(o1.borrow().x, 1); asserteq!(o1.borrow().y, 2); asserteq!(o2.lock().unwrap().x, 1); asserteq!(o2.lock().unwrap().y, 2); asserteq!(o3.read().unwrap().x, 1); asserteq!(o3.read().unwrap().y, 2); ```