For weight_matchers I have a macro that takes any primitive number. It needs to treat floats, integers and unsigneds slightly differently. To that end the type must mostly be given explicitly. I want to change that to leverage inferrence.
Despite not knowing the input-type, I can derive values for some numbers, so that I can test symbolically, independent of syntactical differences:
if one / two > zero
// float
else if zero.saturating_sub(one) < zero
// integer
else
// unsigned
The 1st test makes it so that the 2nd test will only ever see integral types. Sadly the compiler doesn’t know that. So when the macro inputs are floats, it’ll look for that method on the float (which sadly doesn’t have it.)
I’m racking my brain for a formula akin to the 1st, which will not panic on overflow, while letting me distinguish signedness. I’ve also tried various generic approaches. But even if I could distinguish on <T: !Neg>
, I can’t find a way.
To make this harder, it’s a feature of this crate to be able to do everything in const
. Additionally I would not want to impl any trait for all primitive types, because floats currently have unstable sizes, and integers might get arbitrary bit sizes.