forked from SoniEx2/rust.eventbus
46 lines
973 B
Rust
46 lines
973 B
Rust
#[macro_use]
|
|
extern crate eventbus;
|
|
|
|
use eventbus::{Event, EventBus};
|
|
|
|
struct MyEvent {
|
|
i: i32
|
|
}
|
|
|
|
trait DynEvent : Event {
|
|
}
|
|
|
|
impl DynEvent for MyEvent {
|
|
}
|
|
|
|
impl Event for MyEvent {
|
|
}
|
|
|
|
fn add_handler(e: &mut MyEvent) {
|
|
/* adds 1 */
|
|
e.i += 1;
|
|
}
|
|
|
|
fn no_handler(e: &mut MyEvent) {
|
|
/* does nothing */
|
|
e.i += 0;
|
|
}
|
|
|
|
#[test]
|
|
fn test_usage_external() {
|
|
let event_bus = EventBus::new();
|
|
//let handler_id = event_bus.register(add_handler, 0);
|
|
register_hook!(&event_bus, 0, MyEvent, add_handler);
|
|
let mut event = MyEvent { i: 3 };
|
|
assert_eq!(event.i, 3);
|
|
post_event!(&event_bus, &mut event, MyEvent);
|
|
post_event!(&event_bus, &mut event, dyn DynEvent);
|
|
assert_eq!(event.i, 4);
|
|
register_hook!(&event_bus, 1, MyEvent, no_handler);
|
|
post_event!(&event_bus, &mut event, MyEvent);
|
|
assert_eq!(event.i, 5);
|
|
//event_bus.unregister(handler_id);
|
|
post_event!(&event_bus, &mut event, MyEvent);
|
|
//assert_eq!(event.i, 5);
|
|
}
|