Struct StatefulEventHandler

Source
pub struct StatefulEventHandler<'a, A, S: Send> { /* private fields */ }
Expand description

An event handler which manages a closure together with some state which can track information over multiple events. The closure is executed with a mutable reference of the state every time an event is received.

§Type Parameters

  • A: The type of event arguments accepted by this handler.
  • S: The type of the state maintained by this handler.

§Example

use concurrent_event::Event;
use concurrent_event::handler::StatefulEventHandler;

let mut ev = Event::<i32, StatefulEventHandler<i32, i32>>::new();
let handler = StatefulEventHandler::new(|arg: i32, state: &mut i32| *state += arg, 0);
let id = ev.add_handler(handler);
ev.emit(2);
ev.emit(3);
let state = *ev.get_handler(id).unwrap().state();
 
assert_eq!(5, state);

Implementations§

Source§

impl<'a, A, S: Send> StatefulEventHandler<'a, A, S>

Source

pub fn new<F>(f: F, initial_state: S) -> StatefulEventHandler<'a, A, S>
where F: Fn(A, &mut S) + Send + 'a,

Creates a new stateful event handler from a closure and the initial state.

§Parameters
  • f: A closure which is executed every time an event is received. It consumes the event argument and gets a mutable reference to the current state.
  • initial_state: The initial state given to the closure in the first received event.
Source

pub fn state(&self) -> &S

Gets the current state.

Trait Implementations§

Source§

impl<'a, A, S: Send> EventHandler<A> for StatefulEventHandler<'a, A, S>

Source§

fn on_event(&mut self, arg: A)

Auto Trait Implementations§

§

impl<'a, A, S> Freeze for StatefulEventHandler<'a, A, S>
where S: Freeze,

§

impl<'a, A, S> !RefUnwindSafe for StatefulEventHandler<'a, A, S>

§

impl<'a, A, S> Send for StatefulEventHandler<'a, A, S>

§

impl<'a, A, S> !Sync for StatefulEventHandler<'a, A, S>

§

impl<'a, A, S> Unpin for StatefulEventHandler<'a, A, S>
where S: Unpin,

§

impl<'a, A, S> !UnwindSafe for StatefulEventHandler<'a, A, S>

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V