Skip to content

Commit

Permalink
Merge pull request korken89#56 from oblique/const-new
Browse files Browse the repository at this point in the history
Make `StateMachine::new` and `StateMachine::new_with_state` const functions
  • Loading branch information
ryan-summers authored Nov 3, 2022
2 parents 530f4fd + 8eeb603 commit a8e60a9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
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

0 comments on commit a8e60a9

Please sign in to comment.