Nutype 0.4.2 released!

Release notes: Release Nutype 0.4.2 · greyblake/nutype · GitHub

Nutype is a proc macro that allows adding extra constraints like sanitization and validation to the regular newtype pattern.

In this particular release support for derive of Arbitrary trait is added, what facilitates property-based testing and fuzzing.

use nutype::nutype;
use arbtest::arbtest;
use arbitrary::Arbitrary;

#[nutype(
    derive(Arbitrary, AsRef),
    validate(
        finite,
        greater_or_equal = -1.0,
        less_or_equal = 1.0,
    ),
)]
pub struct PearsonCorrelation(f64);

fn main() {
    arbtest(|u| {
        // Arbitrary generates only valid values of PearsonCorrelation
        let correlation = PearsonCorrelation::arbitrary(u)?;
        assert!(correlation.as_ref().is_finite());
        assert!(*correlation.as_ref() >= -1.0);
        assert!(*correlation.as_ref() <= 1.0);
        Ok(())
    });
}
4 Likes

This looks really useful! I’m fairly new to Rust but have been casually on the lookout for a quick and clean way to (say) create a non empty string type. Am bookmarking and will definitely be using Nutype in the near future

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.