Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add nostd #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions kinded/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
extern crate alloc;

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 +12,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 @@ -36,9 +40,3 @@ impl ::core::fmt::Debug for ParseKindError {
write!(f, "ParseKindError: {self}")
}
}

impl ::std::error::Error for ParseKindError {
fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
None
}
}
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)

#![no_std]

mod errors;
mod traits;

Expand Down
6 changes: 4 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,8 @@ 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)
}
Expand Down
22 changes: 14 additions & 8 deletions test_suite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(unused_imports)]
#![allow(dead_code)]
#![no_std]

extern crate alloc;

use kinded::Kinded;

Expand Down Expand Up @@ -58,6 +61,9 @@ mod kind_enum {
use super::RoleKind;

mod traits {
extern crate alloc;
use alloc::format;

use super::super::{Role, RoleKind};

#[test]
Expand Down Expand Up @@ -96,6 +102,9 @@ mod kind_enum {
}

mod display_trait {
extern crate alloc;
use alloc::{format, string::ToString};

use super::RoleKind;

#[test]
Expand Down Expand Up @@ -208,6 +217,9 @@ mod kind_enum {
}

mod from_str_trait {
extern crate alloc;
use alloc::string::ToString;

#[derive(kinded::Kinded)]
enum Mate {
HotMate,
Expand Down Expand Up @@ -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<T> {
Just(T),
Expand All @@ -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),
Expand Down
Loading