Nutype (rust crate) 0.6.2 released(github.com) |
Nutype (rust crate) 0.6.2 released(github.com) |
*nutype* is a procedural macro that adds sanitization and validation to newtypes, ensuring that values always meet your defined constraints.
## What’s new in v0.6.2
* *\[FEATURE]* Added `derive_unsafe(..)` for deriving arbitrary traits (requires enabling the `derive_unsafe` feature). * *\[FIX]* Upgraded Rust edition: *2021 → 2024*. * *\[FIX]* Improved error messages for `len_char_max` and `len_char_min`—they’re now clearer and easier to interpret.
## About `derive_unsafe`
With `derive_unsafe(..)`, you can now derive *any trait*, including third-party ones that nutype doesn’t recognize.
Unlike the safe `derive(..)`, this skips nutype’s internal checks. That means *you can accidentally break your validations at runtime*—for example, by enabling mutation via `DerefMut`.
To use this feature, add the `derive_unsafe` flag.
*Cautionary example:*
```rust use derive_more::{Deref, DerefMut}; use nutype::nutype;
#[nutype( derive(Debug, AsRef), derive_unsafe(Deref, DerefMut), validate(greater_or_equal = 0.0, less_or_equal = 2.0) )] struct LlmTemperature(f64);
fn main() { let mut temperature = LlmTemperature::try_new(1.5).unwrap();
// This is exactly what you shouldn't do!
*temperature = 2.5;
// Validation rule? Violated.
assert_eq!(temperature.as_ref(), &2.5);
}
```*TL;DR:* `derive_unsafe` gives you freedom—but with that comes full responsibility. Use wisely.