Skip to content

Feature Request: NonZeroInt literal coersion #69329

Description

@Lucretiel

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).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions