pub const HashHelper = struct hasher: Hasher = hasher_initpub fn addBytes(hh: *HashHelper, bytes: []const u8) voidpub fn addBytesZ(hh: *HashHelper, bytes: [: 0]const u8) voidpub fn addOptionalBytes(hh: *HashHelper, optional_bytes: ?[]const u8) voidpub fn addListOfBytes(hh: *HashHelper, list_of_bytes: []const []const u8) voidpub fn addOptionalListOfBytes(hh: *HashHelper, optional_list_of_bytes: ?[]const []const u8) voidpub fn add(hh: *HashHelper, x: anytype) voidConvert the input value into bytes and record it as a dependency of the process being cached.
pub fn addOptional(hh: *HashHelper, optional: anytype) voidpub fn peek(hh: HashHelper) [hex_digest_len]u8Returns a hex encoded hash of the inputs, without modifying state.
pub fn peekBin(hh: HashHelper) BinDigestpub fn final(hh: *HashHelper) HexDigestReturns a hex encoded hash of the inputs, mutating the state of the hasher.
pub fn oneShot(bytes: []const u8) [hex_digest_len]u8196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298pub const HashHelper = struct {
hasher: Hasher = hasher_init,
pub fn addBytes(hh: *HashHelper, bytes: []const u8) void {
hh.hasher.update(mem.asBytes(&bytes.len));
hh.hasher.update(bytes);
}
pub fn addBytesZ(hh: *HashHelper, bytes: [:0]const u8) void {
hh.hasher.update(mem.absorbSentinel(bytes));
}
pub fn addOptionalBytes(hh: *HashHelper, optional_bytes: ?[]const u8) void {
hh.add(optional_bytes != null);
hh.addBytes(optional_bytes orelse return);
}
pub fn addListOfBytes(hh: *HashHelper, list_of_bytes: []const []const u8) void {
hh.add(list_of_bytes.len);
for (list_of_bytes) |bytes| hh.addBytes(bytes);
}
pub fn addOptionalListOfBytes(hh: *HashHelper, optional_list_of_bytes: ?[]const []const u8) void {
hh.add(optional_list_of_bytes != null);
hh.addListOfBytes(optional_list_of_bytes orelse return);
}
/// Convert the input value into bytes and record it as a dependency of the process being cached.
pub fn add(hh: *HashHelper, x: anytype) void {
switch (@TypeOf(x)) {
std.SemanticVersion => {
hh.add(x.major);
hh.add(x.minor);
hh.add(x.patch);
},
std.Target.Os.TaggedVersionRange => {
switch (x) {
.hurd => |hurd| {
hh.add(hurd.range.min);
hh.add(hurd.range.max);
hh.add(hurd.glibc);
},
.linux => |linux| {
hh.add(linux.range.min);
hh.add(linux.range.max);
hh.add(linux.glibc);
hh.add(linux.android);
},
.windows => |windows| {
hh.add(windows.min);
hh.add(windows.max);
},
.semver => |semver| {
hh.add(semver.min);
hh.add(semver.max);
},
.none => {},
}
},
std.zig.BuildId => switch (x) {
.none, .fast, .uuid, .sha1, .md5 => hh.add(std.meta.activeTag(x)),
.hexstring => |hex_string| hh.addBytes(hex_string.toSlice()),
},
else => switch (@typeInfo(@TypeOf(x))) {
.bool, .int, .@"enum", .array => hh.addBytes(mem.asBytes(&x)),
else => @compileError("unable to hash type " ++ @typeName(@TypeOf(x))),
},
}
}
pub fn addOptional(hh: *HashHelper, optional: anytype) void {
hh.add(optional != null);
hh.add(optional orelse return);
}
/// Returns a hex encoded hash of the inputs, without modifying state.
pub fn peek(hh: HashHelper) [hex_digest_len]u8 {
var copy = hh;
return copy.final();
}
pub fn peekBin(hh: HashHelper) BinDigest {
var copy = hh;
var bin_digest: BinDigest = undefined;
copy.hasher.final(&bin_digest);
return bin_digest;
}
/// Returns a hex encoded hash of the inputs, mutating the state of the hasher.
pub fn final(hh: *HashHelper) HexDigest {
var bin_digest: BinDigest = undefined;
hh.hasher.final(&bin_digest);
return binToHex(bin_digest);
}
pub fn oneShot(bytes: []const u8) [hex_digest_len]u8 {
var hasher: Hasher = hasher_init;
hasher.update(bytes);
var bin_digest: BinDigest = undefined;
hasher.final(&bin_digest);
return binToHex(bin_digest);
}
}