This module contains utilities and data structures for working with enums.

Functions

fn directEnumArray(comptime E: type, comptime Data: type, comptime max_unused_slots: comptime_int, init_values: EnumFieldStruct(E, Data, null)) [directEnumArrayLen(E, max_unused_slots)]Data

Initializes an array of Data which can be indexed by @intCast(usize, @intFromEn…

Initializes an array of Data which can be indexed by @intCast(usize, @intFromEnum(enum_value)). If the enum is non-exhaustive, the resulting array will only be large enough to hold all explicit fields. If the enum contains any fields with values that cannot be represented by usize, a compile error is issued. The max_unused_slots parameter limits the total number of items which have no matching enum key (holes in the enum numbering). So for example, if an enum has values 1, 2, 5, and 6, max_unused_slots must be at least 3, to allow unused slots 0, 3, and 4. The init_values parameter must be a struct with field names that match the enum values. If the enum has multiple fields with the same value, the name of the first one must be used.

fn directEnumArrayDefault(comptime E: type, comptime Data: type, comptime default: ?Data, comptime max_unused_slots: comptime_int, init_values: EnumFieldStruct(E, Data, default)) [directEnumArrayLen(E, max_unused_slots)]Data

Initializes an array of Data which can be indexed by @intCast(usize, @intFromEn…

Initializes an array of Data which can be indexed by @intCast(usize, @intFromEnum(enum_value)). The enum must be exhaustive. If the enum contains any fields with values that cannot be represented by usize, a compile error is issued. The max_unused_slots parameter limits the total number of items which have no matching enum key (holes in the enum numbering). So for example, if an enum has values 1, 2, 5, and 6, max_unused_slots must be at least 3, to allow unused slots 0, 3, and 4. The init_values parameter must be a struct with field names that match the enum values. If the enum has multiple fields with the same value, the name of the first one must be used.

fn directEnumArrayLen(comptime E: type, comptime max_unused_slots: comptime_int) comptime_int

Determines the length of a direct-mapped enum array, indexed by @intCast(usize,…

Determines the length of a direct-mapped enum array, indexed by @intCast(usize, @intFromEnum(enum_value)). If the enum is non-exhaustive, the resulting length will only be enough to hold all explicit fields. If the enum contains any fields with values that cannot be represented by usize, a compile error is issued. The max_unused_slots parameter limits the total number of items which have no matching enum key (holes in the enum numbering). So for example, if an enum has values 1, 2, 5, and 6, max_unused_slots must be at least 3, to allow unused slots 0, 3, and 4.

fn ensureIndexer(comptime T: type) void

Verifies that a type is a valid Indexer, providing a helpful compile error if n…

Verifies that a type is a valid Indexer, providing a helpful compile error if not. An Indexer maps a comptime-known set of keys to a dense set of zero-based indices. The indexer interface must look like this:

struct {
    /// The key type which this indexer converts to indices
    pub const Key: type,
    /// The number of indexes in the dense mapping
    pub const count: usize,
    /// Converts from a key to an index
    pub fn indexOf(Key) usize;
    /// Converts from an index to a key
    pub fn keyForIndex(usize) Key;
}
fn nameCast(comptime E: type, comptime value: anytype) E

Cast an enum literal, value, or string to the enum value of type E with the sam…

Cast an enum literal, value, or string to the enum value of type E with the same name.

fn tagName(comptime E: type, e: E) ?[]const u8

A safe alternative to @tagName() for non-exhaustive enums that doesn’t panic wh…

A safe alternative to @tagName() for non-exhaustive enums that doesn’t panic when e has no tagged value. Returns the tag name for e or null if no tag exists.

fn values(comptime E: type) []const E

Returns the set of all named values in the given enum, in declaration order.

inline fn valuesFromFields(comptime E: type, comptime fields: []const EnumField) []const E

Looks up the supplied fields in the given enum type. Uses only the field names,…

Looks up the supplied fields in the given enum type. Uses only the field names, field values are ignored. The result array is in the same order as the input.