Anyways, that's not possible, because GlobalState should store references to ShortLivingObjects. The only idea I have is to wrap every object in Rc<RefCell>> and this will allow me to store weak references in GlobalState, but this feels so inconvenient.
struct Event {
data: String,
}
type Handler = Box<dyn FnMut(&Event) -> Result<(), Box<dyn std::error::Error>>>;
fn handler2(_event: &Event) -> Result<(), Box<dyn std::error::Error>> {
Err("failed to process event".into())
}
fn main() {
let mut handlers: Vec<Handler> = vec![
Box::new(|event: &Event| {
println!("Received {}", event.data);
Ok(())
}),
Box::new(handler2)
];
let event = Event {
data: "Some event".to_owned(),
};
for handler in &mut handlers {
handler(&event).expect("event handler failed");
}
}
I made some assumptions about what you might want from your event handlers. Depending on the context you may be able to get away with something simpler.
If your question is not actually how to implement event handlers/subscribing to events but how to share the event handlers throughout a program, consider using Rc<RefCell<Handlers>> or Arc<Mutex<Handlers>> in multi-threaded/async code.