Skip to content

Commit 3d2f6ee

Browse files
committed
Enable as an inner type
1 parent 2f09b47 commit 3d2f6ee

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### v0.x.x - 2025-xx-xx
2-
- **[FEATURE]** Ability to instantiate types in `const` context, when declared with `const_fn` flag.
32
- **[BREAKING]** Fallible `::new()` constructor is removed (was deprecated in 0.4.3).
3+
- **[FEATURE]** Ability to instantiate types in `const` context, when declared with `const_fn` flag.
4+
- **[FIX]** Enable `&'a str` as an inner type.
45

56
### v0.5.1 - 2024-12-20
67

dummy/src/main.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
use nutype::nutype;
22

33
#[nutype(
4-
const_fn,
54
derive(Debug),
6-
validate(greater_or_equal = -273.15),
5+
validate(predicate = |name| !name.trim().is_empty())
76
)]
8-
pub struct Celsius(f64);
9-
10-
macro_rules! nutype_const {
11-
($name:ident, $ty:ty, $value:expr) => {
12-
const $name: $ty = match <$ty>::try_new($value) {
13-
Ok(value) => value,
14-
Err(_) => panic!("Invalid value"),
15-
};
16-
};
17-
}
18-
19-
nutype_const!(WATER_BOILING_POINT, Celsius, 100.0);
7+
pub struct Name<'a>(&'a str);
208

219
fn main() {
22-
println!("{:?}", WATER_BOILING_POINT);
10+
let name_error = Name::try_new(" ").unwrap_err();
11+
assert_eq!(name_error, NameError::PredicateViolated);
2312
}

nutype_macros/src/common/parse/meta.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,7 @@ pub fn parse_meta(token_stream: TokenStream) -> Result<Meta, syn::Error> {
6565
})?;
6666
validate_inner_field_visibility(&seg.vis)?;
6767

68-
let type_path = match seg.ty.clone() {
69-
syn::Type::Path(tp) => tp,
70-
_ => {
71-
let error = syn::Error::new(
72-
seg.span(),
73-
"#[nutype] requires a simple inner type (e.g. String, i32, etc.)",
74-
);
75-
return Err(error);
76-
}
77-
};
78-
79-
let type_path_str = type_path.into_token_stream().to_string();
68+
let type_path_str = seg.ty.clone().into_token_stream().to_string();
8069

8170
let inner_type = match type_path_str.as_ref() {
8271
"String" => InnerType::String(StringInnerType),

test_suite/tests/any.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1045,3 +1045,26 @@ mod constants {
10451045
assert_eq!(VALID.as_ref().y, 10);
10461046
}
10471047
}
1048+
1049+
mod str_reference {
1050+
use nutype::nutype;
1051+
1052+
#[nutype(
1053+
derive(Debug),
1054+
validate(predicate = |name| !name.trim().is_empty())
1055+
)]
1056+
pub struct Name<'a>(&'a str);
1057+
1058+
#[test]
1059+
fn test_validation_of_str_reference() {
1060+
{
1061+
let name_error = Name::try_new(" ").unwrap_err();
1062+
assert_eq!(name_error, NameError::PredicateViolated);
1063+
}
1064+
1065+
{
1066+
let name = Name::try_new("John").unwrap();
1067+
assert_eq!(name.into_inner(), "John");
1068+
}
1069+
}
1070+
}

0 commit comments

Comments
 (0)