Skip to content

Instantly share code, notes, and snippets.

@daurnimator
Created November 25, 2018 10:35
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