Skip to content

Commit

Permalink
Merge branch 'master' into const-new
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers authored Nov 3, 2022
2 parents 3579390 + 530f4fd commit 8eeb603
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 84 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- [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
8 changes: 4 additions & 4 deletions examples/dominos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ impl StateMachineContext for Context {
Some(Events::ToD2)
}

fn to_d3(&mut self, _state_data: &Option<Events>) -> Option<Events> {
fn to_d3(&mut self, _state_data: Option<Events>) -> Option<Events> {
Some(Events::ToD3)
}

fn to_d4(&mut self, _state_data: &Option<Events>) -> Option<Events> {
fn to_d4(&mut self, _state_data: Option<Events>) -> Option<Events> {
Some(Events::ToD4)
}

fn to_d5(&mut self, _state_data: &Option<Events>) -> Option<Events> {
fn to_d5(&mut self, _state_data: Option<Events>) -> Option<Events> {
Some(Events::ToD5)
}
}
Expand Down Expand Up @@ -67,5 +67,5 @@ fn main() {
}

// All the dominos fell!
assert!(sm.state() == &States::D5);
assert!(matches!(sm.state(), Ok(&States::D5)));
}
2 changes: 1 addition & 1 deletion examples/event_with_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl StateMachineContext for Context {
}
}

fn action(&mut self, event_data: &MyEventData) {
fn action(&mut self, event_data: MyEventData) {
println!("Got valid Event Data = {}", event_data.0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/event_with_reference_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl StateMachineContext for Context {
}
}

fn action2(&mut self, event_data: &MyReferenceWrapper) {
fn action2(&mut self, event_data: MyReferenceWrapper) {
println!("Got valid Event Data = {}", event_data.0);
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/ex1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl StateMachineContext for Context {}

fn main() {
let mut sm = StateMachine::new(Context);
assert!(sm.state() == &States::State1);
assert!(matches!(sm.state(), Ok(&States::State1)));

let r = sm.process_event(Events::Event1);
assert!(matches!(r, Ok(&States::State2)));
Expand All @@ -32,9 +32,9 @@ fn main() {
// Now all events will not give any change of state
let r = sm.process_event(Events::Event1);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(matches!(sm.state(), &States::State3));
assert!(matches!(sm.state(), Ok(&States::State3)));

let r = sm.process_event(Events::Event2);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(sm.state() == &States::State3);
assert!(matches!(sm.state(), Ok(&States::State3)));
}
4 changes: 2 additions & 2 deletions examples/ex2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl StateMachineContext for Context {}

fn main() {
let mut sm = StateMachine::new(Context);
assert!(sm.state() == &States::State1);
assert!(matches!(sm.state(), Ok(&States::State1)));

let r = sm.process_event(Events::Event1);
assert!(matches!(r, Ok(&States::State2)));
Expand All @@ -43,5 +43,5 @@ fn main() {
// Now we cannot use Event1 again, as it is outside the state machine loop
let r = sm.process_event(Events::Event1);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(sm.state() == &States::State2);
assert!(matches!(sm.state(), Ok(&States::State2)));
}
4 changes: 2 additions & 2 deletions examples/ex3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl StateMachineContext for Context {

fn main() {
let mut sm = StateMachine::new(Context);
assert!(sm.state() == &States::State1);
assert!(matches!(sm.state(), Ok(&States::State1)));

println!("Before action 1");

Expand All @@ -58,5 +58,5 @@ fn main() {
println!("After action 2");

// Now we are stuck due to the guard never returning true
assert!(sm.state() == &States::State2);
assert!(matches!(sm.state(), Ok(&States::State2)));
}
4 changes: 2 additions & 2 deletions examples/guard_action_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl StateMachineContext for Context {
}

// Action1 has access to the data from Event1, and need to return the state data for State2
fn action1(&mut self, _event_data: &MyEventData) -> MyStateData {
fn action1(&mut self, _event_data: MyEventData) -> MyStateData {
todo!()
}

Expand All @@ -42,7 +42,7 @@ impl StateMachineContext for Context {
}

// Action2 has access to the data from State2
fn action2(&mut self, _state_data: &MyStateData) {
fn action2(&mut self, _state_data: MyStateData) {
todo!()
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/guard_action_syntax_with_temporary_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl StateMachineContext for Context {
}

// Action1 has access to the data from Event1, and need to return the state data for State2
fn action1(&mut self, temp_context: &mut u16, _event_data: &MyEventData) -> MyStateData {
fn action1(&mut self, temp_context: &mut u16, _event_data: MyEventData) -> MyStateData {
*temp_context += 1;

MyStateData(1)
Expand All @@ -49,7 +49,7 @@ impl StateMachineContext for Context {
}

// Action2 has access to the data from State2
fn action2(&mut self, temp_context: &mut u16, _state_data: &MyStateData) {
fn action2(&mut self, temp_context: &mut u16, _state_data: MyStateData) {
*temp_context += 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/guard_custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl StateMachineContext for Context {
}

// Action1 has access to the data from Event1, and need to return the state data for State2
fn action1(&mut self, _event_data: &MyEventData) -> MyStateData {
fn action1(&mut self, _event_data: MyEventData) -> MyStateData {
todo!()
}

Expand All @@ -52,7 +52,7 @@ impl StateMachineContext for Context {
}

// Action2 has access to the data from State2
fn action2(&mut self, _state_data: &MyStateData) {
fn action2(&mut self, _state_data: MyStateData) {
todo!()
}
}
Expand Down
14 changes: 7 additions & 7 deletions examples/input_state_pattern_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl StateMachineContext for Context {}
fn main() {
let mut sm = StateMachine::new(Context);

assert!(sm.state() == &States::Idle);
assert!(matches!(sm.state(), Ok(&States::Idle)));

let r = sm.process_event(Events::Charge);
assert!(matches!(r, Ok(&States::Charging)));
Expand All @@ -61,7 +61,7 @@ fn main() {

let r = sm.process_event(Events::Charge);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(sm.state() == &States::Charged);
assert!(matches!(sm.state(), Ok(&States::Charged)));

let r = sm.process_event(Events::Discharge);
assert!(matches!(r, Ok(&States::Discharging)));
Expand All @@ -71,7 +71,7 @@ fn main() {

let r = sm.process_event(Events::Discharge);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(sm.state() == &States::Discharged);
assert!(matches!(sm.state(), Ok(&States::Discharged)));

sm = StateMachine::new_with_state(Context, States::Idle);
let r = sm.process_event(Events::FaultDetected);
Expand All @@ -95,17 +95,17 @@ fn main() {

let r = sm.process_event(Events::Charge);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(sm.state() == &States::Fault);
assert!(matches!(sm.state(), Ok(&States::Fault)));

let r = sm.process_event(Events::Discharge);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(sm.state() == &States::Fault);
assert!(matches!(sm.state(), Ok(&States::Fault)));

let r = sm.process_event(Events::ChargeComplete);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(sm.state() == &States::Fault);
assert!(matches!(sm.state(), Ok(&States::Fault)));

let r = sm.process_event(Events::DischargeComplete);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(sm.state() == &States::Fault);
assert!(matches!(sm.state(), Ok(&States::Fault)));
}
2 changes: 1 addition & 1 deletion examples/reuse_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl StateMachineContext for Context {

fn main() {
let mut sm = StateMachine::new(Context(0));
assert!(sm.state() == &States::State1);
assert!(matches!(sm.state(), Ok(&States::State1)));
assert!(sm.context.0 == 0);

// triggers action
Expand Down
Loading

0 comments on commit 8eeb603

Please sign in to comment.