authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-02-14 20:24:14 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2023-02-14 20:24:14 -08:00
logcb7f0d0d5d65b7ea9304aea232bb2f5383f0356a
treec25e2a61e2318d14d2570c82d6366772e048a6a6
parent20a6bcda25842a129b6447b8ab852104e07edad7

add safeAdd()


1 files changed, 8 insertions(+), 0 deletions(-)

src/lib.zig+8
...@@ -414,3 +414,11 @@ pub fn skipToBoundary(pos: u64, boundary: u64, reader: anytype) !void {...@@ -414,3 +414,11 @@ pub fn skipToBoundary(pos: u64, boundary: u64, reader: anytype) !void {
414pub fn is(a: anytype, b: @TypeOf(a)) bool {414pub fn is(a: anytype, b: @TypeOf(a)) bool {
415 return a == b;415 return a == b;
416}416}
417
418/// Allows u32 + i16 to work
419pub fn safeAdd(a: anytype, b: anytype) @TypeOf(a) {
420 if (b >= 0) {
421 return a + @intCast(@TypeOf(a), b);
422 }
423 return a - @intCast(@TypeOf(a), -b);
424}