Skip to content

Commit

Permalink
Minor adjustments to enable use in alloc environments without std.
Browse files Browse the repository at this point in the history
- Use hashbrown::HashMap instead of std::collections::HashMap for alloc compatibility.
- Remove standard library dependency from the async feature, enabling
  use in `no_std` environments when `alloc` present.
- Replace std::boxed::Box with alloc::boxed::Box in future types to support
  environments without the complete std library.
  • Loading branch information
peranpl1 committed Feb 9, 2024
1 parent 28a606c commit 3f4b3f7
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 15 deletions.
11 changes: 6 additions & 5 deletions macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ keywords = ["fsm", "hsm", "statechart", "state-machine", "embedded"]
proc-macro = true

[dependencies]
hashbrown = "0.14"
syn = { version = "1.0.107", features = [
"full",
"quote",
"extra-traits",
"visit",
"visit-mut",
"full",
"quote",
"extra-traits",
"visit",
"visit-mut",
] }
quote = "1.0.23"
proc-macro2 = "1.0.50"
Expand Down
2 changes: 1 addition & 1 deletion macro/src/analyze.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use hashbrown::HashMap;

use proc_macro_error::abort;
use syn::parse::Parser;
Expand Down
12 changes: 6 additions & 6 deletions macro/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ fn codegen_state_impl_state(ir: &Ir) -> ItemImpl {
shared_storage: &'fut mut #shared_storage_type,
#event_ident: &'fut <#shared_storage_type as statig::IntoStateMachine>::Event<'_>,
#context_ident: &'fut mut <#shared_storage_type as statig::IntoStateMachine>::Context<'_>
) -> core::pin::Pin<std::boxed::Box<dyn core::future::Future<Output = statig::Response<Self>> + 'fut + Send>> {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = statig::Response<Self>> + 'fut + Send>> {
Box::pin(async move {
match self {
#(#call_handler_arms),*
Expand All @@ -229,7 +229,7 @@ fn codegen_state_impl_state(ir: &Ir) -> ItemImpl {
&'fut mut self,
shared_storage: &'fut mut #shared_storage_type,
#context_ident: &'fut mut <#shared_storage_type as statig::IntoStateMachine>::Context<'_>
) -> core::pin::Pin<std::boxed::Box<dyn core::future::Future<Output = ()> + 'fut + Send>> {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = ()> + 'fut + Send>> {
Box::pin(async move {
match self {
#(#call_entry_action_arms),*
Expand All @@ -241,7 +241,7 @@ fn codegen_state_impl_state(ir: &Ir) -> ItemImpl {
&'fut mut self,
shared_storage: &'fut mut #shared_storage_type,
#context_ident: &'fut mut <#shared_storage_type as statig::IntoStateMachine>::Context<'_>
) -> core::pin::Pin<std::boxed::Box<dyn core::future::Future<Output = ()> + 'fut + Send>> {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = ()> + 'fut + Send>> {
Box::pin(async move {
match self {
#(#call_exit_action_arms),*
Expand Down Expand Up @@ -378,7 +378,7 @@ fn codegen_superstate_impl_superstate(ir: &Ir) -> ItemImpl {
shared_storage: &'fut mut #shared_storage_type,
#event_ident: &'fut <#shared_storage_type as statig::IntoStateMachine>::Event<'_>,
#context_ident: &'fut mut <#shared_storage_type as statig::IntoStateMachine>::Context<'_>
) -> core::pin::Pin<std::boxed::Box<dyn core::future::Future<Output = statig::Response<<#shared_storage_type as statig::IntoStateMachine>::State>> + 'fut + Send>> {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = statig::Response<<#shared_storage_type as statig::IntoStateMachine>::State>> + 'fut + Send>> {
Box::pin(async move {
match self {
#(#call_handler_arms),*
Expand All @@ -390,7 +390,7 @@ fn codegen_superstate_impl_superstate(ir: &Ir) -> ItemImpl {
&'fut mut self,
shared_storage: &'fut mut #shared_storage_type,
#context_ident: &'fut mut <#shared_storage_type as statig::IntoStateMachine>::Context<'_>
) -> core::pin::Pin<std::boxed::Box<dyn core::future::Future<Output = ()> + 'fut + Send>> {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = ()> + 'fut + Send>> {
Box::pin(async move {
match self {
#(#call_entry_action_arms),*
Expand All @@ -402,7 +402,7 @@ fn codegen_superstate_impl_superstate(ir: &Ir) -> ItemImpl {
&'fut mut self,
shared_storage: &'fut mut #shared_storage_type,
#context_ident: &'fut mut <#shared_storage_type as statig::IntoStateMachine>::Context<'_>
) -> core::pin::Pin<std::boxed::Box<dyn core::future::Future<Output = ()> + 'fut + Send>> {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = ()> + 'fut + Send>> {
Box::pin(async move {
match self {
#(#call_exit_action_arms),*
Expand Down
4 changes: 2 additions & 2 deletions macro/src/lower.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::ops::Deref;
use core::ops::Deref;
use hashbrown::HashMap;

use proc_macro2::Span;
use proc_macro_error::abort;
Expand Down
2 changes: 1 addition & 1 deletion statig/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ default = ["macro"]
macro = ["statig_macro"]
serde = ["dep:serde"]
bevy = ["dep:bevy_ecs"]
async = ["std"]
async = []
std = []
3 changes: 3 additions & 0 deletions statig/src/awaitable/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
extern crate alloc;
use alloc::boxed::Box;

use core::future::Future;
use core::pin::Pin;

Expand Down
3 changes: 3 additions & 0 deletions statig/src/awaitable/superstate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
extern crate alloc;
use alloc::boxed::Box;

use core::cmp::Ordering;
use core::future::Future;
use core::pin::Pin;
Expand Down

0 comments on commit 3f4b3f7

Please sign in to comment.