mod: adapt implementation to zig version 0.15.2
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 54s
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 54s
This commit is contained in:
+25
-35
@@ -46,44 +46,21 @@ pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
|
||||
const b_fields = @typeInfo(B).@"union".fields;
|
||||
const b_fields_tag = @typeInfo(B).@"union".tag_type.?;
|
||||
const b_enum_fields = @typeInfo(b_fields_tag).@"enum".fields;
|
||||
var field_names: [a_fields.len + b_fields.len][:0]const u8 = undefined;
|
||||
var field_types: [a_fields.len + b_fields.len]type = undefined;
|
||||
var field_attributes: [a_fields.len + b_fields.len]std.builtin.Type.UnionField.Attributes = undefined;
|
||||
var enum_names: [a_fields.len + b_fields.len][:0]const u8 = undefined;
|
||||
const UEnumSize = blk: {
|
||||
const total_size = a_fields.len + b_fields.len;
|
||||
break :blk switch (total_size) {
|
||||
1...2 => u1,
|
||||
3...4 => u2,
|
||||
5...8 => u4,
|
||||
9...16 => u4,
|
||||
17...32 => u5,
|
||||
33...64 => u6,
|
||||
65...128 => u7,
|
||||
129...256 => u8,
|
||||
else => u16, // should suffice
|
||||
};
|
||||
};
|
||||
var enum_values: [a_fields.len + b_fields.len]UEnumSize = undefined;
|
||||
var fields: [a_fields.len + b_fields.len]std.builtin.Type.UnionField = undefined;
|
||||
var enum_fields: [a_fields.len + b_fields.len]std.builtin.Type.EnumField = undefined;
|
||||
var i: usize = 0;
|
||||
for (a_fields, a_enum_fields) |field, enum_field| {
|
||||
field_names[i] = field.name;
|
||||
field_types[i] = field.type;
|
||||
field_attributes[i] = .{ .@"align" = field.alignment };
|
||||
fields[i] = field;
|
||||
var enum_f = enum_field;
|
||||
enum_f.value = i;
|
||||
enum_names[i] = enum_f.name;
|
||||
enum_values[i] = enum_f.value;
|
||||
enum_fields[i] = enum_f;
|
||||
i += 1;
|
||||
}
|
||||
for (b_fields, b_enum_fields) |field, enum_field| {
|
||||
field_names[i] = field.name;
|
||||
field_types[i] = field.type;
|
||||
field_attributes[i] = .{ .@"align" = field.alignment };
|
||||
fields[i] = field;
|
||||
var enum_f = enum_field;
|
||||
enum_f.value = i;
|
||||
enum_names[i] = enum_f.name;
|
||||
enum_values[i] = enum_f.value;
|
||||
enum_fields[i] = enum_f;
|
||||
i += 1;
|
||||
}
|
||||
|
||||
@@ -94,22 +71,35 @@ pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
|
||||
// user provided one)
|
||||
const a_enum_decls = @typeInfo(A).@"union".decls;
|
||||
const b_enum_decls = @typeInfo(B).@"union".decls;
|
||||
var decl_names: [a_enum_decls.len + b_enum_decls.len]std.builtin.Type.Declaration = undefined;
|
||||
var decls: [a_enum_decls.len + b_enum_decls.len]std.builtin.Type.Declaration = undefined;
|
||||
var j: usize = 0;
|
||||
for (a_enum_decls) |decl| {
|
||||
decl_names[j] = decl.name;
|
||||
decls[j] = decl;
|
||||
j += 1;
|
||||
}
|
||||
for (b_enum_decls) |decl| {
|
||||
decl_names[j] = decl.name;
|
||||
decls[j] = decl;
|
||||
j += 1;
|
||||
}
|
||||
|
||||
const EventType = @Int(.unsigned, @bitSizeOf(@TypeOf(i)) - @clz(i));
|
||||
const EventType = @Type(.{ .int = .{
|
||||
.signedness = .unsigned,
|
||||
.bits = @bitSizeOf(@TypeOf(i)) - @clz(i),
|
||||
} });
|
||||
|
||||
const Event = @Enum(EventType, .exhaustive, enum_names[0..], enum_values[0..]);
|
||||
const Event = @Type(.{ .@"enum" = .{
|
||||
.tag_type = EventType,
|
||||
.fields = enum_fields[0..],
|
||||
.decls = &.{},
|
||||
.is_exhaustive = true,
|
||||
} });
|
||||
|
||||
return @Union(.auto, Event, field_names[0..], field_types[0..], field_attributes[0..]);
|
||||
return @Type(.{ .@"union" = .{
|
||||
.layout = .auto,
|
||||
.tag_type = Event,
|
||||
.fields = fields[0..],
|
||||
.decls = &.{},
|
||||
} });
|
||||
}
|
||||
|
||||
/// Determine whether the provided type `T` is a tagged union: `union(enum)`.
|
||||
|
||||
Reference in New Issue
Block a user