pubsub-bus

Thread-safe one-to-many event system. Simple and easy to use. It just works (hopefully).
Whaty it does (Without words)

Quick Start
1. Add the dependency to your Cargo.toml
eventing-rs = "1.0.0"
2. Create your events and a bus
pub enum Commands {
Atack { player_id: u32 },
Move { player_id: u32, x: f32, y: f32 },
}
let mut bus: Shared<EventBus<Commands>> = EventBus::new().into_shared();
3. Implement the Subscriber trait for your struct and subscribe it to the bus
impl Subscriber<Commands> for Player {
fn on_event(&mut self, event: &Event<Commands>) {
}
}
...
let player = Player { id: 1 }.into_shared();
bus.with(|b| {
b.subscribe(player);
});
4. Create a Publisher and pass the bus to it
pub struct Input {
publisher: Publisher<Commands>,
}
impl Input {
pub fn new(bus: Shared<EventBus<Commands>>) -> Self {
Self {
publisher: Publisher::new(bus),
}
}
...
}
5. Send events
impl Input {
pub fn send_move(&self, player_id: u32, x: f32, y: f32) {
self.publisher.publish(Event::new(Commands::Move { player_id, x, y }));
}
}
Examples
The following example demonstrates how to exchange events between players and an input system.
fn main() {
let bus = EventBus::new().into_shared();
let player1 = Player { id: 1 }.into_shared();
let player2 = Player { id: 2 }.into_shared();
bus.with(|b| {
b.subscribe(player1);
b.subscribe(player2)
});
let input = Input::new(bus.clone());
input.send_move(1, 1.0, 2.0);
input.send_atack(2);
}
For the full example, see the examples/basic_game_events directory.