I have an observable collection and an observer. I want the observer to be a trait implementation of trait Observer. The observable object should be able to not
I used a callback function. It's simple and powerful and there are no lifetime issues or type erasure.
I tried Weak, but
Rcpub struct Notifier {
subscribers: Vec>,
}
impl Notifier {
pub fn new() -> Notifier {
Notifier {
subscribers: Vec::new(),
}
}
pub fn register(&mut self, callback: F)
where
F: 'static + Fn(&E),
{
self.subscribers.push(Box::new(callback));
}
pub fn notify(&self, event: E) {
for callback in &self.subscribers {
callback(&event);
}
}
}