-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add a safe way to use NonZero #27573
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
Wouldn't that risk undefined behaviour when inner = 0? Normally a
The purpose was to provide a safe, checked version of
That's actually a mistake, I must have pressed undo too many times after finding out that |
Add tests Add back comments.
@Diggsey ... Are you arguing with yourself? |
@gankro No, I was replying to @nikomatsakis's comments, but they disappeared when I pushed the new changes. Should I not have squashed them yet? |
@Diggsey yeah we prefer not to squash, but it's weird niko's comments totally disappeared. shrug |
@Diggsey ok, with the comments, I certainly find the fn less mysterious, and I can see why you preferred the transmute over my |
So, I think this seems like a fine addition, given that this is an unstable API etc. But I don't really know the policy here (i.e., what amount of approval is needed to expand an unstable API). Perhaps someone on the @rust-lang/libs team, e.g. @aturon @alexcrichton, would clarify? |
We don't have a super clear-cut policy in a case like this. Personally, I'd like to see a kind of long-term vision for @gankro, IIRC your ideas about |
Yeah I haven't seen a really good usecase for NonZero that wasn't just Unique or Shared. More generally I believe we should want some unsafe ForbiddenValues interface to hook into the sort of stuff I describe in rust-lang/rfcs#1230 But I'm a bit biased here -- I get really sad when I see something like |
I was using |
I agree with @aturon that I'd rather not accumulate little bits of unstable functionality for such nebulous types like this. Using |
As an aside, I've definitely wanted |
Closing due to inactivity, and it sounds like we may want to consider a more comprehensive redesign of |
It should be possible to go from a
T: Zeroable
to anOption<NonZero<T>>
without requiring unsafe code, so this PR adds a new method,NonZero::new_option(...)
for that purpose.This should be a no-op, and ideally it would use
mem::transmute
, but that isn't possible in generic code, so I had to fall back to this slightly less safe alternative.