Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Const #197

Merged
merged 8 commits into from
Jan 4, 2025
Merged

Const #197

merged 8 commits into from
Jan 4, 2025

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
6 checks passed
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