Nutype 0.4.3 - generic newtypes!
Serhii Potapov July 08, 2024 #rust #macro #newtype #nutype #genericsI'm excited to announce the latest update to Nutype: version 0.4.3! You can find the full release notes on GitHub.
What is Nutype?
Nutype is a Rust crate that enhances the newtype pattern. It leverages procedural macros to add features like sanitization and validation, ensuring values meet predefined criteria before instantiation. Unlike the regular newtype pattern, Nutype makes it practically impossible to instantiate a value of a given type that violates the defined constraints.
Support for Generics in Newtypes
This release introduces support for generics, which is particularly useful when you want to enforce constraints on collections. For example, the following is now possible:
use nutype;
;
// Non-empty vector of i32
let numbers = try_new.unwrap;
assert_eq!;
// Non-empty vector of chars
let chars = try_new.unwrap;
assert_eq!;
// It's not possible to instantiate an empty vector
let result = try_new;
assert_eq!;
As you can see, it is still possible to derive most other traits, including Serialize
and Deserialize
(requires the serde
feature to be enabled).
Setting Up Type Bounds
Let's extend the example above and turn it into SortedNonEmpty
. We will use sanitize
with sorting to ensure that the inner vector gets sorted when a value is instantiated.
;
This fails to compile:
14 | sanitize(with = |mut v| { v.sort(); v }),
| ^^^^ the trait `Ord` is not implemented for `T`
Vec::sort
requires T
to implement Ord
. Let's set this bound on our structure:
use Ord;
;
Now it compiles!
let names = try_new.unwrap;
assert_eq!;
I hope you find these updates helpful! If you do, please spread the word.