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>
impl<'a, A, S: Send> StatefulEventHandler<'a, A, S>
Sourcepub fn new<F>(f: F, initial_state: S) -> StatefulEventHandler<'a, A, S>
pub fn new<F>(f: F, initial_state: S) -> StatefulEventHandler<'a, A, S>
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.
Trait Implementations§
Source§impl<'a, A, S: Send> EventHandler<A> for StatefulEventHandler<'a, A, S>
impl<'a, A, S: Send> EventHandler<A> for StatefulEventHandler<'a, A, S>
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> 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