Commit fde188aadc

LeRoyce Pearson <leroycepearson@geemili.xyz>
2020-03-07 05:40:33
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 parent 4173dbd
Changed files (1)
lib/std/cache_hash.zig
@@ -66,24 +66,38 @@ pub const CacheHash = struct {
         self.blake3.update(&[_]u8{0});
     }
 
-    pub fn add(self: *@This(), val: var) void {
+    pub fn addBool(self: *@This(), val: bool) void {
         debug.assert(self.manifest_file == null);
+        self.blake3.update(&[_]u8{@boolToInt(val)});
+    }
+
+    pub fn addInt(self: *@This(), val: var) void {
+        debug.assert(self.manifest_file == null);
+
+        switch (@typeInfo(@TypeOf(val))) {
+            .Int => |int_info| {
+                if (int_info.bits == 0 or int_info.bits % 8 != 0) {
+                    @compileError("Unsupported integer size. Please use a multiple of 8, manually convert to a u8 slice.");
+                }
 
-        const val_type = @TypeOf(val);
-        switch (@typeInfo(val_type)) {
-            .Int => |int_info| if (int_info.bits != 0 and int_info.bits % 8 == 0) {
                 const buf_len = @divExact(int_info.bits, 8);
                 var buf: [buf_len]u8 = undefined;
-                mem.writeIntNative(val_type, &buf, val);
+                mem.writeIntNative(@TypeOf(val), &buf, val);
                 self.addSlice(&buf);
-            } else {
-                @compileError("Unsupported integer size. Please use a multiple of 8, manually convert to a u8 slice.");
-            },
-            .Bool => {
-                var buf: [1]u8 = undefined;
-                buf[0] = if (val) 1 else 0;
-                self.blake3.update(&buf);
+
+                self.blake3.update(&[_]u8{0});
             },
+            else => @compileError("Type must be an integer."),
+        }
+    }
+
+    pub fn add(self: *@This(), val: var) void {
+        debug.assert(self.manifest_file == null);
+
+        const val_type = @TypeOf(val);
+        switch (@typeInfo(val_type)) {
+            .Int => self.addInt(val),
+            .Bool => self.addBool(val),
             else => @compileError("Unsupported type"),
         }
     }