rust.eventbus/tests/dyn_events.rs
2018-10-28 19:43:48 -07:00

60 lines
1.2 KiB
Rust

#[macro_use]
extern crate eventbus;
use std::fmt::Debug;
use eventbus::{Event, EventBus, Cancellable};
trait MyEvent : Event + Debug {
}
trait MyExtraEvent : MyEvent + Cancellable {
}
#[derive(Debug)]
struct MyEventImpl {
i: i32
}
#[derive(Debug)]
struct MyExtraEventImpl {
i: i32
}
impl Event for MyEventImpl {
}
impl MyEvent for MyEventImpl {
}
impl Event for MyExtraEventImpl {
}
impl MyEvent for MyExtraEventImpl {
}
impl MyExtraEvent for MyExtraEventImpl {
}
impl Cancellable for MyExtraEventImpl {
}
fn on_myevent(evt: &mut dyn MyEvent) {
println!("Got event for inspection and manipulation: {:?}", evt);
}
fn on_myextraevent(evt: &mut dyn MyExtraEvent) {
println!("Got event for inspection, manipulation, and cancellation: {:?}", evt);
}
#[test]
fn test_dyn_usage() {
let event_bus = EventBus::new();
let mut event = MyEventImpl { i: 3 };
let mut extraEvent = MyExtraEventImpl { i: 4 };
//let handler_id = event_bus.register(add_handler, 0);
register_hook!(&event_bus, 0, dyn MyEvent, on_myevent);
register_hook!(&event_bus, 1, dyn MyExtraEvent, on_myextraevent);
post_event!(&event_bus, &mut event, dyn MyEvent);
post_event_cancellable!(&event_bus, &mut extraEvent, dyn MyExtraEvent, dyn MyEvent);
}