diff options
| author | LeRoyce Pearson <leroycepearson@geemili.xyz> | 2020-03-05 21:32:26 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-05-25 13:48:43 -0400 |
| commit | 8a77c1c637b07a2ff4f8f8981f9f732756cc38f6 (patch) | |
| tree | e591d37266273f7f2b566bbe221726595000b947 /lib/std | |
| parent | 3158dc424e48d3da52d9a8f99cba65a4ef850f2e (diff) | |
| download | zig-8a77c1c637b07a2ff4f8f8981f9f732756cc38f6.tar.gz zig-8a77c1c637b07a2ff4f8f8981f9f732756cc38f6.zip | |
Add `cache` method; add support for caching integers
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/cache_hash.zig | 63 |
1 files changed, 18 insertions, 45 deletions
diff --git a/lib/std/cache_hash.zig b/lib/std/cache_hash.zig index ea85cb6db3..710f853124 100644 --- a/lib/std/cache_hash.zig +++ b/lib/std/cache_hash.zig @@ -64,13 +64,26 @@ pub const CacheHash = struct { pub fn cache_buf(self: *@This(), val: []const u8) !void { debug.assert(self.manifest_file_path == null); - var temp_buffer = try self.alloc.alloc(u8, val.len + 1); - defer self.alloc.free(temp_buffer); + self.blake3.update(val); + } + + pub fn cache(self: *@This(), val: var) !void { + debug.assert(self.manifest_file_path == null); - mem.copy(u8, temp_buffer, val); - temp_buffer[val.len] = 0; + 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); + self.blake3.update(&buf); + } else { + @compileError("Unsupported integer size. Please use a multiple of 8, manually convert to a u8 slice."); + }, + else => @compileError("Unsupported type"), + } - self.blake3.update(temp_buffer); + self.blake3.update(&[_]u8{0}); } pub fn cache_file(self: *@This(), file_path: []const u8) !void { @@ -287,43 +300,3 @@ fn hash_file(alloc: *Allocator, bin_digest: []u8, handle: *const File) !void { blake3.final(bin_digest); } - -test "see if imported" { - const cwd = fs.cwd(); - - const temp_manifest_dir = "temp_manifest_dir"; - - try cwd.writeFile("test.txt", "Hello, world!\n"); - - var digest1 = try ArrayList(u8).initCapacity(testing.allocator, 32); - defer digest1.deinit(); - var digest2 = try ArrayList(u8).initCapacity(testing.allocator, 32); - defer digest2.deinit(); - - { - var ch = try CacheHash.init(testing.allocator, temp_manifest_dir); - defer ch.release(); - - try ch.cache_buf("1234"); - try ch.cache_file("test.txt"); - - // There should be nothing in the cache - debug.assert((try ch.hit(&digest1)) == false); - - try ch.final(&digest1); - } - { - var ch = try CacheHash.init(testing.allocator, temp_manifest_dir); - defer ch.release(); - - try ch.cache_buf("1234"); - try ch.cache_file("test.txt"); - - // Cache hit! We just "built" the same file - debug.assert((try ch.hit(&digest2)) == true); - } - - debug.assert(mem.eql(u8, digest1.toSlice(), digest2.toSlice())); - - try cwd.deleteTree(temp_manifest_dir); -} |
