-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds a transition_callback function. The function, if not used, will be optimized away by the compiler. Signed-off-by: Martin Broers <[email protected]>
- Loading branch information
Martin Broers
committed
Jul 5, 2024
1 parent
32d7513
commit 43694af
Showing
3 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//! An example of using state data to propagate events (See issue-17) | ||
#![deny(missing_docs)] | ||
|
||
use std::rc::Rc; | ||
use std::sync::Mutex; | ||
|
||
use smlang::statemachine; | ||
|
||
statemachine! { | ||
derive_states: [Debug, Copy, Clone ], | ||
transitions: { | ||
*D0 + ToD1 = D1, | ||
D1 + ToD2 = D2, | ||
}, | ||
} | ||
|
||
/// Context | ||
pub struct Context { | ||
transition_called: Rc<Mutex<bool>>, | ||
state_exited: Rc<Mutex<Option<States>>>, | ||
state_entered: Rc<Mutex<Option<States>>>, | ||
} | ||
|
||
impl StateMachineContext for Context { | ||
fn transition_callback(&self, exit: &States, entry: &States) { | ||
*self.transition_called.lock().unwrap() = true; | ||
*self.state_exited.lock().unwrap() = Some(*exit); | ||
*self.state_entered.lock().unwrap() = Some(*entry); | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut sm = StateMachine::new(Context { | ||
transition_called: Rc::new(Mutex::new(false)), | ||
state_exited: Rc::new(Mutex::new(None)), | ||
state_entered: Rc::new(Mutex::new(None)), | ||
}); | ||
|
||
// first event starts the dominos | ||
let _ = sm.process_event(Events::ToD1).unwrap(); | ||
|
||
assert!(matches!(sm.state(), &States::D1)); | ||
assert!(*sm.context().transition_called.lock().unwrap()); | ||
assert_eq!(*sm.context().state_exited.lock().unwrap(), Some(States::D0)); | ||
assert_eq!( | ||
*sm.context().state_entered.lock().unwrap(), | ||
Some(States::D1) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters