Skip to content

Nutype 0.3.0

Compare
Choose a tag to compare
@greyblake greyblake released this 25 Jun 09:55
· 199 commits to master since this release

Changes

  • [BREAKING] min_len and max_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 and Ord on float types (if finite validation is present)
  • Support deriving of TryFrom for types without validation (in this case Error type is std::convert::Infallible)

Feature Highlights:

  • Deriving Eq and Ord on f32 and f64 types: The new release addresses the limitation in Rust where f32 and f64 types cannot implement the Ord and Eq traits due to the presence of NaN values. Nutype introduces finite validation, which allows the correct implementation of Eq and Ord 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 the Default 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