| ... | ... | @@ -81,24 +81,22 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void { |
| 81 | 81 | .Undefined, |
| 82 | 82 | .Void, |
| 83 | 83 | .Null, |
| 84 | | .BoundFn, |
| 85 | 84 | .ComptimeFloat, |
| 86 | 85 | .ComptimeInt, |
| 87 | 86 | .Type, |
| 88 | 87 | .EnumLiteral, |
| 89 | 88 | .Frame, |
| 89 | .Float, |
| 90 | 90 | => @compileError("cannot hash this type"), |
| 91 | 91 | |
| 92 | 92 | // Help the optimizer see that hashing an int is easy by inlining! |
| 93 | 93 | // TODO Check if the situation is better after #561 is resolved. |
| 94 | 94 | .Int => @call(.{ .modifier = .always_inline }, hasher.update, .{std.mem.asBytes(&key)}), |
| 95 | 95 | |
| 96 | | .Float => |info| hash(hasher, @bitCast(std.meta.Int(false, info.bits), key), strat), |
| 97 | | |
| 98 | 96 | .Bool => hash(hasher, @boolToInt(key), strat), |
| 99 | 97 | .Enum => hash(hasher, @enumToInt(key), strat), |
| 100 | 98 | .ErrorSet => hash(hasher, @errorToInt(key), strat), |
| 101 | | .AnyFrame, .Fn => hash(hasher, @ptrToInt(key), strat), |
| 99 | .AnyFrame, .BoundFn, .Fn => hash(hasher, @ptrToInt(key), strat), |
| 102 | 100 | |
| 103 | 101 | .Pointer => @call(.{ .modifier = .always_inline }, hashPointer, .{ hasher, key, strat }), |
| 104 | 102 | |
| ... | ... | @@ -266,12 +264,12 @@ test "hash slice deep" { |
| 266 | 264 | test "hash struct deep" { |
| 267 | 265 | const Foo = struct { |
| 268 | 266 | a: u32, |
| 269 | | b: f64, |
| 267 | b: u16, |
| 270 | 268 | c: *bool, |
| 271 | 269 | |
| 272 | 270 | const Self = @This(); |
| 273 | 271 | |
| 274 | | pub fn init(allocator: *mem.Allocator, a_: u32, b_: f64, c_: bool) !Self { |
| 272 | pub fn init(allocator: *mem.Allocator, a_: u32, b_: u16, c_: bool) !Self { |
| 275 | 273 | const ptr = try allocator.create(bool); |
| 276 | 274 | ptr.* = c_; |
| 277 | 275 | return Self{ .a = a_, .b = b_, .c = ptr }; |
| ... | ... | @@ -279,9 +277,9 @@ test "hash struct deep" { |
| 279 | 277 | }; |
| 280 | 278 | |
| 281 | 279 | const allocator = std.testing.allocator; |
| 282 | | const foo = try Foo.init(allocator, 123, 1.0, true); |
| 283 | | const bar = try Foo.init(allocator, 123, 1.0, true); |
| 284 | | const baz = try Foo.init(allocator, 123, 1.0, false); |
| 280 | const foo = try Foo.init(allocator, 123, 10, true); |
| 281 | const bar = try Foo.init(allocator, 123, 10, true); |
| 282 | const baz = try Foo.init(allocator, 123, 10, false); |
| 285 | 283 | defer allocator.destroy(foo.c); |
| 286 | 284 | defer allocator.destroy(bar.c); |
| 287 | 285 | defer allocator.destroy(baz.c); |
| ... | ... | @@ -338,12 +336,12 @@ test "testHash struct" { |
| 338 | 336 | test "testHash union" { |
| 339 | 337 | const Foo = union(enum) { |
| 340 | 338 | A: u32, |
| 341 | | B: f32, |
| 339 | B: bool, |
| 342 | 340 | C: u32, |
| 343 | 341 | }; |
| 344 | 342 | |
| 345 | 343 | const a = Foo{ .A = 18 }; |
| 346 | | var b = Foo{ .B = 12.34 }; |
| 344 | var b = Foo{ .B = true }; |
| 347 | 345 | const c = Foo{ .C = 18 }; |
| 348 | 346 | testing.expect(testHash(a) == testHash(a)); |
| 349 | 347 | testing.expect(testHash(a) != testHash(b)); |