mirror of
https://github.com/ziglang/zig.git
synced 2026-01-20 06:15:21 +00:00
use @ptrCast to assigned generic type to default_value
If the type happens to be a pointer, then the double pointer will not coerce implicitly.
This commit is contained in:
parent
38a50f819c
commit
e915b905e0
@ -296,7 +296,7 @@ const emutls_control = extern struct {
|
||||
.size = @sizeOf(T),
|
||||
.alignment = @alignOf(T),
|
||||
.object = .{ .index = 0 },
|
||||
.default_value = @ptrCast(?*anyopaque, default_value),
|
||||
.default_value = @ptrCast(?*const anyopaque, default_value),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -16,7 +16,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
|
||||
fields = fields ++ &[_]StructField{.{
|
||||
.name = field.name,
|
||||
.field_type = Data,
|
||||
.default_value = if (field_default) |d| &d else null,
|
||||
.default_value = if (field_default) |d| @ptrCast(?*const anyopaque, &d) else null,
|
||||
.is_comptime = false,
|
||||
.alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0,
|
||||
}};
|
||||
@ -160,6 +160,20 @@ test "std.enums.directEnumArrayDefault" {
|
||||
try testing.expectEqual(false, array[2]);
|
||||
}
|
||||
|
||||
test "std.enums.directEnumArrayDefault slice" {
|
||||
const E = enum(i4) { a = 4, b = 6, c = 2 };
|
||||
var runtime_b = "b";
|
||||
const array = directEnumArrayDefault(E, []const u8, "default", 4, .{
|
||||
.a = "a",
|
||||
.b = runtime_b,
|
||||
});
|
||||
|
||||
try testing.expectEqual([7][]const u8, @TypeOf(array));
|
||||
try testing.expectEqualSlices(u8, "a", array[4]);
|
||||
try testing.expectEqualSlices(u8, "b", array[6]);
|
||||
try testing.expectEqualSlices(u8, "default", array[2]);
|
||||
}
|
||||
|
||||
/// Cast an enum literal, value, or string to the enum value of type E
|
||||
/// with the same name.
|
||||
pub fn nameCast(comptime E: type, comptime value: anytype) E {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user