EventBus

Struct EventBus 

Source
pub struct EventBus<E, V> { /* private fields */ }
Expand description

An event bus that can be cloned and shared across threads. If you do not need to share the bus across threads use unsync::EventBus which is more efficient in terms of performance since it doens’t need to hold locks on resources

§Example

use tram::{prelude::*, sync::EventBus};
use std::{cell::RefCell, rc::Rc};

#[derive(PartialEq, Eq, Hash)]
enum EventType {
    Start,
    Stop
}

#[derive(Debug, PartialEq)]
enum Status {
    Stopped,
    Started
}

let bus: EventBus<EventType, ()> = EventBus::unbound();
let status = Rc::new(RefCell::new(Status::Stopped));
let status_closure = Rc::clone(&status);

bus.on(EventType::Start, move |_bus, _| {
    *status_closure.borrow_mut() = Status::Started;
})
.expect("Failed to listen for this event");

bus.emit(EventType::Start).expect("Failed to emit");

assert_eq!(*status.borrow(), Status::Started);
assert_eq!(bus.event_count(), 1);

Implementations§

Source§

impl<E, V> EventBus<E, V>

Source

pub fn unbound() -> Self

Creates an unbound bus that can emit any number of events

Source

pub fn bound(limit: usize) -> Self

Creates a bound bus that can emit up to limit events

Source

pub fn disconnected(&self) -> bool

Returns true if this bus has exausted its allowed max number of emits

Source

pub fn event_count(&self) -> usize

Trait Implementations§

Source§

impl<E, V> Clone for EventBus<E, V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E, V> EventEmitter<E, V> for EventBus<E, V>
where E: Eq + Hash,

Source§

fn on<F>(&self, event: E, f: F) -> Result<(), Error>
where F: Fn(&BusRef<E, V>, Option<&V>) + 'static,

Adds a listener f for and event
Source§

fn emit(&self, event: E) -> Result<(), Error>

Emits an event, firing all listeners connected to it via on. Read more
Source§

fn emit_with_value(&self, event: E, value: Option<&V>) -> Result<(), Error>

Emits an event with a value associated to it, firing all listeners connected to it via on.
Source§

impl<E, V> Send for EventBus<E, V>
where E: Send,

Source§

impl<E, V> Sync for EventBus<E, V>
where E: Send,

Auto Trait Implementations§

§

impl<E, V> Freeze for EventBus<E, V>

§

impl<E, V> !RefUnwindSafe for EventBus<E, V>

§

impl<E, V> Unpin for EventBus<E, V>

§

impl<E, V> !UnwindSafe for EventBus<E, V>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.