One obvious optimization that is possible with NonZeroXxx aside from simply elision of extra storage for Option<NonZeroXxx> is that it enables safe unchecked code that skips divide by zero checking.
It would be nice if, e.g., Rem<NonZeroUsize> were implemented for usize such that this would be a valid operation:
fn fast_and_safe(dividend: usize, divisor: NonZeroUsize) -> usize {
dividend % divisor
}
This would be guaranteed panic free and there would be no need to generate the code that handles the case where divisor is zero.
One obvious optimization that is possible with
NonZeroXxxaside from simply elision of extra storage forOption<NonZeroXxx>is that it enables safe unchecked code that skips divide by zero checking.It would be nice if, e.g.,
Rem<NonZeroUsize>were implemented forusizesuch that this would be a valid operation:This would be guaranteed panic free and there would be no need to generate the code that handles the case where
divisoris zero.