I've noticed that if you divide a non-zero x
which is divisible by y
then the result is never zero. Suggesting this API:
impl<T: Div + Mod> NonZero<T> {
fn div_mod(self, divisor: NonZero<T>) -> DivModResult {
match NonZero::new(self % divisor) {
Some(remainder) => DivModResult::WithRemainder { quotient: self / divisor, remainder },
// unchecked is useful here since the optimizer currently doesn't understand this property
None => DivModResult::Divisible(unsafe { NonZero::new_unchecked(self / divisor) }),
}
}
}
pub enum DivModResult<T> {
Divisible(NonZero<T> /* result of division, the result of mod is 0 here*/),
WithRemainder { quotient: T, remainder: NonZero<T> },
}
This actually comes up in some decimal formatting code where one needs to remove the trailing zeros from the number after decimal point. (Using non-zero types helps keep track of requirements.)
Maybe there are other uses. Does anyone think this is worth ACP?