Skip to content

feature: add current and next state matches to on_enter and on_exit #19

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

Merged
merged 5 commits into from
Apr 16, 2025

Conversation

lotus128
Copy link

@lotus128 lotus128 commented Apr 4, 2025

Implements #18.

@Seldom-SE
Copy link
Owner

Seldom-SE commented Apr 6, 2025

Hm, this makes the API a bit more confusing. Would a default type work here (ex fn on_enter<NextState: EntityState, CurrentState: EntityState = AnyState>) and would it allow the previous usage (omitting the second type param like on_enter::<MyState>) to still work?

@lotus128
Copy link
Author

lotus128 commented Apr 6, 2025

I'm not super familiar with rust, but I did try this and found that Rust does not support default type params for functions: rust-lang/rust#36887.

New methods could be introduced (like on_enter_from and on_exit_to) and the old methods could call those instead.

@Seldom-SE
Copy link
Owner

Good idea. I'll give a proper review soon.

Copy link
Owner

@Seldom-SE Seldom-SE left a comment

Choose a reason for hiding this comment

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

Change on_enter and on_exit back and add on_enter_from and on_exit_to (as you suggested). Same for command_on_enter et al.

Someday, I'll add a feature that makes all the command_on_* functions unnecessary, so all this duplication is temporary.

Thanks!

src/machine.rs Outdated
pub fn on_enter<S: EntityState>(
/// Adds an on-enter event to the state machine. Whenever the state machine transitions to the
/// given next state from the given current state, it will run the event.
pub fn on_enter<NextState: EntityState, CurrentState: EntityState>(
Copy link
Owner

Choose a reason for hiding this comment

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

NextState should be called Next and CurrentState should be called Prev to match other methods.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

src/machine.rs Outdated
pub fn on_enter<S: EntityState>(
/// Adds an on-enter event to the state machine. Whenever the state machine transitions to the
/// given next state from the given current state, it will run the event.
pub fn on_enter<NextState: EntityState, CurrentState: EntityState>(
Copy link
Owner

Choose a reason for hiding this comment

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

Put CurrentState before NextState. Both ways have ways they can be confusing, but putting CurrentState first feels to me more consistent with the rest of the API.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@lotus128 lotus128 requested a review from Seldom-SE April 11, 2025 15:36
Copy link
Owner

@Seldom-SE Seldom-SE left a comment

Choose a reason for hiding this comment

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

Aside from this, it looks good

@Seldom-SE
Copy link
Owner

Whoops forgot to add the comment

@lotus128 lotus128 requested a review from Seldom-SE April 13, 2025 15:20
@lotus128 lotus128 requested a review from Seldom-SE April 14, 2025 22:26
@Seldom-SE Seldom-SE merged commit fb6d540 into Seldom-SE:main Apr 16, 2025
3 of 4 checks passed
@Seldom-SE
Copy link
Owner

Thank you

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