mirror of
https://github.com/ziglang/zig.git
synced 2026-02-14 21:38:33 +00:00
Add initialization helper
When using C libraries, C99 designator list initialization is often
times used to initialize data structure.
While `std.mem.zeroes` and manually assigning to each field can
achieve the same result, it is much more verbose then the equivalent
C code:
```zig
usingnamespace @cImport({
@cInclude("sokol_app.h");
});
// Using `std.mem.zeroes` and manual assignment.
var app_desc = std.mem.zeroes(sapp_desc);
app_desc.init_cb = init;
app_desc.frame_cb = frame;
app_desc.cleanup_cb = cleanup;
app_desc.width = 400;
app_desc.height = 300;
app_desc.window_name = "no default init";
// Using `std.mem.defaultInit`.
var app_desc = std.mem.defaultInit(sapp_desc, .{
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.width = 400,
.height = 300,
.window_name = "default init"
});
```
The `std.mem.defaultInit` aims to solve this problem by zero
initializing all fields of the given struct to their zero, or default
value if any. Each field mentionned in the `init` variable is then
assigned to the corresponding field in the struct.
If a field is a struct, and an initializer for it is present, it is
recursively initialized.
This commit is contained in:
parent
937dcad0b3
commit
c0e5eca6f2
@ -516,6 +516,80 @@ test "mem.secureZero" {
|
||||
testing.expectEqualSlices(u8, a[0..], b[0..]);
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn defaultInit(comptime T: type, init: var) T {
|
||||
comptime const Init = @TypeOf(init);
|
||||
|
||||
switch (@typeInfo(T)) {
|
||||
.Struct => |struct_info| {
|
||||
switch (@typeInfo(Init)) {
|
||||
.Struct => |init_info| {
|
||||
var value = std.mem.zeroes(T);
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
inline for (struct_info.fields) |field| {
|
||||
if (@hasField(Init, field.name)) {
|
||||
switch (@typeInfo(field.field_type)) {
|
||||
.Struct => {
|
||||
@field(value, field.name) = defaultInit(field.field_type, @field(init, field.name));
|
||||
},
|
||||
else => {
|
||||
@field(value, field.name) = @field(init, field.name);
|
||||
},
|
||||
}
|
||||
} else if (field.default_value != null) {
|
||||
@field(value, field.name) = field.default_value;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
},
|
||||
else => {
|
||||
@compileError("The initializer must be a struct");
|
||||
},
|
||||
}
|
||||
},
|
||||
else => {
|
||||
@compileError("Can't default init a " ++ @typeName(T));
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test "mem.defaultInit" {
|
||||
const I = struct {
|
||||
d: f64,
|
||||
};
|
||||
|
||||
const S = struct {
|
||||
a: u32,
|
||||
b: ?bool,
|
||||
c: I,
|
||||
e: [3]u8,
|
||||
f: i64,
|
||||
};
|
||||
|
||||
const s = defaultInit(S, .{
|
||||
.a = 42,
|
||||
});
|
||||
|
||||
testing.expectEqual(s, S{
|
||||
.a = 42,
|
||||
.b = null,
|
||||
.c = .{
|
||||
.d = 0,
|
||||
},
|
||||
.e = [3]u8{0, 0, 0},
|
||||
.f = 0,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn order(comptime T: type, lhs: []const T, rhs: []const T) math.Order {
|
||||
const n = math.min(lhs.len, rhs.len);
|
||||
var i: usize = 0;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user