pub struct EventBus<ContentType, TopicId: PartialEq + Clone> { /* private fields */ }Expand description
The Event Bus itself. Add subscribers and publishers to it.
Implementations§
Source§impl<ContentType, TopicId: PartialEq + Clone> EventBus<ContentType, TopicId>
impl<ContentType, TopicId: PartialEq + Clone> EventBus<ContentType, TopicId>
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/basic_game_events/main.rs (line 14)
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}Sourcepub fn add_subscriber<S>(&self, subscriber: S)where
S: Subscriber<ContentType, TopicId> + 'static,
pub fn add_subscriber<S>(&self, subscriber: S)where
S: Subscriber<ContentType, TopicId> + 'static,
Examples found in repository?
examples/basic_game_events/main.rs (line 21)
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}Sourcepub fn add_publisher(
&self,
publisher: &mut dyn Publisher<ContentType, TopicId>,
source_id: Option<u64>,
) -> Result<(), &'static str>
pub fn add_publisher( &self, publisher: &mut dyn Publisher<ContentType, TopicId>, source_id: Option<u64>, ) -> Result<(), &'static str>
Add a publisher to the event bus. If source_id is None, the publisher will be assigned a unique id.
Examples found in repository?
examples/basic_game_events/main.rs (line 23)
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}pub fn publish( &self, event: ContentType, topic_id: Option<TopicId>, source_id: u64, )
pub fn get_internal(&self) -> Arc<EventBusInternal<ContentType, TopicId>>
Auto Trait Implementations§
impl<ContentType, TopicId> Freeze for EventBus<ContentType, TopicId>
impl<ContentType, TopicId> RefUnwindSafe for EventBus<ContentType, TopicId>
impl<ContentType, TopicId> Send for EventBus<ContentType, TopicId>
impl<ContentType, TopicId> Sync for EventBus<ContentType, TopicId>
impl<ContentType, TopicId> Unpin for EventBus<ContentType, TopicId>
impl<ContentType, TopicId> UnwindSafe for EventBus<ContentType, TopicId>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more