Function tagName [src]

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.

Prototype

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

Parameters

E: typee: E

Example

test tagName { const E = enum(u8) { a, b, _ }; try testing.expect(tagName(E, .a) != null); try testing.expectEqualStrings("a", tagName(E, .a).?); try testing.expect(tagName(E, @as(E, @enumFromInt(42))) == null); }

Source

pub fn tagName(comptime E: type, e: E) ?[:0]const u8 { return inline for (@typeInfo(E).@"enum".fields) |f| { if (@intFromEnum(e) == f.value) break f.name; } else null; }