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: TThe data that is held by the event.
Implementations§
Source§impl<T> Event<T>
impl<T> Event<T>
Sourcepub fn new(data: T) -> Event<T>
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}Trait Implementations§
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> 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