From e169cd6c96adc4ba7fdafef1b8e8ed65e657db30 Mon Sep 17 00:00:00 2001 From: Matthias <26800596+not-matthias@users.noreply.github.com> Date: Fri, 22 Mar 2024 09:17:43 +0100 Subject: [PATCH 1/3] fix: tests by adding alloc --- kinded_macros/src/gen/kind_enum.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/kinded_macros/src/gen/kind_enum.rs b/kinded_macros/src/gen/kind_enum.rs index bba6b69..d0e0ded 100644 --- a/kinded_macros/src/gen/kind_enum.rs +++ b/kinded_macros/src/gen/kind_enum.rs @@ -127,6 +127,7 @@ fn gen_impl_from_str_trait(meta: &Meta) -> TokenStream { } // } // If still no success, then return an error + extern crate alloc; use alloc::borrow::ToOwned; let error = ::kinded::ParseKindError::from_type_and_string::<#kind_name>(s.to_owned()); Err(error) From c07b57af799fe9994a165d25d836c03a2beb7bef Mon Sep 17 00:00:00 2001 From: Matthias <26800596+not-matthias@users.noreply.github.com> Date: Fri, 29 Mar 2024 15:21:22 +0100 Subject: [PATCH 2/3] Reapply "feat: `no_std` support" This reverts commit ee5421b667c3798188666d4d5fc33aa54aebe0a8. --- kinded/Cargo.toml | 4 ++++ kinded/src/errors.rs | 8 +++++++- kinded/src/lib.rs | 2 ++ kinded_macros/src/gen/kind_enum.rs | 4 ++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/kinded/Cargo.toml b/kinded/Cargo.toml index 0c619d2..f1d6efc 100644 --- a/kinded/Cargo.toml +++ b/kinded/Cargo.toml @@ -15,5 +15,9 @@ categories = ["data-structures", "rust-patterns"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = [] +no_std = [] + [dependencies] kinded_macros = { version = "0.3.0", path = "../kinded_macros" } diff --git a/kinded/src/errors.rs b/kinded/src/errors.rs index a6a42d8..ed24c73 100644 --- a/kinded/src/errors.rs +++ b/kinded/src/errors.rs @@ -1,3 +1,8 @@ +extern crate alloc; + +#[cfg(feature = "no_std")] +use alloc::string::{String, ToString}; + /// An error which is returned when parsing of a kind type failures. pub struct ParseKindError { kind_type_name: String, @@ -8,7 +13,7 @@ impl ParseKindError { /// This method is used by `kinded` macro to construct an error for FromStr trait and is not /// recommend for a direct usage by users. pub fn from_type_and_string(given_string: String) -> ParseKindError { - let full_kind_type_name = std::any::type_name::(); + let full_kind_type_name = core::any::type_name::(); let kind_type_name = full_kind_type_name .split("::") .last() @@ -37,6 +42,7 @@ impl ::core::fmt::Debug for ParseKindError { } } +#[cfg(not(feature = "no_std"))] impl ::std::error::Error for ParseKindError { fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> { None diff --git a/kinded/src/lib.rs b/kinded/src/lib.rs index 91ec7a1..8107e85 100644 --- a/kinded/src/lib.rs +++ b/kinded/src/lib.rs @@ -187,6 +187,8 @@ //! //! MIT © [Serhii Potapov](https://www.greyblake.com) +#![cfg_attr(feature = "no_std", no_std)] + mod errors; mod traits; diff --git a/kinded_macros/src/gen/kind_enum.rs b/kinded_macros/src/gen/kind_enum.rs index 2e63fce..d0e0ded 100644 --- a/kinded_macros/src/gen/kind_enum.rs +++ b/kinded_macros/src/gen/kind_enum.rs @@ -74,8 +74,8 @@ fn gen_impl_display_trait(meta: &Meta) -> TokenStream { }); quote!( - impl std::fmt::Display for #kind_name { // impl std::fmt::Display for DrinkKind { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + impl core::fmt::Display for #kind_name { // impl core::fmt::Display for DrinkKind { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { // fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { match self { // match self { #(#match_branches),* // DrinkKind::Mate => write!(f, "mate"), } // } From 041fd2197e2e0edc5c2f1a9661ddf5bf9ae6bd88 Mon Sep 17 00:00:00 2001 From: Matthias <26800596+not-matthias@users.noreply.github.com> Date: Fri, 29 Mar 2024 15:28:14 +0100 Subject: [PATCH 3/3] feat: remove features and convert tests to nostd --- kinded/Cargo.toml | 4 ---- kinded/src/errors.rs | 8 -------- kinded/src/lib.rs | 2 +- test_suite/src/lib.rs | 22 ++++++++++++++-------- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/kinded/Cargo.toml b/kinded/Cargo.toml index f1d6efc..0c619d2 100644 --- a/kinded/Cargo.toml +++ b/kinded/Cargo.toml @@ -15,9 +15,5 @@ categories = ["data-structures", "rust-patterns"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[features] -default = [] -no_std = [] - [dependencies] kinded_macros = { version = "0.3.0", path = "../kinded_macros" } diff --git a/kinded/src/errors.rs b/kinded/src/errors.rs index ed24c73..e402d20 100644 --- a/kinded/src/errors.rs +++ b/kinded/src/errors.rs @@ -1,6 +1,5 @@ extern crate alloc; -#[cfg(feature = "no_std")] use alloc::string::{String, ToString}; /// An error which is returned when parsing of a kind type failures. @@ -41,10 +40,3 @@ impl ::core::fmt::Debug for ParseKindError { write!(f, "ParseKindError: {self}") } } - -#[cfg(not(feature = "no_std"))] -impl ::std::error::Error for ParseKindError { - fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> { - None - } -} diff --git a/kinded/src/lib.rs b/kinded/src/lib.rs index 8107e85..501ed71 100644 --- a/kinded/src/lib.rs +++ b/kinded/src/lib.rs @@ -187,7 +187,7 @@ //! //! MIT © [Serhii Potapov](https://www.greyblake.com) -#![cfg_attr(feature = "no_std", no_std)] +#![no_std] mod errors; mod traits; diff --git a/test_suite/src/lib.rs b/test_suite/src/lib.rs index a487172..128f09e 100644 --- a/test_suite/src/lib.rs +++ b/test_suite/src/lib.rs @@ -1,5 +1,8 @@ #![allow(unused_imports)] #![allow(dead_code)] +#![no_std] + +extern crate alloc; use kinded::Kinded; @@ -58,6 +61,9 @@ mod kind_enum { use super::RoleKind; mod traits { + extern crate alloc; + use alloc::format; + use super::super::{Role, RoleKind}; #[test] @@ -96,6 +102,9 @@ mod kind_enum { } mod display_trait { + extern crate alloc; + use alloc::{format, string::ToString}; + use super::RoleKind; #[test] @@ -208,6 +217,9 @@ mod kind_enum { } mod from_str_trait { + extern crate alloc; + use alloc::string::ToString; + #[derive(kinded::Kinded)] enum Mate { HotMate, @@ -307,23 +319,19 @@ fn should_allow_to_give_custom_name_kind_type() { #[test] fn should_allow_to_derive_custom_traits() { - use std::collections::HashMap; - #[derive(Kinded)] - #[kinded(derive(Hash, Eq))] + #[kinded(derive(Hash, Eq, PartialOrd, Ord))] enum Drink { Tea(&'static str), Coffee(&'static str), } - let mut drinks = HashMap::new(); + let mut drinks = alloc::collections::BTreeMap::new(); drinks.insert(DrinkKind::Tea, 5); } #[test] fn should_work_with_generics() { - use std::collections::HashMap; - #[derive(Kinded)] enum Maybe { Just(T), @@ -335,8 +343,6 @@ fn should_work_with_generics() { #[test] fn should_work_with_lifetimes() { - use std::collections::HashMap; - #[derive(Kinded)] enum Identifier<'a, I> { Name(&'a str),