authorgravatar for leroycepearson@geemili.xyzLeRoyce Pearson <leroycepearson@geemili.xyz> 2020-03-06 21:40:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 13:48:43-04:00
logfde188aadcc8c7ff279c9365e38b1bec114af08b
treec0c1e1649f6589daad520bd94be7c6726383cb6a
parent4173dbdce907e39f376a08ce2f192655fc62b664

Make type specific add functions

Basically, move type specific code into their own functions instead of making `add` a giant function responsible for everything.

1 files changed, 26 insertions(+), 12 deletions(-)

lib/std/cache_hash.zig+26-12
......@@ -66,24 +66,38 @@ pub const CacheHash = struct {
6666 self.blake3.update(&[_]u8{0});
6767 }
6868
69 pub fn add(self: *@This(), val: var) void {
69 pub fn addBool(self: *@This(), val: bool) void {
7070 debug.assert(self.manifest_file == null);
71 self.blake3.update(&[_]u8{@boolToInt(val)});
72 }
73
74 pub fn addInt(self: *@This(), val: var) void {
75 debug.assert(self.manifest_file == null);
76
77 switch (@typeInfo(@TypeOf(val))) {
78 .Int => |int_info| {
79 if (int_info.bits == 0 or int_info.bits % 8 != 0) {
80 @compileError("Unsupported integer size. Please use a multiple of 8, manually convert to a u8 slice.");
81 }
7182
72 const val_type = @TypeOf(val);
73 switch (@typeInfo(val_type)) {
74 .Int => |int_info| if (int_info.bits != 0 and int_info.bits % 8 == 0) {
7583 const buf_len = @divExact(int_info.bits, 8);
7684 var buf: [buf_len]u8 = undefined;
77 mem.writeIntNative(val_type, &buf, val);
85 mem.writeIntNative(@TypeOf(val), &buf, val);
7886 self.addSlice(&buf);
79 } else {
80 @compileError("Unsupported integer size. Please use a multiple of 8, manually convert to a u8 slice.");
81 },
82 .Bool => {
83 var buf: [1]u8 = undefined;
84 buf[0] = if (val) 1 else 0;
85 self.blake3.update(&buf);
87
88 self.blake3.update(&[_]u8{0});
8689 },
90 else => @compileError("Type must be an integer."),
91 }
92 }
93
94 pub fn add(self: *@This(), val: var) void {
95 debug.assert(self.manifest_file == null);
96
97 const val_type = @TypeOf(val);
98 switch (@typeInfo(val_type)) {
99 .Int => self.addInt(val),
100 .Bool => self.addBool(val),
87101 else => @compileError("Unsupported type"),
88102 }
89103 }