nameCast
Deprecated: Use @field(E, @tagName(tag)) or @field(E, string)
Function parameters
Parameters
- E:type
- value:anytype
Functions in this namespace
Functions
- EnumFieldStruct
- Returns a struct with a field matching each unique named enum element.
- valuesFromFields
- Looks up the supplied fields in the given enum type.
- values
- Returns the set of all named values in the given enum, in
- tagName
- A safe alternative to @tagName() for non-exhaustive enums that doesn't
- directEnumArrayLen
- Determines the length of a direct-mapped enum array, indexed by
- directEnumArray
- Initializes an array of Data which can be indexed by
- directEnumArrayDefault
- Initializes an array of Data which can be indexed by
- nameCast
- Deprecated: Use @field(E, @tagName(tag)) or @field(E, string)
- EnumSet
- A set of enum elements, backed by a bitfield.
- EnumMap
- A map keyed by an enum, backed by a bitfield and a dense array.
- EnumMultiset
- A multiset of enum elements up to a count of usize.
- BoundedEnumMultiset
- A multiset of enum elements up to CountSize.
- EnumArray
- An array keyed by an enum, backed by a dense array.
Source
Implementation
pub fn nameCast(comptime E: type, comptime value: anytype) E {
return comptime blk: {
const V = @TypeOf(value);
if (V == E) break :blk value;
const name: ?[]const u8 = switch (@typeInfo(V)) {
.enum_literal, .@"enum" => @tagName(value),
.pointer => value,
else => null,
};
if (name) |n| {
if (@hasField(E, n)) {
break :blk @field(E, n);
}
@compileError("Enum " ++ @typeName(E) ++ " has no field named " ++ n);
}
@compileError("Cannot cast from " ++ @typeName(@TypeOf(value)) ++ " to " ++ @typeName(E));
};
}