Skip to content

mem::Alignment: Naming the -> NonZero<usize> method. #154237

Closed

Description

@scottmcm

Brought up in #154004 (comment): Should the method on Alignment that gives a NonNull<usize> be spelled as_nonzero, as_nonzero_usize, as_non_zero_usize, get, or what?

Nominating for libs-api thoughts so someone can make a PR for whatever the answer is.

Existing APIs on the type: https://doc.rust-lang.org/1.94.0/std/ptr/struct.Alignment.html

Other things like https://doc.rust-lang.org/1.94.0/std/ptr/struct.NonNull.html#method.as_non_null_ptr use non_null for NonNull, so non_zero would arguably be consistent with that. copy_nonoverlapping was mentioned as well, though there's no NonOverlapping type so the parallel isn't as close. Apparently I'm consistent in what I write, because I made https://doc.rust-lang.org/nightly/std/intrinsics/fn.ctlz_nonzero.html too.

Alternatively, this could be more like NonZero itself and just have new/get, since .get().get() would exist to go Alignment -> NonZero<usize> -> usize. Going safely & infallibly that way is important. Going the other way of course is fallible or unsafe so the types involved are in some sense less important. new_unchecked's precondition is is_power_of_two whether it's on usize or NonZero<usize>, though on the latter that's slightly more efficient of a check (for new), though I suspect the combination optimizes together nicely either way.

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Mar 23, 2026
  2. added
    T-libs-api[DEPRECATED; DO NOT USE]
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Mar 23, 2026
  3. changed the title [-]Naming the `-> NonNull<usize>` method.[/-] [+]`mem::Alignment`: Naming the `-> NonNull<usize>` method.[/+] on Mar 23, 2026
  4. clarfonthey commented on Mar 23, 2026

    @clarfonthey
    Contributor

    Don't feel like deduping/rewarding ATM so here's my analysis of non_zero vs. nonzero I posted: #154004 (comment)

    (feel free to incorporate into issue description yourself)

  5. lqd commented on Mar 23, 2026

    @lqd
    Member

    Should the method on Alignment that gives a NonNull<usize>

    (There are a couple mentions of NonNull<usize> instead of NonZero<usize>)

  6. changed the title [-]`mem::Alignment`: Naming the `-> NonNull<usize>` method.[/-] [+]`mem::Alignment`: Naming the `-> NonZero<usize>` method.[/+] on Mar 23, 2026
  7. nia-e commented on Mar 24, 2026

    @nia-e
    Member

    We discussed this in today's @rust-lang/libs-api meeting and we agreed on naming it as_nonzero_usize - with the caveat that this isn't necessarily precedent-setting that as_* methods should specify the return type in the name. Thanks ^^

    @rustbot label -I-libs-api-nominated

  8. clarfonthey commented on Mar 24, 2026

    @clarfonthey
    Contributor

    So, I guess we can just close this as resolved since that's what the current PR has.

  9. added a commit that references this issue on Jul 29, 2026
  10. added a commit that references this issue on Jul 29, 2026
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

    T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions