fix: use correct tag type for union enum

This commit is contained in:
LordMZTE 2023-04-29 16:24:32 +02:00
parent d264778c1f
commit c012383247
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6

View file

@ -1,7 +1,7 @@
const std = @import("std"); const std = @import("std");
/// Function type that returns a prototype struct. /// Function type that returns a prototype struct.
/// Self will be the resulting statspatch struct. Methods must use this for the /// Self will be the resulting statspatch struct. Methods must use this for the
/// self-parameter instead of @This()! /// self-parameter instead of @This()!
pub const PrototypeFn = fn (comptime Self: type) type; pub const PrototypeFn = fn (comptime Self: type) type;
@ -32,16 +32,12 @@ pub fn StatspatchType(comptime Prototype: PrototypeFn, comptime impls: []const t
pub inline fn implcall( pub inline fn implcall(
/// The statspatch type. Passed as argument to the prototype function /// The statspatch type. Passed as argument to the prototype function
self: anytype, self: anytype,
/// How the self-parameter to the function should be handled. /// How the self-parameter to the function should be handled.
comptime self_arg: enum { none, self, ptr, const_ptr }, comptime self_arg: enum { none, self, ptr, const_ptr },
/// Name of the function to call /// Name of the function to call
comptime func_name: []const u8, comptime func_name: []const u8,
/// Function return type /// Function return type
comptime Return: type, comptime Return: type,
/// Argument tuple to pass excluding self argument. /// Argument tuple to pass excluding self argument.
args: anytype, args: anytype,
) Return { ) Return {
@ -67,7 +63,7 @@ fn Union(comptime impls: []const type) type {
} }
const Enum = @Type(.{ .Enum = .{ const Enum = @Type(.{ .Enum = .{
.tag_type = std.meta.Int(.unsigned, std.math.log2(impls.len)), .tag_type = std.meta.Int(.unsigned, std.math.log2_int_ceil(u16, impls.len)),
.fields = enum_fields, .fields = enum_fields,
.decls = &.{}, .decls = &.{},
.is_exhaustive = true, .is_exhaustive = true,
@ -94,8 +90,16 @@ fn Union(comptime impls: []const type) type {
test "Union" { test "Union" {
const Impl1 = struct { foo: u32 }; const Impl1 = struct { foo: u32 };
const Impl2 = struct { bar: u16 }; const Impl2 = struct { bar: u16 };
_ = Union(&.{ Impl1, Impl2 }); const Impl3 = struct { baz: bool };
const U = Union(&.{ Impl1, Impl2, Impl3 });
// Couldn't figure out how to test this :/ // Couldn't figure out how to test this :/
std.debug.print(
"Union: \n{s}\n",
.{comptime std.fmt.comptimePrint("{}\n\n{}", .{
@typeInfo(@typeInfo(U).Union.tag_type.?).Enum,
@typeInfo(U).Union,
})},
);
} }
test "StatspatchType" { test "StatspatchType" {