| ... | @@ -16,7 +16,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def | ... | @@ -16,7 +16,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def |
| 16 | fields = fields ++ &[_]StructField{.{ | 16 | fields = fields ++ &[_]StructField{.{ |
| 17 | .name = field.name, | 17 | .name = field.name, |
| 18 | .field_type = Data, | 18 | .field_type = Data, |
| 19 | .default_value = if (field_default) |d| &d else null, | 19 | .default_value = if (field_default) |d| @ptrCast(?*const anyopaque, &d) else null, |
| 20 | .is_comptime = false, | 20 | .is_comptime = false, |
| 21 | .alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0, | 21 | .alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0, |
| 22 | }}; | 22 | }}; |
| ... | @@ -160,6 +160,20 @@ test "std.enums.directEnumArrayDefault" { | ... | @@ -160,6 +160,20 @@ test "std.enums.directEnumArrayDefault" { |
| 160 | try testing.expectEqual(false, array[2]); | 160 | try testing.expectEqual(false, array[2]); |
| 161 | } | 161 | } |
| 162 | | 162 | |
| | 163 | test "std.enums.directEnumArrayDefault slice" { |
| | 164 | const E = enum(i4) { a = 4, b = 6, c = 2 }; |
| | 165 | var runtime_b = "b"; |
| | 166 | const array = directEnumArrayDefault(E, []const u8, "default", 4, .{ |
| | 167 | .a = "a", |
| | 168 | .b = runtime_b, |
| | 169 | }); |
| | 170 | |
| | 171 | try testing.expectEqual([7][]const u8, @TypeOf(array)); |
| | 172 | try testing.expectEqualSlices(u8, "a", array[4]); |
| | 173 | try testing.expectEqualSlices(u8, "b", array[6]); |
| | 174 | try testing.expectEqualSlices(u8, "default", array[2]); |
| | 175 | } |
| | 176 | |
| 163 | /// Cast an enum literal, value, or string to the enum value of type E | 177 | /// Cast an enum literal, value, or string to the enum value of type E |
| 164 | /// with the same name. | 178 | /// with the same name. |
| 165 | pub fn nameCast(comptime E: type, comptime value: anytype) E { | 179 | pub fn nameCast(comptime E: type, comptime value: anytype) E { |