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 22)
21async fn main() {
22 let bus = Bus::new(2);
23
24 // Register EventA listener
25 bus.once::<EventA>(from_fn(|_| async {
26 println!("Handled Event A");
27 }));
28
29 // Register EventB listener
30 let handle2 = bus.on(from_fn(|_: Rent<EventB>| async {
31 println!("Handled Event B");
32 }));
33
34 // Register EventC listener
35 bus.many(3, from_fn(event_c_listener));
36
37 // Emit events
38 bus.emit(EventA).await;
39 bus.emit(EventB).await;
40 bus.emit(EventC).await;
41 bus.emit(EventD).await;
42
43 // Cancel EventB listener and register EventD listener
44 handle2.cancel();
45 tokio::time::sleep(Duration::from_millis(1)).await;
46 bus.on(from_fn(|_: Rent<EventD>| async {
47 println!("Handled Event D");
48 }));
49
50 // Emit again
51 bus.emit(EventA).await;
52 bus.emit(EventB).await;
53 bus.clone().emit(EventD).await;
54 bus.drain().await;
55}pub fn bind<E: Event>( &self, listener: impl Listener<Event = E>, ) -> SubscribeHandle
pub fn bind_cancel<E: Event>( &self, cancel: CancellationToken, listener: impl Listener<Event = E>, ) -> SubscribeHandle
Sourcepub fn on<E: Event>(
&self,
listener: impl Listener<Event = E>,
) -> SubscribeHandle
pub fn on<E: Event>( &self, listener: impl Listener<Event = E>, ) -> SubscribeHandle
Examples found in repository?
examples/base.rs (lines 30-32)
21async fn main() {
22 let bus = Bus::new(2);
23
24 // Register EventA listener
25 bus.once::<EventA>(from_fn(|_| async {
26 println!("Handled Event A");
27 }));
28
29 // Register EventB listener
30 let handle2 = bus.on(from_fn(|_: Rent<EventB>| async {
31 println!("Handled Event B");
32 }));
33
34 // Register EventC listener
35 bus.many(3, from_fn(event_c_listener));
36
37 // Emit events
38 bus.emit(EventA).await;
39 bus.emit(EventB).await;
40 bus.emit(EventC).await;
41 bus.emit(EventD).await;
42
43 // Cancel EventB listener and register EventD listener
44 handle2.cancel();
45 tokio::time::sleep(Duration::from_millis(1)).await;
46 bus.on(from_fn(|_: Rent<EventD>| async {
47 println!("Handled Event D");
48 }));
49
50 // Emit again
51 bus.emit(EventA).await;
52 bus.emit(EventB).await;
53 bus.clone().emit(EventD).await;
54 bus.drain().await;
55}Sourcepub fn once<E: Event>(
&self,
listener: impl Listener<Event = E>,
) -> SubscribeHandle
pub fn once<E: Event>( &self, listener: impl Listener<Event = E>, ) -> SubscribeHandle
Examples found in repository?
examples/base.rs (lines 25-27)
21async fn main() {
22 let bus = Bus::new(2);
23
24 // Register EventA listener
25 bus.once::<EventA>(from_fn(|_| async {
26 println!("Handled Event A");
27 }));
28
29 // Register EventB listener
30 let handle2 = bus.on(from_fn(|_: Rent<EventB>| async {
31 println!("Handled Event B");
32 }));
33
34 // Register EventC listener
35 bus.many(3, from_fn(event_c_listener));
36
37 // Emit events
38 bus.emit(EventA).await;
39 bus.emit(EventB).await;
40 bus.emit(EventC).await;
41 bus.emit(EventD).await;
42
43 // Cancel EventB listener and register EventD listener
44 handle2.cancel();
45 tokio::time::sleep(Duration::from_millis(1)).await;
46 bus.on(from_fn(|_: Rent<EventD>| async {
47 println!("Handled Event D");
48 }));
49
50 // Emit again
51 bus.emit(EventA).await;
52 bus.emit(EventB).await;
53 bus.clone().emit(EventD).await;
54 bus.drain().await;
55}Sourcepub fn many<E: Event>(
&self,
times: usize,
listener: impl Listener<Event = E>,
) -> SubscribeHandle
pub fn many<E: Event>( &self, times: usize, listener: impl Listener<Event = E>, ) -> SubscribeHandle
Examples found in repository?
examples/base.rs (line 35)
21async fn main() {
22 let bus = Bus::new(2);
23
24 // Register EventA listener
25 bus.once::<EventA>(from_fn(|_| async {
26 println!("Handled Event A");
27 }));
28
29 // Register EventB listener
30 let handle2 = bus.on(from_fn(|_: Rent<EventB>| async {
31 println!("Handled Event B");
32 }));
33
34 // Register EventC listener
35 bus.many(3, from_fn(event_c_listener));
36
37 // Emit events
38 bus.emit(EventA).await;
39 bus.emit(EventB).await;
40 bus.emit(EventC).await;
41 bus.emit(EventD).await;
42
43 // Cancel EventB listener and register EventD listener
44 handle2.cancel();
45 tokio::time::sleep(Duration::from_millis(1)).await;
46 bus.on(from_fn(|_: Rent<EventD>| async {
47 println!("Handled Event D");
48 }));
49
50 // Emit again
51 bus.emit(EventA).await;
52 bus.emit(EventB).await;
53 bus.clone().emit(EventD).await;
54 bus.drain().await;
55}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 38)
21async fn main() {
22 let bus = Bus::new(2);
23
24 // Register EventA listener
25 bus.once::<EventA>(from_fn(|_| async {
26 println!("Handled Event A");
27 }));
28
29 // Register EventB listener
30 let handle2 = bus.on(from_fn(|_: Rent<EventB>| async {
31 println!("Handled Event B");
32 }));
33
34 // Register EventC listener
35 bus.many(3, from_fn(event_c_listener));
36
37 // Emit events
38 bus.emit(EventA).await;
39 bus.emit(EventB).await;
40 bus.emit(EventC).await;
41 bus.emit(EventD).await;
42
43 // Cancel EventB listener and register EventD listener
44 handle2.cancel();
45 tokio::time::sleep(Duration::from_millis(1)).await;
46 bus.on(from_fn(|_: Rent<EventD>| async {
47 println!("Handled Event D");
48 }));
49
50 // Emit again
51 bus.emit(EventA).await;
52 bus.emit(EventB).await;
53 bus.clone().emit(EventD).await;
54 bus.drain().await;
55}pub fn emitter<E: Event>(&self) -> Emitter<E>
Sourcepub async fn drain(self)
pub async fn drain(self)
Examples found in repository?
examples/base.rs (line 54)
21async fn main() {
22 let bus = Bus::new(2);
23
24 // Register EventA listener
25 bus.once::<EventA>(from_fn(|_| async {
26 println!("Handled Event A");
27 }));
28
29 // Register EventB listener
30 let handle2 = bus.on(from_fn(|_: Rent<EventB>| async {
31 println!("Handled Event B");
32 }));
33
34 // Register EventC listener
35 bus.many(3, from_fn(event_c_listener));
36
37 // Emit events
38 bus.emit(EventA).await;
39 bus.emit(EventB).await;
40 bus.emit(EventC).await;
41 bus.emit(EventD).await;
42
43 // Cancel EventB listener and register EventD listener
44 handle2.cancel();
45 tokio::time::sleep(Duration::from_millis(1)).await;
46 bus.on(from_fn(|_: Rent<EventD>| async {
47 println!("Handled Event D");
48 }));
49
50 // Emit again
51 bus.emit(EventA).await;
52 bus.emit(EventB).await;
53 bus.clone().emit(EventD).await;
54 bus.drain().await;
55}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