pub fn with_state<H, S1, S2, E>(
state: S2,
handler: H,
) -> impl Handler<S1, E, Output = H::Output>where
H: Handler<S2, E>,
Expand description
Forwards the given state to the composed event handler.
This state overrides any state passed to the constructed event handler; only the state given to this function is visible to the composed event handler.
§Examples
The following examples constructs an event handler where branches of an event handler have
exclusive access to an isize
. Note that it would not be possible for the closures to capture
this state from the environment mutably, as the closures would need to mutably alias the data.
ⓘ
let mut handler = event::with_state(
0isize,
branch::or((
event::extract(Stateful(|count: &mut isize, _: Buffered<MgmtFrame>| {
*count += 1;
*count
})),
event::extract(Stateful(|count: &mut isize, _: Buffered<DataFrame>| {
*count -= 1;
*count
})),
)),
);