Skip to content

Commit

Permalink
Moved transition callback sample to dominos
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Broers <[email protected]>
  • Loading branch information
Martin Broers committed Jul 19, 2024
1 parent 0b23445 commit 1ffd140
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ found in `on_entry_on_exit_generic`.

The statemachine will call for every transition a transition callback. This function
is called with both the old state and new state as arguments. An example can be found
in `transition_callback`.
in `dominos`.

## Helpers

Expand Down
5 changes: 5 additions & 0 deletions examples/dominos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use smlang::statemachine;

statemachine! {
derive_states: [Debug],
derive_events: [Debug],
transitions: {
*D0 + ToD1 / to_d2 = D1,
D1(Option<Events>) + ToD2 / to_d3 = D2,
Expand Down Expand Up @@ -33,6 +35,9 @@ impl StateMachineContext for Context {
fn to_d5(&mut self, _state_data: &Option<Events>) -> Result<Option<Events>, ()> {
Ok(Some(Events::ToD5))
}
fn transition_callback(&self, exit: &States, entry: &States) {
println!("Domino {:?} fell. Next up: {:?}", exit, entry);
}
}

// The macros does not derive Copy/Clone traits to the events, so we need to add them so that the
Expand Down
50 changes: 0 additions & 50 deletions examples/transition_callback.rs

This file was deleted.

6 changes: 3 additions & 3 deletions macros/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {
let out_state = #states_type_name::#out_state;
self.context.log_state_change(&out_state);
#entry_exit_states
self.context().transition_callback(&self.state(), &out_state);
self.context().transition_callback(&self.state, &out_state);
self.state = out_state;
return Ok(&self.state);
}
Expand All @@ -467,7 +467,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {
let out_state = #states_type_name::#out_state;
self.context.log_state_change(&out_state);
#entry_exit_states
self.context().transition_callback(&self.state(), &out_state);
self.context().transition_callback(&self.state, &out_state);
self.state = out_state;
return Ok(&self.state);
}
Expand Down Expand Up @@ -570,7 +570,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {
fn log_state_change(&self, new_state: & #states_type_name) {}

/// Called when transitioning to a new state as a result of an event passed to
/// `process_event()`. No-op by default but can be overridden in implementations
/// `process_event()`. No-op by default which can be overridden in implementations
/// of a state machine's `StateMachineContext` trait.
fn transition_callback(&self, old_state: & #states_type_name, new_state: & #states_type_name) {}
}
Expand Down

0 comments on commit 1ffd140

Please sign in to comment.