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

Fixing the internal state management, reverting actions to take references to state data, cleaning up codegen #76

Merged
merged 7 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ implementation.

### Changed

- [breaking] Actions now take owned values
- [breaking] `state()` now returns a `Result`
- `StateMachine::new` and `StateMachine::new_with_state` are now const functions
- Fixed clippy warnings
- [breaking] Changed guard functions return type from Result<(),_> to Result<bool,_>
Expand Down
6 changes: 3 additions & 3 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn main() {
lock: smol::lock::RwLock::new(false),
done: false,
});
assert!(matches!(sm.state(), Ok(&States::State1)));
assert!(matches!(sm.state(), &States::State1));

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

let r = sm.process_event(Events::Event2).await;
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&States::State4(_))));
assert!(matches!(sm.state(), &States::State4(_)));
});

// ...
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!(matches!(sm.state(), Ok(&States::D5)));
assert!(matches!(sm.state(), &States::D5));
}
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!(matches!(sm.state(), Ok(&States::State1)));
assert!(matches!(sm.state(), &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(), Ok(&States::State3)));
assert!(matches!(sm.state(), &States::State3));

let r = sm.process_event(Events::Event2);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&States::State3)));
assert!(matches!(sm.state(), &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!(matches!(sm.state(), Ok(&States::State1)));
assert!(matches!(sm.state(), &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!(matches!(sm.state(), Ok(&States::State2)));
assert!(matches!(sm.state(), &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!(matches!(sm.state(), Ok(&States::State1)));
assert!(matches!(sm.state(), &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!(matches!(sm.state(), Ok(&States::State2)));
assert!(matches!(sm.state(), &States::State2));
}
2 changes: 1 addition & 1 deletion examples/guard_action_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion examples/guard_action_syntax_with_temporary_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion examples/guard_custom_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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!(matches!(sm.state(), Ok(&States::Idle)));
assert!(matches!(sm.state(), &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!(matches!(sm.state(), Ok(&States::Charged)));
assert!(matches!(sm.state(), &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!(matches!(sm.state(), Ok(&States::Discharged)));
assert!(matches!(sm.state(), &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!(matches!(sm.state(), Ok(&States::Fault)));
assert!(matches!(sm.state(), &States::Fault));

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

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

let r = sm.process_event(Events::DischargeComplete);
assert!(matches!(r, Err(Error::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&States::Fault)));
assert!(matches!(sm.state(), &States::Fault));
}
6 changes: 3 additions & 3 deletions examples/named_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() {
lock: smol::lock::RwLock::new(false),
done: false,
});
assert!(matches!(sm.state(), Ok(&AsyncSimpleStates::State1)));
assert!(matches!(sm.state(), &AsyncSimpleStates::State1));

let r = sm.process_event(AsyncSimpleEvents::Event1).await;
assert!(matches!(r, Ok(&AsyncSimpleStates::State2)));
Expand All @@ -74,11 +74,11 @@ fn main() {
// Now all events will not give any change of state
let r = sm.process_event(AsyncSimpleEvents::Event1).await;
assert!(matches!(r, Err(AsyncSimpleError::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&AsyncSimpleStates::State4(_))));
assert!(matches!(sm.state(), &AsyncSimpleStates::State4(_)));

let r = sm.process_event(AsyncSimpleEvents::Event2).await;
assert!(matches!(r, Err(AsyncSimpleError::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&AsyncSimpleStates::State4(_))));
assert!(matches!(sm.state(), &AsyncSimpleStates::State4(_)));
});

// ...
Expand Down
8 changes: 4 additions & 4 deletions examples/named_dominos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ impl DominosStateMachineContext for Context {
Some(DominosEvents::ToD2)
}

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

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

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

// All the dominos fell!
assert!(matches!(sm.state(), Ok(&DominosStates::D5)));
assert!(matches!(sm.state(), &DominosStates::D5));
}
6 changes: 3 additions & 3 deletions examples/named_ex1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl LinearStateMachineContext for Context {}

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

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

let r = sm.process_event(LinearEvents::Event2);
assert!(matches!(r, Err(LinearError::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&LinearStates::State3)));
assert!(matches!(sm.state(), &LinearStates::State3));
}
4 changes: 2 additions & 2 deletions examples/named_ex2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl LoopingStateMachineContext for Context {}

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

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

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

println!("Before action 1");

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

// Now we are stuck due to the guard never returning true
assert!(matches!(sm.state(), Ok(&LoopingWithGuardsStates::State2)));
assert!(matches!(sm.state(), &LoopingWithGuardsStates::State2));
}
14 changes: 7 additions & 7 deletions examples/named_input_state_pattern_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl BatteryStateMachineContext for Context {}
fn main() {
let mut sm = BatteryStateMachine::new(Context);

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

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

let r = sm.process_event(BatteryEvents::Charge);
assert!(matches!(r, Err(BatteryError::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&BatteryStates::Charged)));
assert!(matches!(sm.state(), &BatteryStates::Charged));

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

let r = sm.process_event(BatteryEvents::Discharge);
assert!(matches!(r, Err(BatteryError::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&BatteryStates::Discharged)));
assert!(matches!(sm.state(), &BatteryStates::Discharged));

sm = BatteryStateMachine::new_with_state(Context, BatteryStates::Idle);
let r = sm.process_event(BatteryEvents::FaultDetected);
Expand All @@ -96,17 +96,17 @@ fn main() {

let r = sm.process_event(BatteryEvents::Charge);
assert!(matches!(r, Err(BatteryError::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&BatteryStates::Fault)));
assert!(matches!(sm.state(), &BatteryStates::Fault));

let r = sm.process_event(BatteryEvents::Discharge);
assert!(matches!(r, Err(BatteryError::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&BatteryStates::Fault)));
assert!(matches!(sm.state(), &BatteryStates::Fault));

let r = sm.process_event(BatteryEvents::ChargeComplete);
assert!(matches!(r, Err(BatteryError::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&BatteryStates::Fault)));
assert!(matches!(sm.state(), &BatteryStates::Fault));

let r = sm.process_event(BatteryEvents::DischargeComplete);
assert!(matches!(r, Err(BatteryError::InvalidEvent)));
assert!(matches!(sm.state(), Ok(&BatteryStates::Fault)));
assert!(matches!(sm.state(), &BatteryStates::Fault));
}
10 changes: 5 additions & 5 deletions examples/on_entry_on_exit_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,31 @@ fn main() {
// first event starts the dominos
let _ = sm.process_event(OnEntryExampleEvents::ToD1).unwrap();

assert!(matches!(sm.state(), Ok(&OnEntryExampleStates::D1)));
assert!(matches!(sm.state(), &OnEntryExampleStates::D1));
assert_eq!(sm.context().exited_d0, 1);
assert_eq!(sm.context().entered_d1, 1);

let _ = sm.process_event(OnEntryExampleEvents::ToD2).unwrap();

assert!(matches!(sm.state(), Ok(&OnEntryExampleStates::D2)));
assert!(matches!(sm.state(), &OnEntryExampleStates::D2));
assert_eq!(sm.context().exited_d0, 1);
assert_eq!(sm.context().entered_d1, 1);

let _ = sm.process_event(OnEntryExampleEvents::ToD1).unwrap();

assert!(matches!(sm.state(), Ok(&OnEntryExampleStates::D1)));
assert!(matches!(sm.state(), &OnEntryExampleStates::D1));
assert_eq!(sm.context().exited_d0, 1);
assert_eq!(sm.context().entered_d1, 2);

let _ = sm.process_event(OnEntryExampleEvents::ToD0).unwrap();

assert!(matches!(sm.state(), Ok(&OnEntryExampleStates::D0)));
assert!(matches!(sm.state(), &OnEntryExampleStates::D0));
assert_eq!(sm.context().exited_d0, 1);
assert_eq!(sm.context().entered_d1, 2);

let _ = sm.process_event(OnEntryExampleEvents::ToD3).unwrap();

assert!(matches!(sm.state(), Ok(&OnEntryExampleStates::D3)));
assert!(matches!(sm.state(), &OnEntryExampleStates::D3));
assert_eq!(sm.context().exited_d0, 2);
assert_eq!(sm.context().entered_d1, 2);
}
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!(matches!(sm.state(), Ok(&States::State1)));
assert!(matches!(sm.state(), &States::State1));
assert!(sm.context.0 == 0);

// triggers action
Expand Down
Loading
Loading