diff --git a/src/lib.zig b/src/lib.zig index 60046b2b86852257db43bdd1550535156f9f2633..6f217856fa283a3f1900fbc6a1749021d9cd33dc 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -451,7 +451,7 @@ pub fn safeAdd(a: anytype, b: anytype) @TypeOf(a) { if (b >= 0) { return a + @as(@TypeOf(a), @intCast(b)); } - return a - @as(@TypeOf(a), @intCast(-b)); + return a - @as(@TypeOf(a), @intCast(-@as(OneBiggerInt(@TypeOf(b)), b))); } pub fn readBytesAlloc(reader: anytype, alloc: std.mem.Allocator, len: usize) ![]u8 { @@ -651,3 +651,9 @@ pub fn joinPartial(comptime P: type, a: P, b: P) P { } return temp; } + +pub fn OneBiggerInt(comptime T: type) type { + var info = @typeInfo(T); + info.Int.bits += 1; + return @Type(info); +}