-
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
Add (checked_)norem_div
methods for integer types
#337
Comments
For the const case, as in your example, it's best to do that assertion at compile time: const PAGE_SIZE: usize = 1 << 14;
const OS_PAGE_SIZE: usize = 1 << 12;
const _: () = {
assert!(PAGE_SIZE % OS_PAGE_SIZE == 0);
}; I recommend you provide a different example that shows how this method would be beneficial at runtime. |
The point is that constant definitions with static assertions (we use them in our code, but they are not relevant here) can be quite far away from operations which depend on the assert. The motivation for the proposed methods is to clearly express that we expect that two numbers divide without remainder without any additional clutter associated with Also, the second part of the example is for runtime values. |
In that case, you should provide a I missed the runtime example All that said, this seems pretty niche. Also for the name, I'm not a fan of |
Here's another alternative. If we had a std version of if let (quotient, 0) = whatever.div_rem(PAGE_SIZE) {
// divides evenly
} else {
// there was a remainder
} |
A runtime example of this is a subproblem of Advent of Code 2024 day 13 (spoiler ahead). In general terms, it involves integer systems of equations, and it is interested in whether or not the solution values are integers. At the final step of my solver, it has equations of the form if b % a == 0 && d % c == 0 {
Some((b / a, d / c))
} else {
None
} With a function like b.checked_norem_div(a).zip(d.checked_norem_div(c)) Before finding this issue, the name that I thought of is |
The name that popped into my head was |
At first, I did think of |
*Except for |
We already have Footnotes
|
fn evenly_divide(n: u64, d: u64) -> Option<u64> {
(n % d == 0).then_some(n / d)
} |
Note that the Makes me wonder if it makes sense to have a safe (And I wonder if it's make sense to have at least the unsafe version take |
Proposal
Problem statement
In some cases it desirable to get division result and ensure that the division has no remainder. For example, a function which works with memory mappings may accept size in bytes, but it requires that the size must be multiple of OS page size. Today we have to insert separate checks (e.g.
assert_eq!(size / OS_PAGE_SIZE, 0)
) somewhere near the division. It makes code less concise and not great at expressing intent.Motivating examples or use cases
In our code we have code which works with pages and accepts size in bytes. It requires that the size should be multiple of the page size. The size is provided in bytes for user convenience and because page size can depend on OS and constants.
So we have a bunch of code like this:
To simplify the code we have introduced the following helper trait and implemented it for necessary integer types:
It's better than the explicit checks, but we need to introduce the helper trait and import it every time the methods are used. Also in a multi-crate projects it becomes even more annoying. We either have to define the trait in each crate where it needed or introduce a whole separate crate for this small functionality.
Solution sketch
Introduce the following methods to all integers through
int_impl!
anduint_impl!
macros:For signed integer the conditions also include potential overflow when
MIN
is divided by-1
.Alternatives
Users may use explicit checks based on the remainder operator or custom traits as shown in the examples section. Arguably, the functionality is too small for a separate crate.
Links and related work
rust-lang/rust#116632
The text was updated successfully, but these errors were encountered: