summaryrefslogtreecommitdiff
path: root/src/walk.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/walk.zig')
-rw-r--r--src/walk.zig24
1 files changed, 17 insertions, 7 deletions
diff --git a/src/walk.zig b/src/walk.zig
index dc1664cb..3e482e9a 100644
--- a/src/walk.zig
+++ b/src/walk.zig
@@ -42,7 +42,7 @@ pub fn fieldDigests(comptime T: type, v: *const T, out: []u64) []u64 {
if (attr.@"comptime" or comptime excluded(T, name)) continue;
var h = Hasher.init(0);
if (comptime lengthOf(T, name)) |len| {
- hashSlice(@typeInfo(F).array.child, @field(v, name)[0..lengthIn(v, len)], &h);
+ hashSlice(Element(F), @field(v, name)[0..lengthIn(v, len)], &h);
} else hash(F, &@field(v, name), &h);
out[n] = h.final();
n += 1;
@@ -63,7 +63,7 @@ pub fn fieldNames(comptime T: type) []const []const u8 {
}
/// Types that are machinery, never state: an allocator, the Io, a lock.
-fn machinery(comptime T: type) bool {
+pub fn machinery(comptime T: type) bool {
inline for (.{ std.mem.Allocator, std.Io, std.Io.Mutex, std.Io.Condition, std.Thread, std.heap.ArenaAllocator }) |M| if (T == M) return true;
return false;
}
@@ -92,7 +92,7 @@ fn opaqueTo(comptime F: type) bool {
};
}
-fn excluded(comptime T: type, comptime name: []const u8) bool {
+pub fn excluded(comptime T: type, comptime name: []const u8) bool {
@setEvalBranchQuota(100_000);
if (!@hasDecl(T, "replica")) return false;
inline for (.{ "local", "derived", "authority" }) |kind| {
@@ -104,12 +104,22 @@ fn excluded(comptime T: type, comptime name: []const u8) bool {
}
/// How far the field `len` names says its array is written (null: none).
-fn lengthIn(v: anytype, comptime len: []const u8) usize {
+/// What a `len`-cut field holds: a fixed array's, or a slice's room (a
+/// selection's ranges, made once and used `nsel` deep).
+fn Element(comptime F: type) type {
+ return switch (@typeInfo(F)) {
+ .array => |a| a.child,
+ .pointer => |p| p.child,
+ else => @compileError("walk: a `len` cuts an array or a slice, not " ++ @typeName(F)),
+ };
+}
+
+pub fn lengthIn(v: anytype, comptime len: []const u8) usize {
const n = @field(v, len);
return if (@typeInfo(@TypeOf(n)) == .optional) (if (n) |k| k else 0) else n;
}
-fn lengthOf(comptime T: type, comptime name: []const u8) ?[]const u8 {
+pub fn lengthOf(comptime T: type, comptime name: []const u8) ?[]const u8 {
@setEvalBranchQuota(100_000);
if (!@hasDecl(T, "replica") or !@hasField(@TypeOf(T.replica), "len")) return null;
inline for (T.replica.len) |pair| if (comptime std.mem.eql(u8, pair[0], name)) return pair[1];
@@ -134,7 +144,7 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void {
.bool => h.update(&.{@intFromBool(v.*)}),
.int => hashInt(T, v.*, h),
.float => h.update(std.mem.asBytes(v)),
- .@"enum" => |e| hashInt(e.tag_type, @intFromEnum(v.*), h),
+ .@"enum" => |e| hashInt(e.tag_type, @backingInt(v.*), h),
.optional => |o| if (v.*) |*payload| {
h.update(&.{1});
hash(o.child, payload, h);
@@ -167,7 +177,7 @@ pub fn hash(comptime T: type, v: *const T, h: *Hasher) void {
if (attr.@"comptime" or comptime excluded(T, name)) continue;
if (comptime opaqueTo(F)) @compileError("walk: " ++ @typeName(T) ++ "." ++ name ++ " is " ++ @typeName(F) ++ ", which no walk sees into: say what it is (`replica`)");
if (comptime lengthOf(T, name)) |len| {
- hashSlice(@typeInfo(F).array.child, @field(v, name)[0..lengthIn(v, len)], h);
+ hashSlice(Element(F), @field(v, name)[0..lengthIn(v, len)], h);
} else hash(F, &@field(v, name), h);
}
},