From 50cbf1f3aa636badec64bcd6d1905cd3c8d67c16 Mon Sep 17 00:00:00 2001 From: LeRoyce Pearson Date: Fri, 6 Mar 2020 21:58:15 -0700 Subject: [PATCH] Add slice and array support to `add` method --- lib/std/cache_hash.zig | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/std/cache_hash.zig b/lib/std/cache_hash.zig index fa2b2e08c0..2b1d86a1e8 100644 --- a/lib/std/cache_hash.zig +++ b/lib/std/cache_hash.zig @@ -98,6 +98,21 @@ pub const CacheHash = struct { switch (@typeInfo(val_type)) { .Int => self.addInt(val), .Bool => self.addBool(val), + .Array => |array_info| if (array_info.child == u8) { + self.addSlice(val[0..]); + } else { + @compileError("Unsupported array type"); + }, + .Pointer => |ptr_info| switch (ptr_info.size) { + .Slice => if (ptr_info.child == u8) { + self.addSlice(val); + }, + .One => self.add(val.*), + else => { + @compileLog("Pointer type: ", ptr_info.size, ptr_info.child); + @compileError("Unsupported pointer type"); + }, + }, else => @compileError("Unsupported type"), } } @@ -328,7 +343,7 @@ test "cache file and the recall it" { ch.add(true); ch.add(@as(u16, 1234)); - ch.addSlice("1234"); + ch.add("1234"); try ch.cache_file("test.txt"); // There should be nothing in the cache @@ -342,7 +357,7 @@ test "cache file and the recall it" { ch.add(true); ch.add(@as(u16, 1234)); - ch.addSlice("1234"); + ch.add("1234"); try ch.cache_file("test.txt"); // Cache hit! We just "built" the same file