Skip to content

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

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

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