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

Make StateMachine::new and StateMachine::new_with_state const functions #56

Merged
merged 2 commits into from
Nov 3, 2022
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 @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- [breaking] Actions now take owned values
- [breaking] `state()` now returns a `Result`
- `StateMachine::new` and `StateMachine::new_with_state` are now const functions

## [v0.6.0] - 2022-11-02

Expand Down
6 changes: 3 additions & 3 deletions macros/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,15 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {
let starting_state_name = starting_state.to_string();
let new_sm_code = match sm.state_data.data_types.get(&starting_state_name) {
Some(st) => quote! {
pub fn new(context: T, state_data: #st ) -> Self {
pub const fn new(context: T, state_data: #st ) -> Self {
StateMachine {
state: Some(States::#starting_state (state_data)),
context
}
}
},
None => quote! {
pub fn new(context: T ) -> Self {
pub const fn new(context: T ) -> Self {
StateMachine {
state: Some(States::#starting_state),
context
Expand Down Expand Up @@ -521,7 +521,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {

/// Creates a new state machine with an initial state.
#[inline(always)]
pub fn new_with_state(context: T, initial_state: States <#state_lifetimes_code>) -> Self {
pub const fn new_with_state(context: T, initial_state: States <#state_lifetimes_code>) -> Self {
StateMachine {
state: Some(initial_state),
context
Expand Down