Skip to content

Commit

Permalink
Fix no_std_example
Browse files Browse the repository at this point in the history
  • Loading branch information
greyblake committed Jan 28, 2024
1 parent 5564559 commit ab85b90
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 14 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@ members = [
"nutype_macros",
"test_suite",
"dummy",
"examples/*",

# All examples except "no_std_example" are tested in the test suite
"examples/any_arbitrary",
"examples/float_arbitrary",
"examples/float_sortable",
"examples/integer_arbitrary",
"examples/integer_bounded",
"examples/new_unchecked_example",
# "examples/no_std_example",
"examples/serde_complex",
"examples/string_bounded_len",
"examples/string_regex_email",
]
112 changes: 112 additions & 0 deletions examples/no_std_example/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/no_std_example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ edition = "2021"

[dependencies]
nutype = { path = "../../nutype", default-features = false }

# Exclude this package from the common workspace, since it's no_std.
[workspace]
63 changes: 62 additions & 1 deletion examples/no_std_example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// This example exists to ensure that code generated by nutype macro
// can compile in no_std environment.
#![no_main]
#![no_std]

Expand All @@ -9,5 +11,64 @@ fn panic(_panic: &PanicInfo<'_>) -> ! {
loop {}
}

#[nutype(validate(greater_or_equal = 1, less_or_equal = 6), derive(Debug))]
// Integer
#[nutype(
validate(greater_or_equal = 1, less_or_equal = 6),
sanitize(with = |x| x),
derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
FromStr,
AsRef,
Deref,
TryFrom,
Into,
Hash,
Borrow,
Display,
Default,
),
default = 4
)]
struct GermanTaxClass(i64);

// Float
#[nutype(
validate(greater_or_equal = 0.0, less_or_equal = 1024.0, finite),
sanitize(with = |x| x),
derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
FromStr,
AsRef,
Deref,
TryFrom,
Into,
Borrow,
Display,
Default,
),
default = 0.0
)]
struct Width(f64);

// NOTE: strings are not working yet with no_std

// Any other type
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Point {
x: i32,
y: i32,
}
#[nutype(derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, AsRef, Into, From, Deref, Borrow, Hash
))]
pub struct Location(Point);
19 changes: 14 additions & 5 deletions nutype_macros/src/common/gen/parse_error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use cfg_if::cfg_if;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

Expand Down Expand Up @@ -55,11 +56,19 @@ pub fn gen_def_parse_error(
}
};

let impl_std_error = quote! {
impl ::std::error::Error for #parse_error_type_name {
fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
None
}
cfg_if! {
if #[cfg(feature = "std")] {
let impl_std_error = quote! {
impl ::std::error::Error for #parse_error_type_name {
fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
None
}
}
};
} else {
// NOTE: There is no `::core::error::Error` yet in stable Rust.
// So for `no_std` we just don't implement `Error` trait.
let impl_std_error = quote! {};
}
};

Expand Down

0 comments on commit ab85b90

Please sign in to comment.