Function takeEnum [src]

Reads an integer with the same size as the given enum's tag type. If the integer matches an enum tag, casts the integer to the enum tag and returns it. Otherwise, returns error.InvalidEnumTag. Asserts the buffer was initialized with a capacity at least @sizeOf(Enum).

Prototype

pub fn takeEnum(r: *Reader, comptime Enum: type, endian: std.builtin.Endian) TakeEnumError!Enum

Parameters

r: *ReaderEnum: typeendian: std.builtin.Endian

Possible Errors

EndOfStream Error
InvalidEnumTag
ReadFailed Error

See the Reader implementation for detailed diagnostics.

Example

test takeEnum { var r: Reader = .fixed(&.{ 2, 0, 1 }); const E1 = enum(u8) { a, b, c }; const E2 = enum(u16) { _ }; try testing.expectEqual(E1.c, try r.takeEnum(E1, .little)); try testing.expectEqual(@as(E2, @enumFromInt(0x0001)), try r.takeEnum(E2, .big)); }

Source

pub fn takeEnum(r: *Reader, comptime Enum: type, endian: std.builtin.Endian) TakeEnumError!Enum { const Tag = @typeInfo(Enum).@"enum".tag_type; const int = try r.takeInt(Tag, endian); return std.meta.intToEnum(Enum, int); }