Commit 8fe076daaf

Vexu <git@vexu.eu>
2020-07-16 15:00:42
std.mem.zeroInit support initiating with tuples
1 parent 01ab167
Changed files (1)
lib
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).