-
Notifications
You must be signed in to change notification settings - Fork 21
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
Use NonZeroUsize
for constants
#81
Conversation
By using NonZero we eliminate a class of errors where you don't get errors with zero length (e.g. traversing arrays with zero length just doesn't happen), however, no work is done and the data at the end is incorrect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you describe the case where we silently fail if don't use NonZeroUsize? In the first synthesize phase, will this cause any error because it passes a potential zero size bit vector?
I had exactly the same error when I was counting carry-bits. Because of a bug in the code, it always gave zero and it was not caught by tests or compiler, because it just calculated zero everywhere and traversed the collection zero times. A bad variant of the same design is to put |
I understand this approach can eliminate this specific type of error. But the use of NonZeroUsize and unsafe seems a little overkill. Let me think about if there is better approach to solve this kind of error, or since we already know it we can avoid it in future. |
It seems to me you vastly overestimate the complexity this approach brings, it's a type from a standard library and a standard mechanism for initializing it. I first saw this mechanism in general in a regular web2 application, where developers are the least tempted to do complicated things |
Putting |
By using
NonZero
we eliminate a class of errors where you don't get errors with zero length (e.g. traversing arrays with zero length just doesn't happen), however, no work is done and the data at the end is incorrect.It also allows us to better reflex some places where we pass the size of some collection as a parameter and the collection turns out to be empty due to an error. Now at least we will get panic in such places