Skip to content

Tracking issue for std::num::NonZero* types #49137

Closed
@Centril

Description

@Centril

This is a tracking issue for the RFC "Add std::num::NonZeroU32 and friends, deprecate core::nonzero" (rust-lang/rfcs#2307).

Steps:

Unresolved questions:

  • Should the signed types be included?
    Decide on naming -- NonZeroU32 or PositiveU32? Should the U be dropped?
    Should the memory layout of e.g. Option be a language guarantee?
    Settle issues wrt. const generics before stabilization.
    -- We should either end up with a generic type or with a clear explanation of why a generic type can't work or doesn't work well.

Activity

added
B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
on Mar 18, 2018
SimonSapin

SimonSapin commented on Mar 18, 2018

@SimonSapin
Contributor

Implementation PR: #48265

Documentation: @Centril, do doc-comments in this PR look sufficient to you?


My opinion on unresolved questions:

Signed types (e.g. NonZeroI32): as discussed in the RFC thread, they are included in the RFC and implementation PR because it was easy and because core::nonzero::NonZero<T> supports them too. I see a downside to having them, but I also haven’t had a use case for them. And they are easy to build outside of std in a crate that uses the unsigned types internally and does the conversion in new, new_unchecked, and get. (Unlike e.g. non-zero integers of different sizes if only NonZeroUsize were available.)

Positive* names: The fact that zero is not positive in English is subtle, and it is not shared by some other languages. (For example zero is both positive and negative in French, and the correct translation for “nombre positif” is “non-negative number”.) I don’t think relying on such a subtlety to communicate the core property of these types is a good idea.

Dropping the U in names: this is only possible if we also remove the signed types. But even so, I think that including the full name of the type accepted by new and returned by get is desirable.

Separate types v.s. one generic type: this was discussed at length in the RFC. The former has precedent with std::sync::atomic::Atomic*. The latter brings other unresolved questions that I feel don’t have satisfactory answers: trait bounds on that type parameter, and implementations of relevant traits for composite for types outside of std.

SimonSapin

SimonSapin commented on Mar 18, 2018

@SimonSapin
Contributor

Making the memory layout of Option<num::NonZero*> a language guarantee: I explicitly made the RFC not make this guarantee in order to keep the RFC within the realm of the standard library (and hopefully easier to get accepted). But if @rust-lang/lang feels comfortable making that guarantee, that sounds good to me.

Centril

Centril commented on Mar 18, 2018

@Centril
ContributorAuthor

@SimonSapin

Documentation: @Centril, do doc-comments in this PR look sufficient to you?

Pretty much, yeah =)


On the unresolved questions:

  1. Maybe we should skip the signed types but add them when someone asks for them with an issue or PR or on IRC (don't need to go to another RFC then...)?

  2. I agree that NonZeroU32 is best.

added
B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.
and removed
B-RFC-approvedBlocker: Approved by a merged RFC but not yet implemented.
on Mar 18, 2018
SimonSapin

SimonSapin commented on Mar 18, 2018

@SimonSapin
Contributor

For what it’s worth the implementation of the 6 signed types costs exactly an additional 111 characters in a macro invocation. They’re not a maintenance burden.

Centril

Centril commented on Mar 18, 2018

@Centril
ContributorAuthor

For what it’s worth the implementation of the 6 signed types costs exactly an additional 111 characters in a macro invocation. They’re not a maintenance burden.

This is true; but they cost in compile times -- but probably not a noteworthy amount of time.

clarfonthey

clarfonthey commented on Mar 19, 2018

@clarfonthey
Contributor

Honestly, compile time of end-user crates matters a lot more than compile time of libstd and co. because for most users libstd will already be compiled. So, even adding a minute of compile time to libstd isn't super bad.

scottmcm

scottmcm commented on Mar 20, 2018

@scottmcm
Member

Sounds like I'm the only one who likes Positive, so let's mark that resolved and stick with NonZeroU32.

Maybe just put the signed ones under a different feature gate? That way we'll find out which ones people use, and there's still nobody "attached" to them, stabilize/remove them separately. (Personally I think 0 is a weird hole in a signed type, and would much rather have nice named types for symmetric integers [not INT_MIN] than ones without zero. [Until we get const generics and can make our own holes.])

96 remaining items

Loading
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

    B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCT-langRelevant to the language teamT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @eddyb@nikomatsakis@joshtriplett@SimonSapin@Centril

        Issue actions

          Tracking issue for std::num::NonZero* types · Issue #49137 · rust-lang/rust