| ... | @@ -570,6 +570,15 @@ pub fn add(comptime T: type, a: T, b: T) (error{Overflow}!T) { | ... | @@ -570,6 +570,15 @@ pub fn add(comptime T: type, a: T, b: T) (error{Overflow}!T) { |
| 570 | return ov[0]; | 570 | return ov[0]; |
| 571 | } | 571 | } |
| 572 | | 572 | |
| | 573 | /// Returns the sum of `a` and `b`, which can be any integer types, including |
| | 574 | /// different ones from each other, or else returns `null` indicating the |
| | 575 | /// result cannot fit inside the provided `Result` type. |
| | 576 | pub fn addAny(comptime Result: type, a: anytype, b: anytype) ?Result { |
| | 577 | if (Result == comptime_int) return @as(comptime_int, a) + @as(comptime_int, b); |
| | 578 | const O = std.meta.Int(.signed, @max(@bitSizeOf(@TypeOf(a)), @bitSizeOf(@TypeOf(b))) + 1); |
| | 579 | return cast(Result, @as(O, a) + @as(O, b)); |
| | 580 | } |
| | 581 | |
| 573 | /// Returns a - b, or an error on overflow. | 582 | /// Returns a - b, or an error on overflow. |
| 574 | pub fn sub(comptime T: type, a: T, b: T) (error{Overflow}!T) { | 583 | pub fn sub(comptime T: type, a: T, b: T) (error{Overflow}!T) { |
| 575 | if (T == comptime_int) return a - b; | 584 | if (T == comptime_int) return a - b; |