-
Notifications
You must be signed in to change notification settings - Fork 41
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 harnesses for safety of primitive conversions #233
base: main
Are you sure you want to change the base?
Add harnesses for safety of primitive conversions #233
Conversation
3d9c94a
to
140887c
Compare
140887c
to
26512c1
Compare
ad404e0
to
b33a833
Compare
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.
Thanks!
0bf5fd7
to
5ebcf38
Compare
5ebcf38
to
8ebf05f
Compare
generate_float_to_int_harness!(f16 => u8, check_u8_from_f16_unchecked); | ||
generate_float_to_int_harness!(f16 => u16, check_u16_from_f16_unchecked); | ||
generate_float_to_int_harness!(f16 => u32, check_u32_from_f16_unchecked); | ||
generate_float_to_int_harness!(f16 => u64, check_u64_from_f16_unchecked); | ||
generate_float_to_int_harness!(f16 => u128, check_u128_from_f16_unchecked); | ||
generate_float_to_int_harness!(f16 => usize, check_usize_from_f16_unchecked); |
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.
Wouldn't it be possible to use a macro to generate all those generators? As is, it's very hard to tell whether any combinations have possibly been forgotten.
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.
I wish I could do so, but I think that might require extra proc macro crate to generate all those check_a_from_b_...
identifiers as MBEs cannot generate identifiers.
There might be some possible alternatives;
- Make a huge, single macro that requires all identifiers like some kind of a table. This would still be verbose, but might prevent mistakes like forgotting something
- Use same identifier for same kind of harness, but segregate them into different namespaces, like
verify_from_f16::to_u32::check_float_to_int_unchecked
. But this way, we will have multiple harnesses with same names
Do you have some recommendations?
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.
Indeed, you cannot generate identifiers with regular macros. I think your second option sounds reasonable to me. It is similar to what we've done here.
I think you might be able to do generate the namespace for the from
type, and the harness using the to
type.
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.
I'll try it. Thanks!
Towards #220
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.