EventBus

Struct EventBus 

Source
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>

Source

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}
Source

pub fn add_subscriber_shared( &self, subscriber: Arc<Mutex<dyn Subscriber<ContentType, TopicId>>>, )

Source

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}
Source

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}
Source

pub fn publish( &self, event: ContentType, topic_id: Option<TopicId>, source_id: u64, )

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.