Skip to content

Commit

Permalink
Merge pull request #14 from not-matthias/master
Browse files Browse the repository at this point in the history
feat: `no_std` support
  • Loading branch information
greyblake authored Mar 16, 2024
2 parents 3f43c7d + fad7e38 commit f781eee
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
4 changes: 4 additions & 0 deletions kinded/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
8 changes: 7 additions & 1 deletion kinded/src/errors.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<KindType>(given_string: String) -> ParseKindError {
let full_kind_type_name = std::any::type_name::<KindType>();
let full_kind_type_name = core::any::type_name::<KindType>();
let kind_type_name = full_kind_type_name
.split("::")
.last()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions kinded/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@
//!
//! MIT © [Serhii Potapov](https://www.greyblake.com)
#![cfg_attr(feature = "no_std", no_std)]

mod errors;
mod traits;

Expand Down
5 changes: 3 additions & 2 deletions kinded_macros/src/gen/kind_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
} // }
Expand Down Expand Up @@ -127,6 +127,7 @@ fn gen_impl_from_str_trait(meta: &Meta) -> TokenStream {
} // }

// If still no success, then return an error
use alloc::borrow::ToOwned;
let error = ::kinded::ParseKindError::from_type_and_string::<#kind_name>(s.to_owned());
Err(error)
}
Expand Down

0 comments on commit f781eee

Please sign in to comment.