-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shortened zimpl.zig improving clarity
- Loading branch information
1 parent
fa03944
commit 49a503a
Showing
8 changed files
with
55 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,31 @@ | ||
const std = @import("std"); | ||
const Type = @import("std").builtin.Type; | ||
|
||
pub fn Impl(comptime Type: type, comptime Ifc: fn (type) type) type { | ||
const ifc_fields = @typeInfo(Ifc(Type)).Struct.fields; | ||
var fields: [ifc_fields.len]std.builtin.Type.StructField = undefined; | ||
for (&fields, ifc_fields) |*fld, ifc_fld| { | ||
fld.* = ifc_fld; | ||
// infer default value from Unwrap(Type) | ||
const UWType = Unwrap(Type); | ||
switch (@typeInfo(UWType)) { | ||
pub fn Impl(comptime T: type, comptime Ifc: fn (type) type) type { | ||
const ifc = @typeInfo(Ifc(T)).Struct.fields; | ||
var fields = @as(*const [ifc.len]Type.StructField, @ptrCast(ifc.ptr)).*; | ||
for (&fields) |*field| { | ||
switch (@typeInfo(Unwrap(T))) { | ||
inline else => |info| if (@hasField(@TypeOf(info), "decls")) { | ||
if (@hasDecl(UWType, ifc_fld.name)) { | ||
const decl = @field(UWType, ifc_fld.name); | ||
fld.*.default_value = &@as(ifc_fld.type, decl); | ||
if (@hasDecl(Unwrap(T), field.name)) { | ||
const decl = @field(Unwrap(T), field.name); | ||
field.*.default_value = &@as(field.type, decl); | ||
} | ||
}, | ||
} | ||
} | ||
return @Type(std.builtin.Type{ | ||
.Struct = .{ | ||
.layout = .Auto, | ||
.fields = &fields, | ||
.decls = &[0]std.builtin.Type.Declaration{}, | ||
.is_tuple = false, | ||
}, | ||
}); | ||
return @Type(Type{ .Struct = .{ | ||
.layout = .Auto, | ||
.fields = &fields, | ||
.decls = &[0]Type.Declaration{}, | ||
.is_tuple = false, | ||
} }); | ||
} | ||
|
||
fn Unwrap(comptime Type: type) type { | ||
return switch (@typeInfo(Type)) { | ||
.Pointer => |info| if (info.size == .One) Unwrap(info.child) else Type, | ||
fn Unwrap(comptime T: type) type { | ||
return switch (@typeInfo(T)) { | ||
.Pointer => |info| if (info.size == .One) Unwrap(info.child) else T, | ||
.Optional => |info| Unwrap(info.child), | ||
.ErrorUnion => |info| Unwrap(info.payload), | ||
else => Type, | ||
else => T, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters