zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 50cbf1f3aa636badec64bcd6d1905cd3c8d67c16 (tree)
parent fde188aadcc8c7ff279c9365e38b1bec114af08b
Author: LeRoyce Pearson <leroycepearson@geemili.xyz>
Date:   Fri,  6 Mar 2020 21:58:15 -0700

Add slice and array support to `add` method

Diffstat:
Mlib/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 @@ -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