Skip to content

Commit ab85b90

Browse files
committed
Fix no_std_example
1 parent 5564559 commit ab85b90

File tree

6 files changed

+203
-14
lines changed

6 files changed

+203
-14
lines changed

Cargo.lock

-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,16 @@ members = [
77
"nutype_macros",
88
"test_suite",
99
"dummy",
10-
"examples/*",
10+
11+
# All examples except "no_std_example" are tested in the test suite
12+
"examples/any_arbitrary",
13+
"examples/float_arbitrary",
14+
"examples/float_sortable",
15+
"examples/integer_arbitrary",
16+
"examples/integer_bounded",
17+
"examples/new_unchecked_example",
18+
# "examples/no_std_example",
19+
"examples/serde_complex",
20+
"examples/string_bounded_len",
21+
"examples/string_regex_email",
1122
]

examples/no_std_example/Cargo.lock

+112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/no_std_example/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ edition = "2021"
77

88
[dependencies]
99
nutype = { path = "../../nutype", default-features = false }
10+
11+
# Exclude this package from the common workspace, since it's no_std.
12+
[workspace]

examples/no_std_example/src/main.rs

+62-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// This example exists to ensure that code generated by nutype macro
2+
// can compile in no_std environment.
13
#![no_main]
24
#![no_std]
35

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

12-
#[nutype(validate(greater_or_equal = 1, less_or_equal = 6), derive(Debug))]
14+
// Integer
15+
#[nutype(
16+
validate(greater_or_equal = 1, less_or_equal = 6),
17+
sanitize(with = |x| x),
18+
derive(
19+
Debug,
20+
Clone,
21+
PartialEq,
22+
Eq,
23+
PartialOrd,
24+
Ord,
25+
FromStr,
26+
AsRef,
27+
Deref,
28+
TryFrom,
29+
Into,
30+
Hash,
31+
Borrow,
32+
Display,
33+
Default,
34+
),
35+
default = 4
36+
)]
1337
struct GermanTaxClass(i64);
38+
39+
// Float
40+
#[nutype(
41+
validate(greater_or_equal = 0.0, less_or_equal = 1024.0, finite),
42+
sanitize(with = |x| x),
43+
derive(
44+
Debug,
45+
Clone,
46+
PartialEq,
47+
Eq,
48+
PartialOrd,
49+
Ord,
50+
FromStr,
51+
AsRef,
52+
Deref,
53+
TryFrom,
54+
Into,
55+
Borrow,
56+
Display,
57+
Default,
58+
),
59+
default = 0.0
60+
)]
61+
struct Width(f64);
62+
63+
// NOTE: strings are not working yet with no_std
64+
65+
// Any other type
66+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
67+
pub struct Point {
68+
x: i32,
69+
y: i32,
70+
}
71+
#[nutype(derive(
72+
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, AsRef, Into, From, Deref, Borrow, Hash
73+
))]
74+
pub struct Location(Point);

nutype_macros/src/common/gen/parse_error.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cfg_if::cfg_if;
12
use proc_macro2::TokenStream;
23
use quote::{format_ident, quote};
34

@@ -55,11 +56,19 @@ pub fn gen_def_parse_error(
5556
}
5657
};
5758

58-
let impl_std_error = quote! {
59-
impl ::std::error::Error for #parse_error_type_name {
60-
fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
61-
None
62-
}
59+
cfg_if! {
60+
if #[cfg(feature = "std")] {
61+
let impl_std_error = quote! {
62+
impl ::std::error::Error for #parse_error_type_name {
63+
fn source(&self) -> Option<&(dyn ::std::error::Error + 'static)> {
64+
None
65+
}
66+
}
67+
};
68+
} else {
69+
// NOTE: There is no `::core::error::Error` yet in stable Rust.
70+
// So for `no_std` we just don't implement `Error` trait.
71+
let impl_std_error = quote! {};
6372
}
6473
};
6574

0 commit comments

Comments
 (0)