-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std.enums: Add std.enums.CombinedEnums(Tagged)
functions
#21325
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1505,3 +1505,70 @@ test values { | |||||
}; | ||||||
try testing.expectEqualSlices(E, &.{ .X, .Y, .Z }, values(E)); | ||||||
} | ||||||
|
||||||
/// Returns an enum containing the combined elements of the given enum types. | ||||||
/// The values are incremental integers starting at 0. | ||||||
fn CombinedEnums(comptime enum_types: anytype) type { | ||||||
return CombinedEnumsTagged(null, enum_types); | ||||||
} | ||||||
|
||||||
/// Returns an enum with the given tag type containing the combined elements | ||||||
/// of the given enum types. | ||||||
/// The values are incremental integers starting at 0. | ||||||
fn CombinedEnumsTagged(comptime tag: ?type, comptime enum_types: anytype) type { | ||||||
var fieldCount = 0; | ||||||
inline for (enum_types) |enum_type| { | ||||||
fieldCount += std.meta.fields(enum_type).len; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like its meant to work on tagged unions as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my suggestion here was disagreeing. putting unions in and getting an enum out would lead to very confusing behavior to users imo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unless there was a strong motivating use case rather than this being suggested for std out of the blue |
||||||
} | ||||||
|
||||||
var fields: [fieldCount]std.builtin.Type.EnumField = undefined; | ||||||
const decls = &[_]std.builtin.Type.Declaration{}; | ||||||
|
||||||
var idx = 0; | ||||||
inline for (enum_types) |enum_type| { | ||||||
inline for (std.meta.fields(enum_type)) |enum_field| { | ||||||
fields[idx] = .{ | ||||||
.name = enum_field.name, | ||||||
.value = idx, | ||||||
}; | ||||||
idx += 1; | ||||||
} | ||||||
} | ||||||
|
||||||
return @Type(.{ | ||||||
.Enum = .{ | ||||||
.tag_type = tag orelse std.math.IntFittingRange(0, fieldCount - 1), | ||||||
.fields = &fields, | ||||||
.decls = decls, | ||||||
.is_exhaustive = true, | ||||||
}, | ||||||
}); | ||||||
} | ||||||
|
||||||
test CombinedEnums { | ||||||
const Fruit = enum(i32) { | ||||||
apple = 1, | ||||||
banana, | ||||||
mango, | ||||||
}; | ||||||
const Vegetable = enum { | ||||||
carrot, | ||||||
broccoli, | ||||||
spinach, | ||||||
}; | ||||||
const Snack = union(enum) { | ||||||
const Bread = enum { | ||||||
garlic_bread, | ||||||
bread_sticks, | ||||||
bruschetta, | ||||||
}; | ||||||
|
||||||
chips, | ||||||
bread: Bread, | ||||||
}; | ||||||
const Consumable = CombinedEnums(.{ Fruit, Vegetable, Snack }); | ||||||
|
||||||
std.debug.assert(@intFromEnum(Consumable.apple) == 0); | ||||||
std.debug.assert(@intFromEnum(Consumable.carrot) != 0); | ||||||
std.debug.assert(@intFromEnum(Consumable.chips) != @intFromEnum(Consumable.bread)); | ||||||
Comment on lines
+1571
to
+1573
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should be using |
||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid Redundant Names in Fully-Qualified Namespaces