Skip to content

Commit

Permalink
Create seperate traits for blocking and awaitable implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeloof committed Jan 3, 2025
1 parent 0557b94 commit 6adedf4
Show file tree
Hide file tree
Showing 23 changed files with 1,246 additions and 160 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ members = [
"examples/macro/generics",
"examples/macro/async_blinky",
"examples/macro/async_io",
"examples/macro/async_bench",
"examples/macro/async_bench_complex",

"examples/no_macro/basic",
"examples/no_macro/blinky",
Expand Down
11 changes: 11 additions & 0 deletions examples/macro/async_bench/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "macro_async_bench"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
statig = { path = "../../../statig", features = ["async"] }
rand = "0.8.5"
futures = { version = "*" }
118 changes: 118 additions & 0 deletions examples/macro/async_bench/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#![allow(unused)]

use statig::prelude::*;
use std::io::Empty;
use std::io::Write;
use std::time::Instant;

pub enum Event {
StartPlayback,
ResumePlayback,
CloseDrawer,
OpenDrawer,
StopAndOpen,
StoppedAgain,
StoreCdInfo,
PausePlayback,
StopPlayback,
Play,
EndPause,
Stop,
Pause2,
OpenClose,
CdDetected,
}

#[derive(Default)]
pub struct CdPlayer;

#[state_machine(initial = "State::empty()", state(derive(Debug)))]
impl CdPlayer {
#[state]
async fn empty(event: &Event) -> Response<State> {
match event {
Event::CdDetected => Transition(State::stopped()),
Event::OpenClose => Transition(State::open()),
_ => Super,
}
}

#[state]
async fn open(event: &Event) -> Response<State> {
match event {
Event::OpenClose => Transition(State::empty()),
_ => Super,
}
}

#[state]
async fn stopped(event: &Event) -> Response<State> {
match event {
Event::Play => Transition(State::playing()),
Event::OpenClose => Transition(State::open()),
Event::Stop => Transition(State::stopped()),
_ => Super,
}
}

#[state]
async fn playing(event: &Event) -> Response<State> {
match event {
Event::OpenClose => Transition(State::open()),
Event::Pause2 => Transition(State::pause()),
Event::Stop => Transition(State::stopped()),
_ => Super,
}
}

#[state]
async fn pause(event: &Event) -> Response<State> {
match event {
Event::EndPause => Transition(State::playing()),
Event::Stop => Transition(State::stopped()),
_ => Super,
}
}
}

async fn future_main() {
let mut state_machine = CdPlayer.uninitialized_state_machine().init().await;

let loops: u32 = 1_000_000;

println!("Loop count: {loops}");

let instant = Instant::now();

for _ in 0..loops {
let flag: bool = rand::random();

state_machine.handle(&Event::OpenClose).await;
state_machine.handle(&Event::OpenClose).await;
state_machine.handle(&Event::CdDetected).await;
state_machine.handle(&Event::Play).await;
state_machine.handle(&Event::Pause2).await;

state_machine.handle(&Event::EndPause).await;
state_machine.handle(&Event::Pause2).await;
state_machine.handle(&Event::Stop).await;

state_machine.handle(&Event::Stop).await;
state_machine.handle(&Event::OpenClose).await;
state_machine.handle(&Event::OpenClose).await;
}

let total_duration = instant.elapsed();
let loop_duration = total_duration.div_f64(loops as f64);
let million_loop_duration = loop_duration.mul_f64(1_000_000.0);

println!("Total duration: {total_duration:?}");
println!("Average loop duration: {loop_duration:?}");
println!("Duration 1M loops: {million_loop_duration:?}");

println!("Final state: {:?}", state_machine.state());
}

fn main() {
futures::executor::block_on(future_main());
}
11 changes: 11 additions & 0 deletions examples/macro/async_bench_complex/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "macro_async_bench_complex"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
statig = { path = "../../../statig", features = ["async"] }
rand = { version = "0.8.5", features = ["small_rng"] }
futures = { version = "*" }
Loading

0 comments on commit 6adedf4

Please sign in to comment.