-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
core::num::NonZero<N> type alias #88990
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
heh. so we have come to a full circle of introducing |
Shrug, this is not at all the |
Well both?Surely the primary purpose of of rust-lang/rfcs#2307 is to remove the public
So I think it is only natural to take one step further (backward?) and make it #[repr(transparent)]
#[rustc_layout_scalar_valid_range_start(1)]
#[rustc_nonnull_optimization_guaranteed]
pub struct NonZero<T: IntegerPrimitive>(T);
// ... then most impl can be freed from the macros ...
// stability compatibility ↓
pub type NonZeroU8 = NonZero<u8>;
...
pub type NonZeroI128 = NonZero<i128>; Edit: I'm moving this to #82363 to not clutter the PR itself. |
i'm going to wait till #82363's current FCP is canceled since this PR removes the |
☔ The latest upstream changes (presumably #89512) made this pull request unmergeable. Please resolve the merge conflicts. |
Kenny's writeup in #82363 (comment) is compelling. Gonna close this in favor of |
This PR implements
NonZero<N>
as I proposed in #82363 (comment), and deletes unstableNonZero_c_*
.NonZero<N>
is a generic type alias for the non-zero type that corresponds to each integer primitive.This type alias simply provides an alternative spelling of the various
NonZero
structs found incore::num
. For exampleNonZero<u16>
refers to the structcore::num::NonZeroU16
.Note: this is not intended for writing code that is generic over multiple types of non-zero integers. The relevant trait bound you would need for that is not exposed. The only thing this is, is an alternative spelling.
Example
Where this comes in handy is if the primitive types themselves are being used via a type alias, such as
std::os::raw::c_char
. Thec_char
type refers to eitheri8
oru8
depending on whether the platform's C character type is signed or unsigned, and withoutNonZero<N>
there is not a straightforward way to name eitherNonZeroI8
orNonZeroU8
depending on which one a non-zeroc_char
corresponds to.