|  | 
|  | 1 | +const std = @import("std"); | 
|  | 2 | +const builtin = @import("builtin"); | 
|  | 3 | +const mem = std.mem; | 
|  | 4 | +const meta = std.meta; | 
|  | 5 | + | 
|  | 6 | +/// Provides generic hashing for any eligible type. | 
|  | 7 | +/// Only hashes `key` itself, pointers are not followed. | 
|  | 8 | +pub fn autoHash(hasher: var, key: var) void { | 
|  | 9 | +    const Key = @typeOf(key); | 
|  | 10 | +    switch (@typeInfo(Key)) { | 
|  | 11 | +        builtin.TypeId.NoReturn, | 
|  | 12 | +        builtin.TypeId.Opaque, | 
|  | 13 | +        builtin.TypeId.Undefined, | 
|  | 14 | +        builtin.TypeId.ArgTuple, | 
|  | 15 | +        builtin.TypeId.Void, | 
|  | 16 | +        builtin.TypeId.Null, | 
|  | 17 | +        builtin.TypeId.BoundFn, | 
|  | 18 | +        builtin.TypeId.ComptimeFloat, | 
|  | 19 | +        builtin.TypeId.ComptimeInt, | 
|  | 20 | +        builtin.TypeId.Type, | 
|  | 21 | +        builtin.TypeId.EnumLiteral, | 
|  | 22 | +        => @compileError("cannot hash this type"), | 
|  | 23 | + | 
|  | 24 | +        // Help the optimizer see that hashing an int is easy by inlining! | 
|  | 25 | +        // TODO Check if the situation is better after #561 is resolved. | 
|  | 26 | +        builtin.TypeId.Int => @inlineCall(hasher.update, std.mem.asBytes(&key)), | 
|  | 27 | + | 
|  | 28 | +        builtin.TypeId.Float => |info| autoHash(hasher, @bitCast(@IntType(false, info.bits), key)), | 
|  | 29 | + | 
|  | 30 | +        builtin.TypeId.Bool => autoHash(hasher, @boolToInt(key)), | 
|  | 31 | +        builtin.TypeId.Enum => autoHash(hasher, @enumToInt(key)), | 
|  | 32 | +        builtin.TypeId.ErrorSet => autoHash(hasher, @errorToInt(key)), | 
|  | 33 | +        builtin.TypeId.Promise, builtin.TypeId.Fn => autoHash(hasher, @ptrToInt(key)), | 
|  | 34 | + | 
|  | 35 | +        builtin.TypeId.Pointer => |info| switch (info.size) { | 
|  | 36 | +            builtin.TypeInfo.Pointer.Size.One, | 
|  | 37 | +            builtin.TypeInfo.Pointer.Size.Many, | 
|  | 38 | +            builtin.TypeInfo.Pointer.Size.C, | 
|  | 39 | +            => autoHash(hasher, @ptrToInt(key)), | 
|  | 40 | + | 
|  | 41 | +            builtin.TypeInfo.Pointer.Size.Slice => { | 
|  | 42 | +                autoHash(hasher, key.ptr); | 
|  | 43 | +                autoHash(hasher, key.len); | 
|  | 44 | +            }, | 
|  | 45 | +        }, | 
|  | 46 | + | 
|  | 47 | +        builtin.TypeId.Optional => if (key) |k| autoHash(hasher, k), | 
|  | 48 | + | 
|  | 49 | +        builtin.TypeId.Array => { | 
|  | 50 | +            // TODO detect via a trait when Key has no padding bits to | 
|  | 51 | +            // hash it as an array of bytes. | 
|  | 52 | +            // Otherwise, hash every element. | 
|  | 53 | +            for (key) |element| { | 
|  | 54 | +                autoHash(hasher, element); | 
|  | 55 | +            } | 
|  | 56 | +        }, | 
|  | 57 | + | 
|  | 58 | +        builtin.TypeId.Vector => |info| { | 
|  | 59 | +            if (info.child.bit_count % 8 == 0) { | 
|  | 60 | +                // If there's no unused bits in the child type, we can just hash | 
|  | 61 | +                // this as an array of bytes. | 
|  | 62 | +                hasher.update(mem.asBytes(&key)); | 
|  | 63 | +            } else { | 
|  | 64 | +                // Otherwise, hash every element. | 
|  | 65 | +                // TODO remove the copy to an array once field access is done. | 
|  | 66 | +                const array: [info.len]info.child = key; | 
|  | 67 | +                comptime var i: u32 = 0; | 
|  | 68 | +                inline while (i < info.len) : (i += 1) { | 
|  | 69 | +                    autoHash(hasher, array[i]); | 
|  | 70 | +                } | 
|  | 71 | +            } | 
|  | 72 | +        }, | 
|  | 73 | + | 
|  | 74 | +        builtin.TypeId.Struct => |info| { | 
|  | 75 | +            // TODO detect via a trait when Key has no padding bits to | 
|  | 76 | +            // hash it as an array of bytes. | 
|  | 77 | +            // Otherwise, hash every field. | 
|  | 78 | +            inline for (info.fields) |field| { | 
|  | 79 | +                // We reuse the hash of the previous field as the seed for the | 
|  | 80 | +                // next one so that they're dependant. | 
|  | 81 | +                autoHash(hasher, @field(key, field.name)); | 
|  | 82 | +            } | 
|  | 83 | +        }, | 
|  | 84 | + | 
|  | 85 | +        builtin.TypeId.Union => |info| blk: { | 
|  | 86 | +            if (info.tag_type) |tag_type| { | 
|  | 87 | +                const tag = meta.activeTag(key); | 
|  | 88 | +                const s = autoHash(hasher, tag); | 
|  | 89 | +                inline for (info.fields) |field| { | 
|  | 90 | +                    const enum_field = field.enum_field.?; | 
|  | 91 | +                    if (enum_field.value == @enumToInt(tag)) { | 
|  | 92 | +                        autoHash(hasher, @field(key, enum_field.name)); | 
|  | 93 | +                        // TODO use a labelled break when it does not crash the compiler. | 
|  | 94 | +                        // break :blk; | 
|  | 95 | +                        return; | 
|  | 96 | +                    } | 
|  | 97 | +                } | 
|  | 98 | +                unreachable; | 
|  | 99 | +            } else @compileError("cannot hash untagged union type: " ++ @typeName(Key) ++ ", provide your own hash function"); | 
|  | 100 | +        }, | 
|  | 101 | + | 
|  | 102 | +        builtin.TypeId.ErrorUnion => blk: { | 
|  | 103 | +            const payload = key catch |err| { | 
|  | 104 | +                autoHash(hasher, err); | 
|  | 105 | +                break :blk; | 
|  | 106 | +            }; | 
|  | 107 | +            autoHash(hasher, payload); | 
|  | 108 | +        }, | 
|  | 109 | +    } | 
|  | 110 | +} | 
|  | 111 | + | 
|  | 112 | +const testing = std.testing; | 
|  | 113 | +const Wyhash = std.hash.Wyhash; | 
|  | 114 | + | 
|  | 115 | +fn testAutoHash(key: var) u64 { | 
|  | 116 | +    // Any hash could be used here, for testing autoHash. | 
|  | 117 | +    var hasher = Wyhash.init(0); | 
|  | 118 | +    autoHash(&hasher, key); | 
|  | 119 | +    return hasher.final(); | 
|  | 120 | +} | 
|  | 121 | + | 
|  | 122 | +test "autoHash slice" { | 
|  | 123 | +    // Allocate one array dynamically so that we're assured it is not merged | 
|  | 124 | +    // with the other by the optimization passes. | 
|  | 125 | +    const array1 = try std.heap.direct_allocator.create([6]u32); | 
|  | 126 | +    defer std.heap.direct_allocator.destroy(array1); | 
|  | 127 | +    array1.* = [_]u32{ 1, 2, 3, 4, 5, 6 }; | 
|  | 128 | +    const array2 = [_]u32{ 1, 2, 3, 4, 5, 6 }; | 
|  | 129 | +    const a = array1[0..]; | 
|  | 130 | +    const b = array2[0..]; | 
|  | 131 | +    const c = array1[0..3]; | 
|  | 132 | +    testing.expect(testAutoHash(a) == testAutoHash(a)); | 
|  | 133 | +    testing.expect(testAutoHash(a) != testAutoHash(array1)); | 
|  | 134 | +    testing.expect(testAutoHash(a) != testAutoHash(b)); | 
|  | 135 | +    testing.expect(testAutoHash(a) != testAutoHash(c)); | 
|  | 136 | +} | 
|  | 137 | + | 
|  | 138 | +test "testAutoHash optional" { | 
|  | 139 | +    const a: ?u32 = 123; | 
|  | 140 | +    const b: ?u32 = null; | 
|  | 141 | +    testing.expectEqual(testAutoHash(a), testAutoHash(u32(123))); | 
|  | 142 | +    testing.expect(testAutoHash(a) != testAutoHash(b)); | 
|  | 143 | +    testing.expectEqual(testAutoHash(b), 0); | 
|  | 144 | +} | 
|  | 145 | + | 
|  | 146 | +test "testAutoHash array" { | 
|  | 147 | +    const a = [_]u32{ 1, 2, 3 }; | 
|  | 148 | +    const h = testAutoHash(a); | 
|  | 149 | +    var hasher = Wyhash.init(0); | 
|  | 150 | +    autoHash(&hasher, u32(1)); | 
|  | 151 | +    autoHash(&hasher, u32(2)); | 
|  | 152 | +    autoHash(&hasher, u32(3)); | 
|  | 153 | +    testing.expectEqual(h, hasher.final()); | 
|  | 154 | +} | 
|  | 155 | + | 
|  | 156 | +test "testAutoHash struct" { | 
|  | 157 | +    const Foo = struct { | 
|  | 158 | +        a: u32 = 1, | 
|  | 159 | +        b: u32 = 2, | 
|  | 160 | +        c: u32 = 3, | 
|  | 161 | +    }; | 
|  | 162 | +    const f = Foo{}; | 
|  | 163 | +    const h = testAutoHash(f); | 
|  | 164 | +    var hasher = Wyhash.init(0); | 
|  | 165 | +    autoHash(&hasher, u32(1)); | 
|  | 166 | +    autoHash(&hasher, u32(2)); | 
|  | 167 | +    autoHash(&hasher, u32(3)); | 
|  | 168 | +    testing.expectEqual(h, hasher.final()); | 
|  | 169 | +} | 
|  | 170 | + | 
|  | 171 | +test "testAutoHash union" { | 
|  | 172 | +    const Foo = union(enum) { | 
|  | 173 | +        A: u32, | 
|  | 174 | +        B: f32, | 
|  | 175 | +        C: u32, | 
|  | 176 | +    }; | 
|  | 177 | + | 
|  | 178 | +    const a = Foo{ .A = 18 }; | 
|  | 179 | +    var b = Foo{ .B = 12.34 }; | 
|  | 180 | +    const c = Foo{ .C = 18 }; | 
|  | 181 | +    testing.expect(testAutoHash(a) == testAutoHash(a)); | 
|  | 182 | +    testing.expect(testAutoHash(a) != testAutoHash(b)); | 
|  | 183 | +    testing.expect(testAutoHash(a) != testAutoHash(c)); | 
|  | 184 | + | 
|  | 185 | +    b = Foo{ .A = 18 }; | 
|  | 186 | +    testing.expect(testAutoHash(a) == testAutoHash(b)); | 
|  | 187 | +} | 
|  | 188 | + | 
|  | 189 | +test "testAutoHash vector" { | 
|  | 190 | +    const a: @Vector(4, u32) = [_]u32{ 1, 2, 3, 4 }; | 
|  | 191 | +    const b: @Vector(4, u32) = [_]u32{ 1, 2, 3, 5 }; | 
|  | 192 | +    const c: @Vector(4, u31) = [_]u31{ 1, 2, 3, 4 }; | 
|  | 193 | +    testing.expect(testAutoHash(a) == testAutoHash(a)); | 
|  | 194 | +    testing.expect(testAutoHash(a) != testAutoHash(b)); | 
|  | 195 | +    testing.expect(testAutoHash(a) != testAutoHash(c)); | 
|  | 196 | +} | 
|  | 197 | + | 
|  | 198 | +test "testAutoHash error union" { | 
|  | 199 | +    const Errors = error{Test}; | 
|  | 200 | +    const Foo = struct { | 
|  | 201 | +        a: u32 = 1, | 
|  | 202 | +        b: u32 = 2, | 
|  | 203 | +        c: u32 = 3, | 
|  | 204 | +    }; | 
|  | 205 | +    const f = Foo{}; | 
|  | 206 | +    const g: Errors!Foo = Errors.Test; | 
|  | 207 | +    testing.expect(testAutoHash(f) != testAutoHash(g)); | 
|  | 208 | +    testing.expect(testAutoHash(f) == testAutoHash(Foo{})); | 
|  | 209 | +    testing.expect(testAutoHash(g) == testAutoHash(Errors.Test)); | 
|  | 210 | +} | 
0 commit comments