diff options
Diffstat (limited to 'core/walk.zig')
| -rw-r--r-- | core/walk.zig | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/core/walk.zig b/core/walk.zig index d870052d..593532f8 100644 --- a/core/walk.zig +++ b/core/walk.zig @@ -89,6 +89,45 @@ pub fn digest(comptime T: type, v: *const T) u64 { return h.final(); } +/// Equal as the digest sees them, by the digest's rules: what the walk +/// leaves out (a field `replica` names, a Buf's bytes past its length) is +/// not compared; a type that walks itself, by its digest. std.meta.eql +/// compares every byte of an array, and those bytes are undefined: 0xAA on +/// every Debug process, each release process's own garbage; a decision of +/// the core taken on them agrees between a session and its clients in +/// Debug alone. +pub fn eql(comptime T: type, a: *const T, b: *const T) bool { + switch (@typeInfo(T)) { + .void => return true, + .bool, .int, .@"enum" => return a.* == b.*, + .optional => |o| return if (a.*) |*x| (if (b.*) |*y| eql(o.child, x, y) else false) else b.* == null, + .array => |arr| { + if (comptime flatHash(arr.child)) return std.mem.eql(arr.child, a, b); + for (a, b) |*x, *y| if (!eql(arr.child, x, y)) return false; + return true; + }, + .@"struct" => |s| { + if (comptime machinery(T)) return true; + if (comptime s.layout == .@"packed" or @hasDecl(T, "digest") or isList(T) or isAtomic(T) or isBitset(T) or @hasField(T, "any_future")) return digest(T, a) == digest(T, b); + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| { + if (attr.@"comptime" or comptime excluded(T, name)) continue; + if (comptime lengthOf(T, name)) |len| { + const x = @field(a, name)[0..lengthIn(a, len)]; + const y = @field(b, name)[0..lengthIn(b, len)]; + if (comptime flatHash(Element(F))) { + if (!std.mem.eql(Element(F), x, y)) return false; + } else { + if (x.len != y.len) return false; + for (x, y) |*p, *q| if (!eql(Element(F), p, q)) return false; + } + } else if (!eql(F, &@field(a, name), &@field(b, name))) return false; + } + return true; + }, + else => return digest(T, a) == digest(T, b), + } +} + /// A digest of each field `hash` would walk, in order (what a divergence /// is narrowed down by); `names` are theirs. pub fn fieldDigests(comptime T: type, v: *const T, out: []u64) []u64 { |
