greyblake / nutype

Rust newtype with guarantees πŸ‡ΊπŸ‡¦ πŸ¦€
MIT License
1.42k stars 23 forks source link

Ability to generate associated consts #194

Closed vic1707 closed 2 weeks ago

vic1707 commented 2 weeks ago

This PR makes it possible to do, inspiration taken from https://github.com/greyblake/nutype/issues/35

use nutype::nutype;

#[nutype(
    validate(finite, greater = 0.0),
    consts(
        ONE = 1.0,
        TWO = 2.0_f32,
        SUM = 1.0 + 4.0,
        INVALID_LESSER = -1.0,
        INVALID_FINITE = f32::NAN,
        FROM_CONST_FN = get_static_f32(),
        // and pretty much anything else
    ),
)]
pub struct FinitePositiveFloat(f32);

Tests are generated in order to ensure that those consts are valid.

TODO: [ ] make consts only available to ints and floats. Strings simply can't. But how about any other datatypes ? [ ] changelog & docs [ ] any ideas ? @greyblake

greyblake commented 2 weeks ago

Hi, thanks for your initiative and effort, but I don't think I will be willing to merge & maintain this. It's not the direction I want this library to move to.

I'd rather to to make const fn new possible (where it's possible), and let users to define the constant they want. They should not need to use nutype macro for that.

vic1707 commented 2 weeks ago

I was thinking of this as a temporary solution since I don't have the skills to enable nutype to generate const fn, but I get your point πŸ‘