authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-08 23:41:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-12 00:14:07-07:00
logdeea36250ffe458d92b32b1ad090b8a958ba8082
tree26474e427b254aa7aaaaccebfb6470a4f7d83768
parent6c64090e7af56ccbf374e1a927fe232b340ed681

std.Build.Cache.Path: add `subPathOpt` and `TableAdapter`

Helpful methods when using one of these structs as a hash table key.

1 files changed, 20 insertions(+), 0 deletions(-)

lib/std/Build/Cache/Path.zig+20
......@@ -151,6 +151,26 @@ pub fn eql(self: Path, other: Path) bool {
151151 return self.root_dir.eql(other.root_dir) and std.mem.eql(u8, self.sub_path, other.sub_path);
152152}
153153
154pub fn subPathOpt(self: Path) ?[]const u8 {
155 return if (self.sub_path.len == 0) null else self.sub_path;
156}
157
158/// Useful to make `Path` a key in `std.ArrayHashMap`.
159pub const TableAdapter = struct {
160 pub const Hash = std.hash.Wyhash;
161
162 pub fn hash(self: TableAdapter, a: Cache.Path) u32 {
163 _ = self;
164 const seed: u32 = @bitCast(a.root_dir.handle.fd);
165 return @truncate(Hash.hash(seed, a.sub_path));
166 }
167 pub fn eql(self: TableAdapter, a: Cache.Path, b: Cache.Path, b_index: usize) bool {
168 _ = self;
169 _ = b_index;
170 return a.eql(b);
171 }
172};
173
154174const Path = @This();
155175const std = @import("../../std.zig");
156176const fs = std.fs;