Created
November 25, 2018 10:35
-
-
Save daurnimator/039529a207cbdf3c9360e787a6141c4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
fn stringToEnum(comptime T: type, str: []const u8) ?T { | |
inline for (@typeInfo(T).Enum.fields) |enumField| { | |
if (std.mem.eql(u8, str, enumField.name)) { | |
return @intToEnum(T, @intCast(@TagType(T), enumField.value)); | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This would be a good addition to
std.meta
, however it should returnerror.InvalidEnum
(see std.meta.intToEnum) instead of null.