zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit d4e22e3eb668968fa30f25c382b97629902f0bf8 (tree)
parent d99f55b7cf3b3b15ccbef224f157725b91e40bea
Author: Tau <jonathan.haehne@hotmail.com>
Date:   Wed, 21 Jul 2021 16:12:01 +0200

Correct hasUniqueRepresentation for vectors

Closes #9333.

Diffstat:
Mlib/std/hash/auto_hash.zig | 5+----
Mlib/std/meta/trait.zig | 5++++-
2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/std/hash/auto_hash.zig b/lib/std/hash/auto_hash.zig @@ -122,12 +122,9 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void { .Array => hashArray(hasher, key, strat), .Vector => |info| { - if (std.meta.bitCount(info.child) % 8 == 0) { - // If there's no unused bits in the child type, we can just hash - // this as an array of bytes. + if (comptime meta.trait.hasUniqueRepresentation(Key)) { hasher.update(mem.asBytes(&key)); } else { - // Otherwise, hash every element. comptime var i = 0; inline while (i < info.len) : (i += 1) { hash(hasher, key[i], strat); diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig @@ -582,7 +582,7 @@ pub fn hasUniqueRepresentation(comptime T: type) bool { return @sizeOf(T) == sum_size; }, - .Vector => |info| return comptime hasUniqueRepresentation(info.child), + .Vector => |info| return comptime hasUniqueRepresentation(info.child) and @sizeOf(T) == @sizeOf(info.child) * info.len, } } @@ -653,4 +653,7 @@ test "std.meta.trait.hasUniqueRepresentation" { try testing.expect(!hasUniqueRepresentation([]u8)); try testing.expect(!hasUniqueRepresentation([]const u8)); + + try testing.expect(hasUniqueRepresentation(@Vector(4, u16))); + try testing.expect(!hasUniqueRepresentation(@Vector(3, u16))); }