-
Notifications
You must be signed in to change notification settings - Fork 20
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
ACP: add {integer}::unchecked_div
and {integer}::unchecked_rem
#526
Comments
Can you say more about where you wanted to use this on signed numbers?
We should absolutely not treat this as a reason, and if these are added, they should have the precondition check from day one. All the others (or at the very least nearly all the others) have them as of rust-lang/rust#121571
Notes that there's https://doc.rust-lang.org/std/primitive.u32.html#impl-Div%3CNonZero%3Cu32%3E%3E-for-u32 but there's not One reason I really like delegating the unsafety through Otherwise it's really easy to write // SAFETY: z isn't zero
unsafe { a.unchecked_div(z) } and have a soundness bug. |
…rk-Simulacrum Add an `unchecked_div` alias to the `Div<NonZero<_>>` impls Inspired by rust-lang/libs-team#526, if people are looking for `unchecked_div`, point them to `u32: Div<NonZero<u32>>` and friends which do no runtime checks -- and are safe! -- rather than today's behaviour of [the intrinsic being the top result](https://doc.rust-lang.org/std/?search=unchecked_div). 
…rk-Simulacrum Add an `unchecked_div` alias to the `Div<NonZero<_>>` impls Inspired by rust-lang/libs-team#526, if people are looking for `unchecked_div`, point them to `u32: Div<NonZero<u32>>` and friends which do no runtime checks -- and are safe! -- rather than today's behaviour of [the intrinsic being the top result](https://doc.rust-lang.org/std/?search=unchecked_div). 
Rollup merge of rust-lang#136019 - scottmcm:alias-unchecked-div, r=Mark-Simulacrum Add an `unchecked_div` alias to the `Div<NonZero<_>>` impls Inspired by rust-lang/libs-team#526, if people are looking for `unchecked_div`, point them to `u32: Div<NonZero<u32>>` and friends which do no runtime checks -- and are safe! -- rather than today's behaviour of [the intrinsic being the top result](https://doc.rust-lang.org/std/?search=unchecked_div). 
Most of my initial usecase came from investigating removal of the
I don't disagree. The only reason I mentioned it is because the code linked above cites using the intrinsic as a workaround for rust-lang/rust#72751, but I don't find the motivation overly strong.
For what it's worth, the existing
but there isn't any specific reason for this to be the same. That being said, anybody using these needs to be reading the precondition docs anyway... |
We discussed this in the libs-api meeting and decided to accept this. The main argument in favor was consistency with the other |
Opened rust-lang/rust#136716 to track this |
Proposal
Problem statement
core::intrinsics
providesunchecked_add
,unchecked_div
,unchecked_mul
,unchecked_rem
,unchecked_shl
,unchecked_shr
, andunchecked_sub
. Theadd
,div
,mul
,shl
,shr
, andsub
versions are available asi32::unchecked_mul
or similar, but there is noi32::unchecked_div
ori32::unchecked_rem
.Motivating examples or use cases
This behavior can already be achieved today with
x.checked_div(y).unwrap_unchecked()
, so the main motivation is consistency with the otherunchecked_*
methods. A side benefit is lighter codegen in debug mode compared tounwrap_unchecked
, but this may not always be true (if we choose to add anassert_unsafe_precondition
at some point in the future).Solution sketch
Alternatives
Continue using
checked_div
withunwrap_unchecked
.Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: