Nutype 0.3.0
Changes
- [BREAKING]
min_len
andmax_len
validators run against number of characters in a string (val.chars().count()
), not number of bytes (val.len()
). - Add
finite
validation for float types which checks against NaN and infinity. - Support deriving of
Default
- Support deriving of
Eq
andOrd
on float types (iffinite
validation is present) - Support deriving of
TryFrom
for 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
f32
andf64
types cannot implement theOrd
andEq
traits due to the presence ofNaN
values. Nutype introducesfinite
validation, which allows the correct implementation ofEq
andOrd
traits 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
Default
trait. This allows users to derive theDefault
trait 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