From c584ee229453e0b478e0e306b121b9bf600233e7 Mon Sep 17 00:00:00 2001 From: stifskere Date: Tue, 1 Jul 2025 03:12:04 +0200 Subject: [PATCH 1/4] feat: allow no_std --- Cargo.toml | 5 +++++ src/lib.rs | 12 +++++++++--- src/test.rs | 5 ++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cab0c76..777b44f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,3 +18,8 @@ keywords = [ [dev-dependencies] serde_json = "1.0.140" + +[features] +default = ["std"] +std = [] +alloc = [] diff --git a/src/lib.rs b/src/lib.rs index 558f805..73e347f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,8 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(all(not(feature = "std"), feature = "alloc"))] +extern crate alloc; + #[cfg(test)] mod test; @@ -59,7 +64,7 @@ macro_rules! dyn_access { }}; (@recurse $acc:expr, . $field:ident $($rest:tt)*) => {{ - let __ = $acc.and_then(|v| v.get(::std::stringify!($field))); + let __ = $acc.and_then(|v| v.get(::core::stringify!($field))); $crate::dyn_access!(@recurse __, $($rest)*) }}; @@ -89,11 +94,12 @@ macro_rules! dyn_access { /// assert_eq!(display_path, r#"nested.path.at[2].with["no"]["head"]"#); /// ``` /// Notice how the macro pre-computes the indexes and generates the target string. +#[cfg(feature = "alloc")] #[macro_export] macro_rules! dyn_path { ($head:ident $($rest:tt)*) => {{ - use ::std::fmt::Write; - let mut __ = ::std::stringify!($head).to_string(); + use ::core::fmt::Write; + let mut __ = ::core::stringify!($head).to_string(); $crate::dyn_path!(@recurse __, $($rest)*) }}; diff --git a/src/test.rs b/src/test.rs index 771df7d..3e7e2de 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,6 +1,8 @@ use serde_json::{json, Value}; -use crate::{dyn_access, dyn_path}; +use crate::dyn_access; +#[cfg(feature = "alloc")] +use crate::dyn_path; const ERROR: &str = "nested value to exist."; @@ -52,6 +54,7 @@ pub fn direct_expression() { assert_eq!(_1, "of"); } +#[cfg(feature = "alloc")] #[test] pub fn path_descriptor() { let _1 = dyn_path!(very.nested["value"].on.index[1 + 1]); From fe68c0e1ff4e60c377f2fdc7780b56341623e379 Mon Sep 17 00:00:00 2001 From: stifskere Date: Tue, 1 Jul 2025 03:17:08 +0200 Subject: [PATCH 2/4] fix: gate logic --- src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 73e347f..2713e43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -94,7 +94,7 @@ macro_rules! dyn_access { /// assert_eq!(display_path, r#"nested.path.at[2].with["no"]["head"]"#); /// ``` /// Notice how the macro pre-computes the indexes and generates the target string. -#[cfg(feature = "alloc")] +#[cfg(any(feature = "alloc", feature = "std"))] #[macro_export] macro_rules! dyn_path { ($head:ident $($rest:tt)*) => {{ @@ -104,12 +104,12 @@ macro_rules! dyn_path { }}; (@recurse $acc:expr, . $field:ident $($rest:tt)*) => {{ - let _ = ::std::write!($acc, ".{}", ::std::stringify!($field)); + let _ = ::core::write!($acc, ".{}", ::core::stringify!($field)); $crate::dyn_path!(@recurse $acc, $($rest)*) }}; (@recurse $acc:expr, [$idx:expr] $($rest:tt)*) => {{ - let _ = ::std::write!($acc, "[{:?}]", ($idx)); + let _ = ::core::write!($acc, "[{:?}]", ($idx)); $crate::dyn_path!(@recurse $acc, $($rest)*) }}; From 9de856d047f49382e7a758714f5e7e99a4672ef3 Mon Sep 17 00:00:00 2001 From: stifskere Date: Tue, 1 Jul 2025 03:22:03 +0200 Subject: [PATCH 3/4] fix: solve alloc logic with no_std --- src/lib.rs | 3 ++- src/test.rs | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2713e43..f56c6c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] #[cfg(all(not(feature = "std"), feature = "alloc"))] -extern crate alloc; +pub extern crate alloc; #[cfg(test)] mod test; @@ -99,6 +99,7 @@ macro_rules! dyn_access { macro_rules! dyn_path { ($head:ident $($rest:tt)*) => {{ use ::core::fmt::Write; + use $crate::alloc::string::ToString; let mut __ = ::core::stringify!($head).to_string(); $crate::dyn_path!(@recurse __, $($rest)*) }}; diff --git a/src/test.rs b/src/test.rs index 3e7e2de..2a22307 100644 --- a/src/test.rs +++ b/src/test.rs @@ -47,8 +47,8 @@ pub fn nested_access() { #[test] pub fn direct_expression() { - let vector = vec![map()]; - let _1 = dyn_access!((vector[0]).very.nested[1]) + let vector = (map(), ()); + let _1 = dyn_access!((vector.0).very.nested[1]) .expect(ERROR); assert_eq!(_1, "of"); From d642db31aa4594bd760f77d261634597659de1d2 Mon Sep 17 00:00:00 2001 From: stifskere Date: Tue, 1 Jul 2025 03:24:26 +0200 Subject: [PATCH 4/4] chore: bump version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 00bd498..6c846a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 4 [[package]] name = "dyn_path" -version = "1.0.0" +version = "1.0.1" dependencies = [ "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 777b44f..a242698 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ description = "A JavaScript-like nested object-like structure non-fallible path repository = "https://github.com/FlakySL/dyn_path" license = "GPL-3.0" readme = "./README.md" -version = "1.0.0" +version = "1.0.1" edition = "2024" authors = ["Esteve Autet ", "Chiko "] keywords = [