Finite state machines in rust; bendns fork to add types.
implement `Error` and introduce `std` feature to be able to fall back to `no_std`
Yevhenii Babichenko 2021-02-24
parent 9f0f697 · commit e0ce6d6
-rw-r--r--CHANGELOG.md3
-rw-r--r--Cargo.toml4
-rw-r--r--README.md8
-rw-r--r--ensure_no_std/Cargo.toml2
-rw-r--r--src/lib.rs20
5 files changed, 35 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d57a05a..276ac1f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,9 @@ adheres to [Semantic Versioning][semver].
### Changed
* State transition error is now represented with `TransitionImpossibleError`
instead of `()`.
+* The library is not `no_std` by default due to the use of `std::error::Error`.
+ Users should disable the new `std` feature to use this library in a `no_std`
+ environment.
## [0.4.0] - 2020-08-25
### Added
diff --git a/Cargo.toml b/Cargo.toml
index 9831a38..44ec2ad 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,10 @@ version = "0.4.0"
authors = ["Yevhenii Babichenko"]
edition = "2018"
+[features]
+default = ["std"]
+std = []
+
[dependencies]
rust-fsm-dsl = { path = "./rust_fsm_dsl", version = "0.4.0" }
diff --git a/README.md b/README.md
index 0b6b647..9474657 100644
--- a/README.md
+++ b/README.md
@@ -30,6 +30,14 @@ of state machines:
* A Moore machine by providing an output function that do not depend on the
provided inputs.
+## Usage in `no_std` environments
+
+This library has the feature named `std` which is enabled by default. You
+may want to import this library as
+`rust-fsm = { version = "0.5", default-features = false }` to use it in a
+`no_std` environment. This only affects error types (the `Error` trait is
+only available in `std`).
+
## Use
Initially this library was designed to build an easy to use DSL for defining
diff --git a/ensure_no_std/Cargo.toml b/ensure_no_std/Cargo.toml
index 6db14fb..6cdcfc7 100644
--- a/ensure_no_std/Cargo.toml
+++ b/ensure_no_std/Cargo.toml
@@ -5,4 +5,4 @@ authors = ["Yevhenii Babichenko <[email protected]>"]
edition = "2018"
[dependencies]
-rust-fsm = { path = ".." }
+rust-fsm = { path = "..", default-features = false }
diff --git a/src/lib.rs b/src/lib.rs
index d750135..e8b78bb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,3 @@
-#![no_std]
//! A framework for building finite state machines in Rust
//!
//! The `rust-fsm` crate provides a simple and universal framework for building
@@ -28,6 +27,14 @@
//! * A Moore machine by providing an output function that do not depend on the
//! provided inputs.
//!
+//! # Usage in `no_std` environments
+//!
+//! This library has the feature named `std` which is enabled by default. You
+//! may want to import this library as
+//! `rust-fsm = { version = "0.5", default-features = false }` to use it in a
+//! `no_std` environment. This only affects error types (the `Error` trait is
+//! only available in `std`).
+//!
//! # Use
//!
//! Initially this library was designed to build an easy to use DSL for defining
@@ -113,7 +120,11 @@
//!
//! [repo]: https://github.com/eugene-babichenko/rust-fsm/blob/master/tests/circuit_breaker.rs
+#![cfg_attr(not(feature = "std"), no_std)]
+
use core::fmt;
+#[cfg(feature = "std")]
+use std::error::Error;
#[doc(hidden)]
pub use rust_fsm_dsl::*;
@@ -209,3 +220,10 @@ impl fmt::Display for TransitionImpossibleError {
)
}
}
+
+#[cfg(feature = "std")]
+impl Error for TransitionImpossibleError {
+ fn source(&self) -> Option<&(dyn Error + 'static)> {
+ None
+ }
+}