Nutype 0.3.0
Changes
- [BREAKING]
min_lenandmax_lenvalidators run against number of characters in a string (val.chars().count()), not number of bytes (val.len()). - Add
finitevalidation for float types which checks against NaN and infinity. - Support deriving of
Default - Support deriving of
EqandOrdon float types (iffinitevalidation is present) - Support deriving of
TryFromfor types without validation (in this case Error type isstd::convert::Infallible)
Feature Highlights:
- Deriving Eq and Ord on f32 and f64 types: The new release addresses the limitation in Rust where
f32andf64types cannot implement theOrdandEqtraits due to the presence ofNaNvalues. Nutype introducesfinitevalidation, which allows the correct implementation ofEqandOrdtraits for float-based newtypes.
use nutype::nutype;
#[nutype(validate(finite))]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
struct Distance(f64);- Deriving Default: Nutype 0.3.0 introduces support for deriving the
Defaulttrait. This allows users to derive theDefaulttrait for their custom types with validation logic. Nutype also generates a unit test to ensure the validity of the default value.
use nutype::nutype;
#[nutype(
validate(with = |n| n % 2 == 1)
default = 1
)]
#[derive(Debug, Default)]
pub struct OddNumber(u64);Please note that dynamic validation makes it impossible to guarantee the validity of the default value at compile time. Panics will occur if an invalid default value is obtained.
For more details, refer to the Nutype documentation.
Blog post: Nutype 0.3.0 released