commit 8fe076daaf182863d116f2be7b13e3743bbcaae3 (tree)
parent 01ab167ce3c5c0db908b11451f160359230a440f
Author: Vexu <git@vexu.eu>
Date: Thu, 16 Jul 2020 16:00:42 +0300
std.mem.zeroInit support initiating with tuples
Diffstat:
1 file changed, 25 insertions(+), 2 deletions(-)
diff --git a/lib/std/mem.zig b/lib/std/mem.zig
@@ -709,6 +709,14 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
.Struct => |init_info| {
var value = std.mem.zeroes(T);
+ // typeInfo won't tell us if this is a tuple
+ if (comptime eql(u8, init_info.fields[0].name, "0")) {
+ inline for (init_info.fields) |field, i| {
+ @field(value, struct_info.fields[i].name) = @field(init, field.name);
+ }
+ return value;
+ }
+
inline for (init_info.fields) |field| {
if (!@hasField(T, field.name)) {
@compileError("Encountered an initializer for `" ++ field.name ++ "`, but it is not a field of " ++ @typeName(T));
@@ -760,7 +768,7 @@ test "zeroInit" {
.a = 42,
});
- testing.expectEqual(s, S{
+ testing.expectEqual(S{
.a = 42,
.b = null,
.c = .{
@@ -768,7 +776,22 @@ test "zeroInit" {
},
.e = [3]u8{ 0, 0, 0 },
.f = -1,
- });
+ }, s);
+
+ const Color = struct {
+ r: u8,
+ g: u8,
+ b: u8,
+ a: u8,
+ };
+
+ const c = zeroInit(Color, .{255, 255});
+ testing.expectEqual(Color{
+ .r = 255,
+ .g = 255,
+ .b = 0,
+ .a = 0,
+ }, c);
}
/// Compares two slices of numbers lexicographically. O(n).