Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions lib/std/enums.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn CombinedEnums(comptime enum_types: anytype) type {
fn Combined(comptime enum_types: anytype) type {

Avoid Redundant Names in Fully-Qualified Namespaces

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fieldCount += std.meta.fields(enum_type).len;
fieldCount += @typeInfo(enum_type).@"enum".fields.len;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like its meant to work on tagged unions as well

Copy link
Contributor

@nektro nektro Sep 6, 2024

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should be using std.testing.expect or expectEqual in tests, assert will cause the test to crash if it fails

}
Loading