Open jhpratt opened 4 years ago
You can do:
match (foo > 0, bar > 0) {
(true, true) | (false, false) => max,
(false, true) | (true, false) => min,
_ => zero,
}
if you want to treat 0 specially, you can also do foo.cmp(&0)
and match on that.
@est31 I'd definitely match on > 0
if it were only on unsigned integers. As I had said, though, it is used in a macro that is generic.
I could use .cmp(&0)
, I suppose. I just think having parity between various integer types makes more sense.
Also note that matching on signum values doesn't work on float values. Matching on partial_cmp results is most portable.
This can be quite useful when writing macros that are generic over any integer.
For example, I just tried writing this code (bar is any of i8, i16, i32, u8, u16, u32 via a macro)
But because
u*::signum
isn't implemented, I had to resort to something far more verbose:This can be simplified a bit, but at the expense of performing the operations more than once. This might be optimized away, though.
To allow for the first example to compile (along with the rest of the code, of course), I'd think having
u32::signum
returni32
would make sense, despite returning-1
being impossible. I definitely think usingsignum
makes things clearer and more readable.If this is something that is desired, I can submit a PR.