-
Notifications
You must be signed in to change notification settings - Fork 19
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
Infallible div_ceil
for NonZero<unsigned>
#471
Comments
I disagree. The existing API surface of these types is pretty much only functions that can't already be written without |
Note that For signed numbers you would need |
That's not an equivalent operation to "regular division" which will panic on overflow. |
Yeah, this. That's why there's no So I agree with skipping something that's just EDIT later for clarity: they're useful when they're |
Sure, but it would be insane to add a |
But there is a |
Not exactly. We know that For div, you have to check if the result is zero regardless, you can't make any assumptions about it based on the fact that both inputs are nonzero. So in the division case, the |
Fair enough. I've updated the proposal to only propose the |
Seems reasonable; let's accept this. |
Proposal
Problem statement
The
NonZero<T>
types currently doesn't have any division functions. If you need to perform divisions on these types, you need to convert it to its underlying type, do the division and convert it back. It would make sense to provide these methods directly on the types.Motivating examples or use cases
I recently needed this when calculating the number of sheets needed for printing a document using duplex printing.
As a document can never have 0 pages, I represent the page count as a
NonZero<u32>
. The number of sheets required can then be calculated with (assuming thatdiv_ceil
exists onNonZero<u32>
):Solution sketch
NonZero<unsigned>
should implementdiv_ceil
as it would be panic-free and could never produce a zero:I think it would be weird to just add just `div_ceil` without also adding a checked version of "regular" division. The signed `NonZero` types should probably also have the checked "regular" division function.So in conclusion I think the following functions should be added to core:
Alternatives
Alternatively users can define these themselves using an extension trait or using a separate function.
For example:
The user will either need to use
unsafe
orunwrap
to create the result, which they could avoid ifdiv_ceil
was implemented in core.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: