| ... | @@ -22,7 +22,7 @@ pub fn fromInt(comptime E: type, integer: anytype) ?E { | ... | @@ -22,7 +22,7 @@ pub fn fromInt(comptime E: type, integer: anytype) ?E { |
| 22 | // without requiring an inline loop. | 22 | // without requiring an inline loop. |
| 23 | // This generates better machine code. | 23 | // This generates better machine code. |
| 24 | for (values(E)) |value| { | 24 | for (values(E)) |value| { |
| 25 | if (@intFromEnum(value) == integer) return @enumFromInt(integer); | 25 | if (@intFromEnum(value) == integer) return value; |
| 26 | } | 26 | } |
| 27 | return null; | 27 | return null; |
| 28 | } | 28 | } |
| ... | @@ -213,6 +213,7 @@ test fromInt { | ... | @@ -213,6 +213,7 @@ test fromInt { |
| 213 | B, | 213 | B, |
| 214 | }; | 214 | }; |
| 215 | const E3 = enum(i8) { A, _ }; | 215 | const E3 = enum(i8) { A, _ }; |
| | 216 | const E4 = enum(u8) { A }; |
| 216 | | 217 | |
| 217 | var zero: u8 = 0; | 218 | var zero: u8 = 0; |
| 218 | var one: u16 = 1; | 219 | var one: u16 = 1; |
| ... | @@ -226,6 +227,10 @@ test fromInt { | ... | @@ -226,6 +227,10 @@ test fromInt { |
| 226 | try testing.expectEqual(null, fromInt(E1, one)); | 227 | try testing.expectEqual(null, fromInt(E1, one)); |
| 227 | try testing.expectEqual(null, fromInt(E3, 128)); | 228 | try testing.expectEqual(null, fromInt(E3, 128)); |
| 228 | try testing.expectEqual(null, fromInt(E3, -129)); | 229 | try testing.expectEqual(null, fromInt(E3, -129)); |
| | 230 | |
| | 231 | // `fromInt` used to produce a compiler error instead of `null` if trying to convert an integer |
| | 232 | // that wasn't out of range, but also wasn't a valid value. |
| | 233 | try testing.expectEqual(null, fromInt(E4, 1)); |
| 229 | } | 234 | } |
| 230 | | 235 | |
| 231 | /// A set of enum elements, backed by a bitfield. If the enum | 236 | /// A set of enum elements, backed by a bitfield. If the enum |