Skip to content

Const#197

Merged
greyblake merged 8 commits intomasterfrom
const
Jan 4, 2025
Merged

Const#197
greyblake merged 8 commits intomasterfrom
const

Conversation

@greyblake
Copy link
Owner

@greyblake greyblake commented Jan 2, 2025

Addresses #35

Changes

  • Add const_fn attribute, which marks new / try_new and other underlying functions as const

Example

#[nutype(
    const_fn,
    validate(greater_or_equal = -1.0, less_or_equal = 1.0)
)]
struct Correlation(f32);

macro_rules! nutype_const {
    ($name:ident, $ty:ty, $value:expr) => {
        const $name: $ty = match <$ty>::try_new($value) {
            Ok(value) => value,
            Err(_) => panic!("Invalid value"),
        };
    };
}

nutype_const!(MAX_CORRELATION, Correlation, 1.0);

TODO

  • const for into_inner() - double check with Add const getter for Copy types #126
    • Check if that will work for non Copy types
  • Update docs
  • Update README
  • Update CHANGELOG
  • Fix CI (if necessary)
  • Code review

@greyblake greyblake merged commit f2fd76e into master Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant