-
Notifications
You must be signed in to change notification settings - Fork 28
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
Support for wildcard internal transitions #80
Support for wildcard internal transitions #80
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few minor comments. I'm also confused how the wild-card transition will avoid generating transitions for previously-defined wildcard states. For example:
statemachine! {
*State1 + Event = State2,
_ + Event,
}
How do we ensure that there is no State1 + Event = State1
transition generated, since there's already a state+event combo for this?
This actually works out of the box. I did not do anything for this, as it is covered by the wildcard transitions support, which had already been there. I only extended support to allow statemachine! {
transitions: {
*State1 + Event2 = State2,
State2 + Event3 = State3,
_ + Event1 / increment_count, // Internal transition (implicit: omitting target state)
_ + Event3 / increment_count = _ , // Internal transition (explicit: using _ as target state)
},
derive_states: [Debug, Clone, Copy]
} The macro generates the following transitions for processing Event3: State2 + Event3 = State3,
State1 + Event3 / increment_count = State1,
State3 + Event3 / increment_count = State3, And it is validated by the test, that Event3 in State2 does not increment the count (current state is State2, and the count is 1 before the following assertion is invoked): assert_transition!(sm, Events::Event3, States::State3, 1); while the same Event3 in State3, does increment assert_transition!(sm, Events::Event3, States::State3, 3); |
I pushed the fixes. |
Hi @ryan-summers , would you approve this PR or we have anything undecided ? |
…ike '"on_entry_state3 "', and fails to expand the macro
Hi @ryan-summers, I have rebased due to a merge conflict. I noticed that a new callback, transition_callback, had been added. Now, the transition logic looks as follows, and I am wondering about the purpose of having two different callbacks at practically the same point, #on_exit
#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); // transition_callback
self.state = out_state;
#on_entry // on_entry callback |
The purpose of the |
Okay, I'll leave it to you to decide what to do with this as this is not part of this PR. Thanks |
Thanks for the PR and patience in getting this merged while I was traveling :) |
This PR
Ident
with trailing space, breaking IDE rendering the macro (the fix trims the string before converting it toIdent
)The DSL supports internal transitions.
Internals transition allow to accept some event and process an action and then stay in the current state.
Internal transitions can be specified implicitly, e.g.
and now, after this change, can be specified explicitly, using underscore ('_')
or implicitly, by omitting the target state including symbol '='.
It is also possible to define wildcard implicit (or explicit using '_') internal transitions.
The example above demonstrates, how you could make Event2 acceptable in any state,
not covered by any of the previous transitions, and to do an action to process it.
It is equivalent to:
See also
examples/wildcard_states_and_internal_transitions.rs
orexamples/internal_transitions_with_data.rs
for a usage example.