Publisher

Trait Publisher 

Source
pub trait Publisher<ContentType, TopicId: PartialEq + Clone> {
    // Required method
    fn get_mut_emitter(&mut self) -> &mut EventEmitter<ContentType, TopicId>;

    // Provided method
    fn publish(&mut self, content: ContentType, topic_id: Option<TopicId>) { ... }
}
Expand description

Publisher is a trait that defines a publisher to the event bus. Publisher is expected to care an EventEmitter.

Required Methods§

Source

fn get_mut_emitter(&mut self) -> &mut EventEmitter<ContentType, TopicId>

Get the emitter of the publisher. Has to be implemented by the publisher.

Provided Methods§

Source

fn publish(&mut self, content: ContentType, topic_id: Option<TopicId>)

Default implementation to Publish an event to the event bus. If topic_id is None, the event will be sent to all subscribers.

Examples found in repository?
examples/basic_game_events/main.rs (line 26)
12fn main() {
13    // Create a bus
14    let bus: EventBus<Commands, TopicIds> = EventBus::new();
15
16    // Create players, input, and attach to the bus
17    let player1 = Player { id: 1 };
18    let player2 = Player { id: 2 };
19    let mut input = Input::new();
20
21    bus.add_subscriber(player1);
22    bus.add_subscriber(player2);
23    bus.add_publisher(&mut input, Some(85)).unwrap();
24
25    // Send some events
26    input.publish(Commands::Move { dx: 1.0, dy: 2.0 }, Some(TopicIds::Player2));
27    input.publish(Commands::Move { dx: 1.0, dy: 2.0 }, Some(TopicIds::Player1));
28    input.publish(Commands::Atack, Some(TopicIds::Player2));
29    input.publish(Commands::Atack, Some(TopicIds::Player1));
30}

Implementors§