Skip to content

Commit

Permalink
use non-mutable self for guards
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Kumshayev committed Jul 2, 2024
1 parent 8bd2571 commit ab2a206
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ process.
- Fixed clippy warnings
- [breaking] Changed guard functions return type from Result<(),_> to Result<bool,_>
- [breaking] Changed action functions return type from () to Result<NextStateData,_>
- [breaking] Disallow guards mutable access to the context

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

Expand Down
6 changes: 3 additions & 3 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ pub struct Context {

#[async_trait]
impl StateMachineContext for Context {
fn guard3(&mut self) -> Result<bool, ()> {
fn guard3(&self) -> Result<bool, ()> {
println!("`guard3` called from async context");
Ok(true)
}

async fn guard2(&mut self) -> Result<bool, ()> {
async fn guard2(&self) -> Result<bool, ()> {
println!("`guard2` called from async context");
let mut lock = self.lock.write().await;
*lock = false;
Ok(true)
}

fn guard1(&mut self) -> Result<bool, ()> {
fn guard1(&self) -> Result<bool, ()> {
println!("`guard1` called from sync context");
Ok(true)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/event_with_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ statemachine! {
pub struct Context;

impl StateMachineContext for Context {
fn guard(&mut self, event_data: &MyEventData) -> Result<bool, ()> {
fn guard(&self, event_data: &MyEventData) -> Result<bool, ()> {
Ok(event_data == &MyEventData(42))
}

Expand Down
2 changes: 1 addition & 1 deletion examples/event_with_mutable_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ statemachine! {
pub struct Context;

impl StateMachineContext for Context {
fn guard(&mut self, event_data: &mut MyEventData) -> Result<bool, ()> {
fn guard(&self, event_data: &mut MyEventData) -> Result<bool, ()> {
event_data.0 = 55;
Ok(true)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/event_with_reference_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ statemachine! {
pub struct Context;

impl StateMachineContext for Context {
fn guard1(&mut self, event_data: &[u8]) -> Result<bool, ()> {
fn guard1(&self, event_data: &[u8]) -> Result<bool, ()> {
// Only ok if the slice is not empty
Ok(!event_data.is_empty())
}
Expand All @@ -31,7 +31,7 @@ impl StateMachineContext for Context {
Ok(())
}

fn guard2(&mut self, event_data: &MyReferenceWrapper) -> Result<bool, ()> {
fn guard2(&self, event_data: &MyReferenceWrapper) -> Result<bool, ()> {
Ok(*event_data.0 > 9000)
}

Expand Down
4 changes: 2 additions & 2 deletions examples/ex3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ statemachine! {
pub struct Context;

impl StateMachineContext for Context {
fn guard(&mut self) -> Result<bool, ()> {
fn guard(&self) -> Result<bool, ()> {
// Always ok
Ok(true)
}

fn guard_fail(&mut self) -> Result<bool, ()> {
fn guard_fail(&self) -> Result<bool, ()> {
// Always fail
Ok(false)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/guard_action_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct Context;

impl StateMachineContext for Context {
// Guard1 has access to the data from Event1
fn guard1(&mut self, _event_data: &MyEventData) -> Result<bool, ()> {
fn guard1(&self, _event_data: &MyEventData) -> Result<bool, ()> {
todo!()
}

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

// Guard2 has access to the data from State2
fn guard2(&mut self, _state_data: &MyStateData) -> Result<bool, ()> {
fn guard2(&self, _state_data: &MyStateData) -> Result<bool, ()> {
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 @@ -28,7 +28,7 @@ pub struct Context;

impl StateMachineContext for Context {
// Guard1 has access to the data from Event1
fn guard1(&mut self, temp_context: &mut u16, _event_data: &MyEventData) -> Result<bool, ()> {
fn guard1(&self, temp_context: &mut u16, _event_data: &MyEventData) -> Result<bool, ()> {
*temp_context += 1;

Ok(true)
Expand All @@ -46,7 +46,7 @@ impl StateMachineContext for Context {
}

// Guard2 has access to the data from State2
fn guard2(&mut self, temp_context: &mut u16, _state_data: &MyStateData) -> Result<bool, ()> {
fn guard2(&self, temp_context: &mut u16, _state_data: &MyStateData) -> Result<bool, ()> {
*temp_context += 1;

Ok(true)
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 @@ -35,7 +35,7 @@ pub struct Context;

impl StateMachineContext for Context {
type GuardError = GuardError; // Guard1 has access to the data from Event1
fn guard1(&mut self, _event_data: &MyEventData) -> Result<bool, GuardError> {
fn guard1(&self, _event_data: &MyEventData) -> Result<bool, GuardError> {
Err(GuardError::Custom)
}

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

// Guard2 has access to the data from State2
fn guard2(&mut self, _state_data: &MyStateData) -> Result<bool, GuardError> {
fn guard2(&self, _state_data: &MyStateData) -> Result<bool, GuardError> {
todo!()
}

Expand Down
4 changes: 2 additions & 2 deletions examples/named_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ pub struct Context {

#[async_trait]
impl AsyncSimpleStateMachineContext for Context {
fn guard1(&mut self) -> Result<bool, ()> {
fn guard1(&self) -> Result<bool, ()> {
println!("`guard1` called from sync context");
Ok(true)
}

async fn guard2(&mut self) -> Result<bool, ()> {
async fn guard2(&self) -> Result<bool, ()> {
println!("`guard2` called from async context");
let mut lock = self.lock.write().await;
*lock = false;
Expand Down
4 changes: 2 additions & 2 deletions examples/named_ex3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ statemachine! {
pub struct Context;

impl LoopingWithGuardsStateMachineContext for Context {
fn guard(&mut self) -> Result<bool, ()> {
fn guard(&self) -> Result<bool, ()> {
// Always ok
Ok(true)
}

fn guard_fail(&mut self) -> Result<bool, ()> {
fn guard_fail(&self) -> Result<bool, ()> {
// Always fail
Ok(false)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/state_machine_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct Context;

impl StateMachineContext for Context {
// Guard1 has access to the data from Event1
fn guard1(&mut self, event_data: &MyEventData) -> Result<bool, ()> {
fn guard1(&self, event_data: &MyEventData) -> Result<bool, ()> {
Ok(event_data.0 % 2 == 0)
}

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

// Guard2 has access to the data from State2
fn guard2(&mut self, state_data: &MyStateData) -> Result<bool, ()> {
fn guard2(&self, state_data: &MyStateData) -> Result<bool, ()> {
Ok(state_data.0 % 2 == 0)
}

Expand Down
2 changes: 1 addition & 1 deletion macros/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ pub fn generate_code(sm: &ParsedStateMachine) -> proc_macro2::TokenStream {
guard_list.extend(quote! {
#[allow(missing_docs)]
#[allow(clippy::result_unit_err)]
#is_async fn #guard <#all_lifetimes> (&mut self, #temporary_context #state_data #event_data) -> Result<bool,#guard_error>;
#is_async fn #guard <#all_lifetimes> (&self, #temporary_context #state_data #event_data) -> Result<bool,#guard_error>;
});
};
Ok(())
Expand Down
16 changes: 8 additions & 8 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ fn multiple_lifetimes() {
struct Context;

impl StateMachineContext for Context {
fn guard1(&mut self, _event_data: &X) -> Result<bool, ()> {
fn guard1(&self, _event_data: &X) -> Result<bool, ()> {
Ok(true)
}

fn guard2(&mut self, _state_data: &X, _event_data: &Y) -> Result<bool, ()> {
fn guard2(&self, _state_data: &X, _event_data: &Y) -> Result<bool, ()> {
Ok(true)
}

fn guard3(&mut self, _event_data: &Z) -> Result<bool, ()> {
fn guard3(&self, _event_data: &Z) -> Result<bool, ()> {
Ok(true)
}

Expand Down Expand Up @@ -146,7 +146,7 @@ fn async_guards_and_actions() {
struct Context;
#[smlang::async_trait]
impl StateMachineContext for Context {
async fn guard1(&mut self) -> Result<bool, ()> {
async fn guard1(&self) -> Result<bool, ()> {
Ok(true)
}

Expand Down Expand Up @@ -186,10 +186,10 @@ fn guard_expressions() {
attempts: u32,
}
impl StateMachineContext for Context {
fn valid_entry(&mut self, e: &Entry) -> Result<bool, ()> {
fn valid_entry(&self, e: &Entry) -> Result<bool, ()> {
Ok(e.0 == self.password)
}
fn too_many_attempts(&mut self, _e: &Entry) -> Result<bool, ()> {
fn too_many_attempts(&self, _e: &Entry) -> Result<bool, ()> {
Ok(self.attempts >= 3)
}
fn reset(&mut self) -> Result<(), ()> {
Expand Down Expand Up @@ -260,7 +260,7 @@ fn guarded_transition_before_unguarded() {
pub enabled: bool,
}
impl StateMachineContext for Context {
fn guard(&mut self) -> Result<bool, ()> {
fn guard(&self) -> Result<bool, ()> {
Ok(self.enabled)
}

Expand Down Expand Up @@ -294,7 +294,7 @@ fn guard_errors() {
pub guard_errors: bool,
}
impl StateMachineContext for Context {
fn guard(&mut self) -> Result<bool, ()> {
fn guard(&self) -> Result<bool, ()> {
if self.guard_errors {
Err(())
} else {
Expand Down

0 comments on commit ab2a206

Please sign in to comment.