Finite state machines in rust; bendns fork to add types.
rust_fsm: switch to a more conventional naming for the bridge pattern
* trait StateMachine -> StateMachineImpl * struct StateMachineWrapper -> StateMachine
Yevhenii Babichenko 2019-05-07
parent 8c0f496 · commit 8ac2cb6
-rw-r--r--README.md10
-rw-r--r--rust_fsm/examples/circuit_breaker.rs4
-rw-r--r--rust_fsm/examples/circuit_breaker_dsl.rs2
-rw-r--r--rust_fsm/examples/simple.rs2
-rw-r--r--rust_fsm/src/lib.rs26
-rw-r--r--rust_fsm_dsl/src/lib.rs2
6 files changed, 23 insertions, 23 deletions
diff --git a/README.md b/README.md
index 6d9eb1b..b84769b 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@ components:
parses a simple DSL and generates all boilerplate code for the described
state machine.
-The essential part of this crate is the [`StateMachine`] trait. This trait
+The essential part of this crate is the [`StateMachineImpl`] trait. This trait
allows a developer to provide a strict state machine definition, e.g.
specify its:
@@ -77,7 +77,7 @@ This state machine can be used as follows:
```rust
// Initialize the state machine. The state is `Closed` now.
-let mut machine: StateMachineWrapper<CircuitBreaker> = StateMachineWrapper::new();
+let mut machine: StateMachine<CircuitBreaker> = StateMachine::new();
// Consume the `Successful` input. No state transition is performed.
let _ = machine.consume(&CircuitBreakerInput::Successful);
// Consume the `Unsuccesful` input. The machine is moved to the `Open`
@@ -95,7 +95,7 @@ if machine.state() == &CircuitBreakerState::Open {
As you can see, the following entities are generated:
-* An empty structure `CircuitBreaker` that implements the `StateMachine`
+* An empty structure `CircuitBreaker` that implements the `StateMachineImpl`
trait.
* Enums `CircuitBreakerState`, `CircuitBreakerInput` and
`CircuitBreakerOutput` that represent the state, the input alphabet and
@@ -112,8 +112,8 @@ cannot carry any additional data), so in certain complex cases a user might
want to write a more complex state machine by hand.
All you need to do to build a state machine is to implement the
-`StateMachine` trait and use it in conjuctions with some of the provided
-wrappers (for now there is only `StateMachineWrapper`).
+`StateMachineImpl` trait and use it in conjuctions with some of the provided
+wrappers (for now there is only `StateMachine`).
You can see an example of the Circuit Breaker state machine in the
[project repository][repo].
diff --git a/rust_fsm/examples/circuit_breaker.rs b/rust_fsm/examples/circuit_breaker.rs
index e318b30..e0bcbbe 100644
--- a/rust_fsm/examples/circuit_breaker.rs
+++ b/rust_fsm/examples/circuit_breaker.rs
@@ -25,7 +25,7 @@ struct CircuitBreakerOutputSetTimer;
#[derive(Debug)]
struct CircuitBreakerMachine;
-impl StateMachine for CircuitBreakerMachine {
+impl StateMachineImpl for CircuitBreakerMachine {
type Input = CircuitBreakerInput;
type State = CircuitBreakerState;
type Output = CircuitBreakerOutputSetTimer;
@@ -63,7 +63,7 @@ impl StateMachine for CircuitBreakerMachine {
}
fn main() {
- let machine: StateMachineWrapper<CircuitBreakerMachine> = StateMachineWrapper::new();
+ let machine: StateMachine<CircuitBreakerMachine> = StateMachine::new();
// Unsuccessful request
let machine = Arc::new(Mutex::new(machine));
diff --git a/rust_fsm/examples/circuit_breaker_dsl.rs b/rust_fsm/examples/circuit_breaker_dsl.rs
index ecdc080..2860601 100644
--- a/rust_fsm/examples/circuit_breaker_dsl.rs
+++ b/rust_fsm/examples/circuit_breaker_dsl.rs
@@ -18,7 +18,7 @@ state_machine! {
}
fn main() {
- let machine: StateMachineWrapper<CircuitBreaker> = StateMachineWrapper::new();
+ let machine: StateMachine<CircuitBreaker> = StateMachine::new();
// Unsuccessful request
let machine = Arc::new(Mutex::new(machine));
diff --git a/rust_fsm/examples/simple.rs b/rust_fsm/examples/simple.rs
index 46ef7be..67b4fe1 100644
--- a/rust_fsm/examples/simple.rs
+++ b/rust_fsm/examples/simple.rs
@@ -13,7 +13,7 @@ state_machine! {
}
fn main() {
- let mut machine: StateMachineWrapper<Door> = StateMachineWrapper::new();
+ let mut machine: StateMachine<Door> = StateMachine::new();
machine.consume(&DoorInput::Key).unwrap();
println!("{:?}", machine.state());
machine.consume(&DoorInput::Key).unwrap();
diff --git a/rust_fsm/src/lib.rs b/rust_fsm/src/lib.rs
index a45beab..d842311 100644
--- a/rust_fsm/src/lib.rs
+++ b/rust_fsm/src/lib.rs
@@ -10,8 +10,8 @@
//! parses a simple DSL and generates all boilerplate code for the described
//! state machine.
//!
-//! The essential part of this crate is the [`StateMachine`] trait. This trait
-//! allows a developer to provide a strict state machine definition, e.g.
+//! The essential part of this crate is the [`StateMachineImpl`] trait. This
+//! trait allows a developer to provide a strict state machine definition, e.g.
//! specify its:
//!
//! * An input alphabet - a set of entities that the state machine takes as
@@ -75,7 +75,7 @@
//!
//! ```rust,ignore
//! // Initialize the state machine. The state is `Closed` now.
-//! let mut machine: StateMachineWrapper<CircuitBreaker> = StateMachineWrapper::new();
+//! let mut machine: StateMachine<CircuitBreaker> = StateMachine::new();
//! // Consume the `Successful` input. No state transition is performed.
//! let _ = machine.consume(&CircuitBreakerInput::Successful);
//! // Consume the `Unsuccesful` input. The machine is moved to the `Open`
@@ -93,7 +93,7 @@
//!
//! As you can see, the following entities are generated:
//!
-//! * An empty structure `CircuitBreaker` that implements the `StateMachine`
+//! * An empty structure `CircuitBreaker` that implements the `StateMachineImpl`
//! trait.
//! * Enums `CircuitBreakerState`, `CircuitBreakerInput` and
//! `CircuitBreakerOutput` that represent the state, the input alphabet and
@@ -110,8 +110,8 @@
//! want to write a more complex state machine by hand.
//!
//! All you need to do to build a state machine is to implement the
-//! `StateMachine` trait and use it in conjuctions with some of the provided
-//! wrappers (for now there is only `StateMachineWrapper`).
+//! `StateMachineImpl` trait and use it in conjuctions with some of the provided
+//! wrappers (for now there is only `StateMachine`).
//!
//! You can see an example of the Circuit Breaker state machine in the
//! [project repository][repo].
@@ -122,7 +122,7 @@
/// machine/transducer. This is just a formal definition that may be
/// inconvenient to be used in practical programming, but it is used throughout
/// this library for more practical things.
-pub trait StateMachine {
+pub trait StateMachineImpl {
/// The input alphabet.
type Input;
/// The set of possible states.
@@ -143,18 +143,18 @@ pub trait StateMachine {
/// A convenience wrapper around the `StateMachine` trait that encapsulates the
/// state and transition and output function calls.
-pub struct StateMachineWrapper<T: StateMachine> {
+pub struct StateMachine<T: StateMachineImpl> {
state: T::State,
}
-impl<T> StateMachineWrapper<T>
+impl<T> StateMachine<T>
where
- T: StateMachine,
+ T: StateMachineImpl,
{
/// Create a new instance of this wrapper which encapsulates the initial
/// state.
pub fn new() -> Self {
- StateMachineWrapper {
+ Self {
state: T::INITIAL_STATE,
}
}
@@ -181,9 +181,9 @@ where
}
}
-impl<T> Default for StateMachineWrapper<T>
+impl<T> Default for StateMachine<T>
where
- T: StateMachine,
+ T: StateMachineImpl,
{
fn default() -> Self {
Self::new()
diff --git a/rust_fsm_dsl/src/lib.rs b/rust_fsm_dsl/src/lib.rs
index 449c703..fba3122 100644
--- a/rust_fsm_dsl/src/lib.rs
+++ b/rust_fsm_dsl/src/lib.rs
@@ -168,7 +168,7 @@ pub fn state_machine(tokens: TokenStream) -> TokenStream {
#outputs_repr
- impl rust_fsm::StateMachine for #struct_name {
+ impl rust_fsm::StateMachineImpl for #struct_name {
type Input = #inputs_enum_name;
type State = #states_enum_name;
type Output = #outputs_type;