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

Added transition_callback #77

Merged
merged 2 commits into from
Jul 19, 2024

Conversation

MartinBroers
Copy link

@ryan-summers Thanks for merging !66. I also like the function of a transition callback, so I went straight on it.

In terms of refinement and/or discussion, I am doubting mostly about this right now:

                                              self.context().transition_callback(&in_state, &out_state);
                                              self.state = Some(out_state);

Or should it be like this:

                                              self.state = Some(out_state);
                                              self.context().transition_callback(&in_state, &out_state);

So, call the transition callback after the internal state has been updated?

@ryan-summers
Copy link
Collaborator

I don't think it matters too much since the user will have both the input and output state directly in the method call as opposed to via the context.

@@ -500,8 +502,10 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {
quote!{
#action_code
let out_state = #states_type_name::#out_state;
let in_state = #states_type_name::#in_state;
Copy link
Collaborator

@ryan-summers ryan-summers Jun 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually work for in_state that has data associated with it? I'm not sure how we're moving the internal state machine data around.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, the sample does not compile. Nor do the tests. I'll rework this so it will all work and compile as intended. I think we do not want the data to be passed to the transition callback, since it will make things way more complex (if not impossible for a generic callback).

\"custom_guard_error\", \
\"derive_states\", \
\"derive_events\"
"Unknown keyword {}. Support keywords: [\"name\",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, how does this shown up in the console? The \ ending were supposed to prevent newlines from being inserted into the formatted string.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was some autoformat, but in my console it looks good though.

error: Unknown keyword unknown. Support keywords: ["name",
                                       "transitions",
                                       "temporary_context",
                                       "custom_guard_error",
                                       "derive_states",
                                       "derive_events",
                                       ]

but since it has nothing to do with this MR, I'll revert it.

@MartinBroers MartinBroers force-pushed the transition-callback branch from 2f97f41 to 43694af Compare July 5, 2024 19:09
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]>
@MartinBroers MartinBroers force-pushed the transition-callback branch from 43694af to 0b23445 Compare July 5, 2024 19:13
Copy link
Collaborator

@ryan-summers ryan-summers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just a few minor cleanup requests

#![deny(missing_docs)]

use std::rc::Rc;
use std::sync::Mutex;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why we need all these sync primitives, and I think they may make the example more complex than it should be.

Also, could we insert this into an existing example instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, moved to dominos example. I also removed the sync primitives. We needed them to have a non-mut reference to self in the callback. Don't see the need to make it mutable at this point in time. If we need it mutable, we can do so in another feature request?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think its probably correct that we don't get mut access in the transition callback, since we shouldn't be mutating the state machine

macros/src/codegen.rs Outdated Show resolved Hide resolved
macros/src/codegen.rs Outdated Show resolved Hide resolved
macros/src/codegen.rs Outdated Show resolved Hide resolved
@ryan-summers ryan-summers merged commit 5980311 into korken89:master Jul 19, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants