Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing redundant log_state_change function #84

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ implementation.

- [breaking] Renamed custom_guard_error flag to custom_error as it is not guard specific anymore
- [breaking] Re-ordered on_exit/on_entry hooks calls
- [breaking] New `transition_callback` has replaced `log_state_change` as it is more flexible.

## [0.7.0] - 2024-07-03

Expand Down
9 changes: 6 additions & 3 deletions examples/state_machine_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl StateMachineContext for Context {

fn log_process_event(&self, current_state: &States, event: &Events) {
println!(
"[StateMachineLogger][{:?}] Processing event {:?}",
"[StateMachineLogger]\t[{:?}] Processing event {:?}",
current_state, event
);
}
Expand All @@ -70,8 +70,11 @@ impl StateMachineContext for Context {
println!("[StateMachineLogger]\tRunning `{}`", action);
}

fn log_state_change(&self, new_state: &States) {
println!("[StateMachineLogger]\tTransitioning to {:?}", new_state);
fn transition_callback(&self, old_state: &States, new_state: &States) {
println!(
"[StateMachineLogger]\tTransitioning {:?} -> {:?}",
old_state, new_state
);
}
}

Expand Down
8 changes: 1 addition & 7 deletions macros/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {
is_async_state_machine |= is_async_action;

let transition = if in_state_string == out_state_string {
// Stay in the same state => no need to call on_entry/on_exit and log_state_change
// Stay in the same state => no need to call on_entry/on_exit
quote!{
#action_code
self.state = #states_type_name::#out_state;
Expand All @@ -436,7 +436,6 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {
self.context.#exit_ident();
#action_code
let out_state = #states_type_name::#out_state;
self.context.log_state_change(&out_state);
self.context().transition_callback(&self.state, &out_state);
self.state = out_state;
self.context.#entry_ident();
Expand Down Expand Up @@ -568,11 +567,6 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {
/// `StateMachineContext` trait.
fn log_action(&self, action: &'static str) {}

/// 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
/// of a state machine's `StateMachineContext` trait.
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 which can be overridden in implementations
/// of a state machine's `StateMachineContext` trait.
Expand Down