Struct EventBus

Source
pub struct EventBus<T: Clone + Send + Sync + 'static> { /* private fields */ }
Expand description

Manages a generic, multi-producer, single-consumer (MPSC), thread-safe event channel.

This EventBus is generic over the event type T it transports. It serves as a foundational communication primitive within Khora, allowing different parts of the engine to communicate in a decoupled manner.

The design is intentional: there are many senders but only one receiver, ensuring that a single, authoritative system is responsible for processing all events of a given type. Senders can be cloned freely and passed to different threads.

§Examples

#[derive(Clone, Debug, PartialEq)]
enum GameEvent {
    PlayerJumped,
    ScoreChanged(u32),
}

// Create a new bus for our specific event type.
let event_bus = EventBus::<GameEvent>::new();

// Clone the sender to give to a game system.
let sender = event_bus.sender();

// A system publishes an event.
sender.send(GameEvent::PlayerJumped);

// The main event loop (the owner of the bus) processes the event.
if let Ok(event) = event_bus.receiver().try_recv() {
    assert_eq!(event, GameEvent::PlayerJumped);
}

Implementations§

Source§

impl<T: Clone + Send + Sync + 'static> EventBus<T>

Source

pub fn new() -> Self

Creates a new EventBus with an unbounded channel.

Source

pub fn publish(&self, event: T)

Publishes an event to all receivers.

This method is a convenience wrapper around the channel’s send operation. It logs an error if the send fails, which typically means the receiver (and thus the EventBus instance) has been dropped.

§Arguments
  • event: The event of type T to be sent over the channel.
Source

pub fn sender(&self) -> Sender<T>

Returns a clone of the sender part of the channel.

This is the primary way to allow other parts of the system to send events without giving them ownership of the entire bus. Senders can be cloned multiple times and sent across threads.

Source

pub fn receiver(&self) -> &Receiver<T>

Returns a reference to the receiver part of the channel.

This is intended for the owner of the bus (e.g., the main event loop) to process incoming events. It returns a reference to prevent the receiver from being moved out of the EventBus.

Trait Implementations§

Source§

impl<T: Debug + Clone + Send + Sync + 'static> Debug for EventBus<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Clone + Send + Sync + 'static> Default for EventBus<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for EventBus<T>

§

impl<T> RefUnwindSafe for EventBus<T>

§

impl<T> Send for EventBus<T>

§

impl<T> Sync for EventBus<T>

§

impl<T> Unpin for EventBus<T>

§

impl<T> UnwindSafe for EventBus<T>

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<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.