NNN: a new take on nutype
Hi,
TL/DR: I made a library to create new-types with guarantees https://github.com/vic1707/nnn
A few months ago I stumbled upon nutype (https://github.com/greyblake/nutype) and absolutely loved it.
I started using it everywhere I could and made sense, encountered a few issues, contributed a bit but I was missing some features, conditional derives https://github.com/greyblake/nutype/issues/188 and one to be able to make associated consts: https://github.com/greyblake/nutype/issues/35 . I proposed a solution for the latter which got rightfully rejected.
Another issue I had was the overwhelming complexity of the nutype code, I got lost a few times reading it and felt that some limitations were maybe unnecessary.
So I decided to try to make my own proc-macro inspired by nutype during my free-time over 3 weeks.
I'm very happy with the results and would love to get your advices about the project, the code etc...
The crate allows you to declare newtypes like this and doesn't enforce much on you beside syntax and obvious footguns.
use nnn::nnn;
#[nnn(
derive(Debug, PartialEq, Eq, PartialOrd, Ord),
nnn_derive(TryFrom),
consts(
ZERO = 0.0_f64,
pub ONE = 1.0_f64,
),
default = 5.0_f64,
validators(finite, positive),
sanitizers(custom = |v: f64| v.abs()),
// Serialize & Deserialize are only available in test env.
cfg(test, derive(Serialize, Deserialize)),
attrs(
repr(transparent),
),
)]
struct PositiveFiniteFloat(f64);
Hope you'll like it !