Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Public Function

zeroInit

Public Function
std.mem.zeroInit
pub fn zeroInit(comptime T: type, init: anytype) T

Initializes all fields of the struct with their default value, or zero values if no default value is present. If the field is present in the provided initial values, it will have that value instead. Structs are initialized recursively.

Params

  • T: type
  • init

File

mem.zig:474
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
pub fn zeroInit(comptime T: type, init: anytype) T {
    const Init = @TypeOf(init);

    switch (@typeInfo(T)) {
        .@"struct" => |struct_info| {
            switch (@typeInfo(Init)) {
                .@"struct" => |init_info| {
                    if (init_info.is_tuple) {
                        if (init_info.field_names.len > struct_info.field_names.len) {
                            @compileError("Tuple initializer has more elements than there are fields in `" ++ @typeName(T) ++ "`");
                        }
                    } else {
                        inline for (init_info.field_names) |field_name| {
                            if (!@hasField(T, field_name)) {
                                @compileError("Encountered an initializer for `" ++ field_name ++ "`, but it is not a field of " ++ @typeName(T));
                            }
                        }
                    }

                    var value: T = if (struct_info.layout == .@"extern") zeroes(T) else undefined;

                    inline for (
                        struct_info.field_names,
                        struct_info.field_types,
                        struct_info.field_attrs,
                        0..,
                    ) |f_name, f_type, f_attr, i| {
                        if (f_attr.@"comptime") {
                            continue;
                        }

                        if (init_info.is_tuple and init_info.field_names.len > i) {
                            @field(value, f_name) = @field(init, init_info.field_names[i]);
                        } else if (@hasField(@TypeOf(init), f_name)) {
                            switch (@typeInfo(f_type)) {
                                .@"struct" => {
                                    @field(value, f_name) = zeroInit(f_type, @field(init, f_name));
                                },
                                else => {
                                    @field(value, f_name) = @field(init, f_name);
                                },
                            }
                        } else if (f_attr.defaultValue(f_type)) |val| {
                            @field(value, f_name) = val;
                        } else {
                            switch (@typeInfo(f_type)) {
                                .@"struct" => {
                                    @field(value, f_name) = std.mem.zeroInit(f_type, .{});
                                },
                                else => {
                                    @field(value, f_name) = std.mem.zeroes(@TypeOf(@field(value, f_name)));
                                },
                            }
                        }
                    }

                    return value;
                },
                else => {
                    @compileError("The initializer must be a struct");
                },
            }
        },
        else => {
            @compileError("Can't default init a " ++ @typeName(T));
        },
    }
}