Skip to content

impl Div<NonZeroXxx> and Rem<NonZeroXxx> for xxx #3033

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
mqudsi opened this issue Dec 11, 2020 · 2 comments
Closed

impl Div<NonZeroXxx> and Rem<NonZeroXxx> for xxx #3033

mqudsi opened this issue Dec 11, 2020 · 2 comments

Comments

@mqudsi
Copy link

mqudsi commented Dec 11, 2020

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.

@sfackler
Copy link
Member

rust-lang/rust#79134

@mqudsi
Copy link
Author

mqudsi commented Dec 11, 2020

@sfackler thanks! I searched with a few variations of the type name and didn't find anything. (Maybe we should add a "keywords: ..." convention)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants