-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from yaak-ai/async
Add support for async
- Loading branch information
Showing
11 changed files
with
221 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//! Async guards and actions example | ||
//! | ||
//! An example of using async guards and actions mixed with standard ones. | ||
|
||
#![deny(missing_docs)] | ||
|
||
use smlang::{async_trait, statemachine}; | ||
use smol; | ||
|
||
statemachine! { | ||
transitions: { | ||
*State1 + Event1 [guard1] / async action1 = State2, | ||
State2 + Event2 [async guard2] / async action2 = State3, | ||
State3 + Event3 / action3 = State4(bool), | ||
} | ||
} | ||
|
||
/// Context with member | ||
pub struct Context { | ||
lock: smol::lock::RwLock<bool>, | ||
done: bool, | ||
} | ||
|
||
#[async_trait] | ||
impl StateMachineContext for Context { | ||
fn guard1(&mut self) -> Result<(), ()> { | ||
println!("`guard1` called from sync context"); | ||
Ok(()) | ||
} | ||
|
||
async fn action1(&mut self) -> () { | ||
println!("`action1` called from async context"); | ||
let mut lock = self.lock.write().await; | ||
*lock = true; | ||
} | ||
|
||
async fn guard2(&mut self) -> Result<(), ()> { | ||
println!("`guard2` called from async context"); | ||
let mut lock = self.lock.write().await; | ||
*lock = false; | ||
Ok(()) | ||
} | ||
|
||
async fn action2(&mut self) -> () { | ||
println!("`action2` called from async context"); | ||
if !*self.lock.read().await { | ||
self.done = true; | ||
} | ||
} | ||
|
||
fn action3(&mut self) -> bool { | ||
println!("`action3` called from sync context, done = `{}`", self.done); | ||
self.done | ||
} | ||
} | ||
|
||
fn main() { | ||
smol::block_on(async { | ||
let mut sm = StateMachine::new(Context { | ||
lock: smol::lock::RwLock::new(false), | ||
done: false, | ||
}); | ||
assert!(matches!(sm.state(), Ok(&States::State1))); | ||
|
||
let r = sm.process_event(Events::Event1).await; | ||
assert!(matches!(r, Ok(&States::State2))); | ||
|
||
let r = sm.process_event(Events::Event2).await; | ||
assert!(matches!(r, Ok(&States::State3))); | ||
|
||
let r = sm.process_event(Events::Event3).await; | ||
assert!(matches!(r, Ok(&States::State4(true)))); | ||
|
||
// 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(_)))); | ||
|
||
let r = sm.process_event(Events::Event2).await; | ||
assert!(matches!(r, Err(Error::InvalidEvent))); | ||
assert!(matches!(sm.state(), Ok(&States::State4(_)))); | ||
}); | ||
|
||
// ... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.