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

Feature Request: NonZeroInt literal coersion #69329

Open
Lucretiel opened this issue Feb 20, 2020 · 2 comments
Open

Feature Request: NonZeroInt literal coersion #69329

Lucretiel opened this issue Feb 20, 2020 · 2 comments
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Lucretiel
Copy link
Contributor

It should be possible for an integer literal to initialize a NonZeroInt type, like NonZeroUsize. For instance, this should be legal:

struct Settings {
    // None means unlimited
    max_args: Option<NonZeroUsize>
}

let settings = Settings {
    max_args: Some(2),
}

The compiler can easily check whether the literal is zero and fail if it is.

While I know that the NonZero types are "just" regular structs, it seems as though they already have so many special compiler attributes attached to them to ensure that they work with Option correctly that it seems like it wouldn't be much of a stretch to have this one as well (though I'm happy to be corrected there).

@adrianheine
Copy link
Contributor

There is a clever macro implementing this: https://docs.rs/nonzero_ext/latest/nonzero_ext/macro.nonzero.html

@zopsicle
Copy link
Contributor

While I know that the NonZero types are "just" regular structs

There is now syntax for CStr literals. Thus, literals for non-primitive types are not inherently problematic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants