pub struct Bus { /* private fields */ }Implementations§
Source§impl Bus
impl Bus
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Examples found in repository?
examples/base.rs (line 23)
22async fn main() {
23 let bus = Bus::new(2);
24
25 // Register EventA listener
26 let handle1 = bus.bind::<EventA>(once(|_| async {
27 println!("Handled Event A");
28 }));
29
30 // Register EventB listener
31 let handle2 = bus.bind(on(|_: EventB| async {
32 println!("Handled Event B");
33 }));
34
35 // Register EventC listener
36 let handle3 = bus.bind(many(3, event_c_listener));
37
38 // Emit events
39 bus.emit(EventA).await;
40 bus.emit(EventB).await;
41 bus.emit(EventC).await;
42 bus.emit(EventD).await;
43
44 // Cancel EventB listener and register EventD listener
45 handle2.cancel();
46 tokio::time::sleep(Duration::from_millis(1)).await;
47 let handle4 = bus.bind(on(|_: EventD| async {
48 println!("Handled Event D");
49 }));
50
51 // Emit again
52 bus.emit(EventA).await;
53 bus.emit(EventB).await;
54 bus.clone().emit(EventD).await;
55
56 drop(bus);
57
58 let _ = join!(handle1, handle3, handle4);
59}Sourcepub fn bind<E: Event>(
&self,
listener: impl Listener<Event = E>,
) -> SubscribeHandle
pub fn bind<E: Event>( &self, listener: impl Listener<Event = E>, ) -> SubscribeHandle
Examples found in repository?
examples/base.rs (lines 26-28)
22async fn main() {
23 let bus = Bus::new(2);
24
25 // Register EventA listener
26 let handle1 = bus.bind::<EventA>(once(|_| async {
27 println!("Handled Event A");
28 }));
29
30 // Register EventB listener
31 let handle2 = bus.bind(on(|_: EventB| async {
32 println!("Handled Event B");
33 }));
34
35 // Register EventC listener
36 let handle3 = bus.bind(many(3, event_c_listener));
37
38 // Emit events
39 bus.emit(EventA).await;
40 bus.emit(EventB).await;
41 bus.emit(EventC).await;
42 bus.emit(EventD).await;
43
44 // Cancel EventB listener and register EventD listener
45 handle2.cancel();
46 tokio::time::sleep(Duration::from_millis(1)).await;
47 let handle4 = bus.bind(on(|_: EventD| async {
48 println!("Handled Event D");
49 }));
50
51 // Emit again
52 bus.emit(EventA).await;
53 bus.emit(EventB).await;
54 bus.clone().emit(EventD).await;
55
56 drop(bus);
57
58 let _ = join!(handle1, handle3, handle4);
59}Sourcepub async fn emit<E: Event>(&self, event: E)
pub async fn emit<E: Event>(&self, event: E)
Examples found in repository?
examples/base.rs (line 39)
22async fn main() {
23 let bus = Bus::new(2);
24
25 // Register EventA listener
26 let handle1 = bus.bind::<EventA>(once(|_| async {
27 println!("Handled Event A");
28 }));
29
30 // Register EventB listener
31 let handle2 = bus.bind(on(|_: EventB| async {
32 println!("Handled Event B");
33 }));
34
35 // Register EventC listener
36 let handle3 = bus.bind(many(3, event_c_listener));
37
38 // Emit events
39 bus.emit(EventA).await;
40 bus.emit(EventB).await;
41 bus.emit(EventC).await;
42 bus.emit(EventD).await;
43
44 // Cancel EventB listener and register EventD listener
45 handle2.cancel();
46 tokio::time::sleep(Duration::from_millis(1)).await;
47 let handle4 = bus.bind(on(|_: EventD| async {
48 println!("Handled Event D");
49 }));
50
51 // Emit again
52 bus.emit(EventA).await;
53 bus.emit(EventB).await;
54 bus.clone().emit(EventD).await;
55
56 drop(bus);
57
58 let _ = join!(handle1, handle3, handle4);
59}pub fn emitter<E: Event>(&self) -> Emitter<E>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Bus
impl RefUnwindSafe for Bus
impl Send for Bus
impl Sync for Bus
impl Unpin for Bus
impl UnwindSafe for Bus
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