mirror of
https://github.com/ziglang/zig.git
synced 2025-12-14 02:03:08 +00:00
34 lines
626 B
Zig
34 lines
626 B
Zig
const std = @import("std");
|
|
const builtin = @import("builtin");
|
|
const testing = std.testing;
|
|
|
|
test {
|
|
const g: error{Test}!void = error.Test;
|
|
|
|
var v: u32 = 0;
|
|
hash(&v, g);
|
|
try testing.expect(v == 1);
|
|
}
|
|
|
|
fn hash(v: *u32, key: anytype) void {
|
|
const Key = @TypeOf(key);
|
|
|
|
if (@typeInfo(Key) == .ErrorSet) {
|
|
v.* += 1;
|
|
return;
|
|
}
|
|
|
|
switch (@typeInfo(Key)) {
|
|
.ErrorUnion => blk: {
|
|
const payload = key catch |err| {
|
|
hash(v, err);
|
|
break :blk;
|
|
};
|
|
|
|
hash(v, payload);
|
|
},
|
|
|
|
else => unreachable,
|
|
}
|
|
}
|