Skip to content

Commit

Permalink
Merge pull request #16 from rupurt/fix-comptime-bitmask-zig-0.11
Browse files Browse the repository at this point in the history
fix comptime Bitmask for Zig >= 0.11
  • Loading branch information
mjoerussell authored Feb 13, 2024
2 parents c6b1462 + 0c0a051 commit 4d12f30
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/util.zig
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const std = @import("std");
const TypeInfo = std.builtin.TypeInfo;
const Type = std.builtin.Type;

pub fn Bitmask(comptime BackingType: type, comptime fields: anytype) type {
const BaseStruct = packed struct {};
var base_struct_info = @typeInfo(BaseStruct);

var incoming_fields: [fields.len]TypeInfo.StructField = undefined;
var incoming_fields: [fields.len]Type.StructField = undefined;
inline for (fields, 0..) |field, i| {
incoming_fields[i] = .{ .name = field[0], .field_type = bool, .default_value = false, .is_comptime = false, .alignment = @alignOf(bool) };
incoming_fields[i] = .{ .name = field[0], .type = bool, .default_value = &false, .is_comptime = false, .alignment = @alignOf(bool) };
}

base_struct_info.Struct.fields = incoming_fields[0..];

const BitmaskFields = @Type(base_struct_info);
const BitmaskFields = @Type(.{ .Struct = .{
.layout = .Auto,
.fields = incoming_fields[0..],
.decls = &[_]std.builtin.Type.Declaration{},
.is_tuple = false,
} });

return struct {
pub const Result = BitmaskFields;
Expand Down Expand Up @@ -49,12 +49,12 @@ pub fn Bitmask(comptime BackingType: type, comptime fields: anytype) type {
pub fn EnumErrorSet(comptime BaseEnum: type) type {
switch (@typeInfo(BaseEnum)) {
.Enum => |info| {
var error_set: [info.fields.len]TypeInfo.Error = undefined;
var error_set: [info.fields.len]Type.Error = undefined;
inline for (info.fields, 0..) |field, index| {
error_set[index] = .{ .name = field.name };
}

return @Type(TypeInfo{ .ErrorSet = error_set[0..] });
return @Type(Type{ .ErrorSet = error_set[0..] });
},
else => @compileError("EnumErrorSet requires an enum, found " ++ @typeName(BaseEnum)),
}
Expand Down

0 comments on commit 4d12f30

Please sign in to comment.