Event

Struct Event 

Source
pub struct Event<T> {
    pub data: T,
}
Expand description

§Event

An event is a struct that can hold any data type. It is then published to the event bus. Once published, the event is then passed to each subscriber when the event bus runs.

§Fields

  • data - The data that is held by the event.

§Methods

  • new - Creates a new event.

  • get_data - Returns the data held by the event.

Fields§

§data: T

The data that is held by the event.

Implementations§

Source§

impl<T> Event<T>

Source

pub fn new(data: T) -> Event<T>

§New

Creates a new event.

Examples found in repository?
examples/01.rs (line 29)
21fn main() {
22    let mut event_bus = EventBus::new();
23
24    // We have to manually create and add each subscriber to the event bus.
25    event_bus.subscribe_listener(ExampleSubscriber::new("Subscriber 1".to_string()));
26    event_bus.subscribe_listener(ExampleSubscriber::new("Subscriber 2".to_string()));
27
28    // We can manually define an event and publish it to the event bus.
29    event_bus.publish(Event::new("hello".to_string()));
30    event_bus.publish(Event::new("world".to_string()));
31
32    // Alternatively, we can use the From trait to define an event and publish it to the event bus.
33    // This is as simple as using ".into()" on the data (so long as it matches the event bus's type parameter).
34    event_bus.publish("!".to_string().into());
35
36    // Here we show another example, this time directly from a String instead of using &str.
37    let test_string = String::from("Hello, World!");
38    event_bus.publish(test_string.into());
39
40    // Runs through each event, and calls each listener's on_event method.
41    event_bus.run();
42}
Source

pub fn get_data(&self) -> &T

§Get Data

Returns the data held by the event.

Examples found in repository?
examples/01.rs (line 17)
16    fn on_event(&mut self, event: &Event<Self::Input>) {
17        println!("{} received message: {}", self.name, event.get_data());
18    }

Trait Implementations§

Source§

impl<T> From<T> for Event<T>

Source§

fn from(data: T) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for Event<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Event<T>
where T: RefUnwindSafe,

§

impl<T> Send for Event<T>
where T: Send,

§

impl<T> Sync for Event<T>
where T: Sync,

§

impl<T> Unpin for Event<T>
where T: Unpin,

§

impl<T> UnwindSafe for Event<T>
where T: UnwindSafe,

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<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
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.