authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-03 21:56:52 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-03 21:56:52 -07:00
logbb956f2cf60cf5983309a22f1f88facda0a04a16
tree2df82220662c44bac353d120f30feae25296efb8
parent2cb1b3d619b10f4ad2c870f0359adb0c6129ca67
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

update to zig 0.16.0


13 files changed, 67 insertions(+), 95 deletions(-)

README.md+1-1
......@@ -3,7 +3,7 @@
33![loc](https://sloc.xyz/github/nektro/zig-extras)
44[![license](https://img.shields.io/github/license/nektro/zig-extras.svg)](https://github.com/nektro/zig-extras/blob/master/LICENSE)
55[![nektro @ github sponsors](https://img.shields.io/badge/sponsors-nektro-purple?logo=github)](https://github.com/sponsors/nektro)
6[![Zig](https://img.shields.io/badge/Zig-0.15-f7a41d)](https://ziglang.org/)
6[![Zig](https://img.shields.io/badge/Zig-0.16-f7a41d)](https://ziglang.org/)
77[![Zigmod](https://img.shields.io/badge/Zigmod-latest-f7a41d)](https://github.com/nektro/zigmod)
88
99An assortment of random utility functions that aren't in std and don't deserve their own package.
src/FieldUnion.zig+9-14
......@@ -4,22 +4,17 @@ const extras = @import("./lib.zig");
44const expectSimilarType = extras.expectSimilarType;
55
66pub fn FieldUnion(comptime T: type) type {
7 const infos = std.meta.fields(T);
7 const fields = std.meta.fields(T);
8 var names: [fields.fields.len][]const u8 = undefined;
9 var types: [fields.fields.len]type = undefined;
10 var attrs: [fields.fields.len]std.builtin.Type.UnionField.Attributes = undefined;
811
9 var fields: [infos.len]std.builtin.Type.UnionField = undefined;
10 inline for (infos, 0..) |field, i| {
11 fields[i] = .{
12 .name = field.name,
13 .type = field.type,
14 .alignment = field.alignment,
15 };
12 inline for (fields, 0..) |field, i| {
13 names[i] = field.name;
14 types[i] = field.type;
15 attrs[i] = .{ .@"align" = field.alignment };
1616 }
17 return @Type(std.builtin.Type{ .@"union" = .{
18 .layout = .auto,
19 .tag_type = std.meta.FieldEnum(T),
20 .fields = &fields,
21 .decls = &.{},
22 } });
17 return @Union(.auto, std.meta.FieldEnum(T), &names, &types, &attrs);
2318}
2419
2520test {
src/FlippedInt.zig+8-6
......@@ -3,12 +3,14 @@ const string = []const u8;
33const extras = @import("./lib.zig");
44
55pub fn FlippedInt(comptime T: type) type {
6 var info = @typeInfo(T);
7 info.int.signedness = switch (info.int.signedness) {
8 .signed => .unsigned,
9 .unsigned => .signed,
10 };
11 return @Type(info);
6 const info = @typeInfo(T).int;
7 return @Int(
8 switch (info.signedness) {
9 .signed => .unsigned,
10 .unsigned => .signed,
11 },
12 info.signedness,
13 );
1214}
1315
1416test {
src/LoggingReader.zig+1-1
......@@ -2,7 +2,7 @@ const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
44
5pub fn LoggingReader(comptime T: type, comptime scope: @Type(.EnumLiteral)) type {
5pub fn LoggingReader(comptime T: type, comptime scope: @EnumLiteral()) type {
66 return struct {
77 child_stream: T,
88
src/LoggingWriter.zig+1-1
......@@ -2,7 +2,7 @@ const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
44
5pub fn LoggingWriter(comptime T: type, comptime scope: @Type(.EnumLiteral)) type {
5pub fn LoggingWriter(comptime T: type, comptime scope: @EnumLiteral()) type {
66 return struct {
77 child_stream: T,
88
src/OneBiggerInt.zig+2-3
......@@ -3,9 +3,8 @@ const string = []const u8;
33const extras = @import("./lib.zig");
44
55pub fn OneBiggerInt(comptime T: type) type {
6 var info = @typeInfo(T);
7 info.int.bits += 1;
8 return @Type(info);
6 const info = @typeInfo(T).int;
7 return @Int(info.signedness, info.bits + 1);
98}
109
1110test {
src/OneSmallerInt.zig+2-3
......@@ -3,9 +3,8 @@ const string = []const u8;
33const extras = @import("./lib.zig");
44
55pub fn OneSmallerInt(comptime T: type) type {
6 var info = @typeInfo(T);
7 info.int.bits -= 1;
8 return @Type(info);
6 const info = @typeInfo(T).int;
7 return @Int(info.signedness, info.bits - 1);
98}
109
1110test {
src/Partial.zig+9-17
......@@ -6,24 +6,16 @@ const expectSimilarType = extras.expectSimilarType;
66/// Creates a new version of struct T where all fields are optional.
77/// Name inspried by https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype.
88pub fn Partial(comptime T: type) type {
9 const fields_before = std.meta.fields(T);
10 var fields_after: [fields_before.len]std.builtin.Type.StructField = undefined;
11 inline for (fields_before, 0..) |item, i| {
12 fields_after[i] = std.builtin.Type.StructField{
13 .name = item.name,
14 .type = ?item.type,
15 .default_value_ptr = &@as(?item.type, null),
16 .is_comptime = false,
17 .alignment = @alignOf(?item.type),
18 };
9 const fields = std.meta.fields(T);
10 var names: [fields.len][]const u8 = undefined;
11 var types: [fields.len]type = undefined;
12 var attrs: [fields.len]std.builtin.Type.StructField.Attributes = undefined;
13 for (fields, 0..) |item, i| {
14 names[i] = item.name;
15 types[i] = ?item.type;
16 attrs[i] = .{ .default_value_ptr = &@as(?item.type, null) };
1917 }
20 return @Type(@unionInit(std.builtin.Type, "struct", .{
21 .layout = .auto,
22 .backing_integer = null,
23 .fields = &fields_after,
24 .decls = &.{},
25 .is_tuple = false,
26 }));
18 return @Struct(.auto, null, &names, &types, &attrs);
2719}
2820
2921test {
src/ReverseFields.zig+6-5
......@@ -4,14 +4,15 @@ const extras = @import("./lib.zig");
44const expectSimilarType = extras.expectSimilarType;
55
66pub fn ReverseFields(comptime T: type) type {
7 var info = @typeInfo(T).@"struct";
7 const info = @typeInfo(T).@"struct";
88 const len = info.fields.len;
9 var fields: [len]std.builtin.Type.StructField = undefined;
9 var names: [len][]const u8 = undefined;
10 var types: [len]type = undefined;
1011 for (0..len) |i| {
11 fields[i] = info.fields[len - 1 - i];
12 names[i] = info.fields[len - 1 - i].name;
13 types[i] = info.fields[len - 1 - i].type;
1214 }
13 info.fields = &fields;
14 return @Type(.{ .@"struct" = info });
15 return @Struct(.auto, null, &names, &types, &@splat(.{}));
1516}
1617
1718test {
src/StructOfArrays.zig+5-16
......@@ -6,24 +6,13 @@ const expectSimilarType = extras.expectSimilarType;
66pub fn StructOfArrays(len: usize, T: type) type {
77 const info = @typeInfo(T).@"struct";
88 const fields = info.fields;
9 var new_fields: [fields.len]std.builtin.Type.StructField = undefined;
9 var names: [fields.len][]const u8 = undefined;
10 var types: [fields.len]type = undefined;
1011 for (fields, 0..) |item, i| {
11 new_fields[i] = .{
12 .name = item.name,
13 .type = [len]item.type,
14 .default_value_ptr = null,
15 .is_comptime = false,
16 .alignment = @alignOf([len]item.type),
17 };
12 names[i] = item.name;
13 types[i] = [len]item.type;
1814 }
19 const result = new_fields[0..fields.len];
20 return @Type(@unionInit(std.builtin.Type, "struct", .{
21 .layout = .auto,
22 .backing_integer = null,
23 .fields = result,
24 .decls = &.{},
25 .is_tuple = info.is_tuple,
26 }));
15 return @Struct(.auto, null, &names, &types, &@splat(.{}));
2716}
2817
2918test {
src/StructOfSlices.zig+5-16
......@@ -6,24 +6,13 @@ const expectSimilarType = extras.expectSimilarType;
66pub fn StructOfSlices(T: type) type {
77 const info = @typeInfo(T).@"struct";
88 const fields = info.fields;
9 var new_fields: [fields.len]std.builtin.Type.StructField = undefined;
9 var names: [fields.len][]const u8 = undefined;
10 var types: [fields.len]type = undefined;
1011 for (fields, 0..) |item, i| {
11 new_fields[i] = .{
12 .name = item.name,
13 .type = []const item.type,
14 .default_value_ptr = null,
15 .is_comptime = false,
16 .alignment = @alignOf([]const item.type),
17 };
12 names[i] = item.name;
13 types[i] = []const item.type;
1814 }
19 const result = new_fields[0..fields.len];
20 return @Type(@unionInit(std.builtin.Type, "struct", .{
21 .layout = .auto,
22 .backing_integer = null,
23 .fields = result,
24 .decls = &.{},
25 .is_tuple = info.is_tuple,
26 }));
15 return @Struct(.auto, null, &names, &types, &@splat(.{}));
2716}
2817
2918test {
src/join.zig+5-9
......@@ -14,17 +14,13 @@ pub fn join(input: anytype) Join(@TypeOf(input)) {
1414}
1515
1616pub fn Join(comptime T: type) type {
17 var fields: []const std.builtin.Type.StructField = &.{};
17 var names: [][]const u8 = &.{};
18 var types: []type = &.{};
1819 inline for (std.meta.fields(T)) |item| {
1920 inline for (std.meta.fields(item.type)) |f| {
20 fields = fields ++ &[_]std.builtin.Type.StructField{.{
21 .name = f.name,
22 .type = f.type,
23 .default_value_ptr = null,
24 .is_comptime = false,
25 .alignment = @alignOf(f.type),
26 }};
21 names = names ++ &[_][]const u8{f.name};
22 types = types ++ &[_]type{f.type};
2723 }
2824 }
29 return @Type(.{ .@"struct" = .{ .layout = .auto, .fields = fields, .decls = &.{}, .is_tuple = false } });
25 return @Struct(.auto, null, &names, &types, &@splat(.{}));
3026}
src/omit.zig+13-3
......@@ -14,12 +14,22 @@ pub fn omit(value: anytype, comptime field_name: []const u8) Omit(@TypeOf(value)
1414
1515pub fn Omit(T: type, field_name: []const u8) type {
1616 const fields_original = std.meta.fields(T);
17 var fields: [fields_original.len - 1]std.builtin.Type.StructField = undefined;
17 const len = fields_original.len - 1;
18 var names: [len][]const u8 = undefined;
19 var types: [len]type = undefined;
20 var attrs: [len]std.builtin.Type.StructField.Attributes = undefined;
1821 var i: usize = 0;
1922 for (fields_original) |f| {
2023 if (std.mem.eql(u8, f.name, field_name)) continue;
21 fields[i] = f;
24 names[i] = f.name;
25 types[i] = f.type;
26 attrs[i] = .{
27 .@"comptime" = f.is_comptime,
28 .@"align" = f.alignment,
29 .default_value_ptr = f.default_value_ptr,
30 };
2231 i += 1;
2332 }
24 return @Type(.{ .@"struct" = .{ .layout = .auto, .fields = &fields, .decls = &.{}, .is_tuple = false } });
33 return @Struct(.auto, null, &names, &types, &attrs);
34 // return @Type(.{ .@"struct" = .{ .layout = .auto, .fields = &fields, .decls = &.{}, .is_tuple = false } });
2535}