Struct Publisher

Source
pub struct Publisher { /* private fields */ }
Expand description

Publishes all Events to all subscribed Handlers that accept Events of that type

§Examples

use gawk::{Event, Handler, Publisher};

#[derive(Copy, Clone)]
struct GamePaused {}

impl Event for GamePaused {}

let mut publisher = Publisher::default();
let pause_handler = Handler::new(|_event: GamePaused| println!("Game paused"));
let pause_handler_id = publisher.subscribe(pause_handler);

publisher.publish(GamePaused {});

publisher.unsubscribe(pause_handler_id);

Implementations§

Source§

impl Publisher

Source

pub fn subscribe<T>(&mut self, handler: T) -> usize
where T: DynHandle + 'static,

Subscribe a handler to the publisher so that the handler receives all published events. Returns the ID needed to unsubscribe the handler.

Source

pub fn unsubscribe(&mut self, id: usize)

Remove a handler from the publisher so that it stops receiving events

Source

pub fn publish<T>( &self, event: T, ) -> Result<(), Vec<Box<dyn Any + Send + 'static>>>
where T: DynEvent,

Publish an event to all subscribed handlers, utilizing as many threads as possible to run handlers in parallel

Trait Implementations§

Source§

impl Default for Publisher

Source§

fn default() -> Publisher

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

Auto Trait Implementations§

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.