Skip to content

Instantly share code, notes, and snippets.

@daurnimator
Created November 25, 2018 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daurnimator/039529a207cbdf3c9360e787a6141c4c to your computer and use it in GitHub Desktop.
Save daurnimator/039529a207cbdf3c9360e787a6141c4c to your computer and use it in GitHub Desktop.
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;
}
@tgschultz
Copy link

This would be a good addition to std.meta, however it should return error.InvalidEnum (see std.meta.intToEnum) instead of null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment