summaryrefslogtreecommitdiff
path: root/9proc/src/vars.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-21 14:38:16 -0300
committerGabriel Schneider <[email protected]>2026-10-21 14:44:48 -0300
commitd5c37d9122240fe95d308fbe15874e14dd786c5a (patch)
tree3ed108d2af5bd7a5cb07491b16e855e5243b5d63 /9proc/src/vars.zig
parentfe9772b178575fcab93de09fc61c2f13db7db1a8 (diff)
downloadcloud9-d5c37d9122240fe95d308fbe15874e14dd786c5a.tar.gz
cloud9-d5c37d9122240fe95d308fbe15874e14dd786c5a.zip
Build with Zig 0.17
Diffstat (limited to '9proc/src/vars.zig')
-rw-r--r--9proc/src/vars.zig86
1 files changed, 45 insertions, 41 deletions
diff --git a/9proc/src/vars.zig b/9proc/src/vars.zig
index 20cadfc..90a5ee4 100644
--- a/9proc/src/vars.zig
+++ b/9proc/src/vars.zig
@@ -100,8 +100,9 @@ fn hasFields(comptime T: type, depth: u8) bool {
fn fieldCount(comptime T: type) usize {
var n: usize = 0;
- for (@typeInfo(T).@"struct".fields) |f| {
- if (!f.is_comptime and @sizeOf(f.type) != 0) n += 1;
+ const info = @typeInfo(T).@"struct";
+ for (info.field_types, info.field_attrs) |F, attrs| {
+ if (!attrs.@"comptime" and @sizeOf(F) != 0) n += 1;
}
return n;
}
@@ -110,9 +111,10 @@ fn countNodes(comptime T: type, depth: u8) usize {
var n: usize = 6; // dir + value, type, size, addr, raw
if (hasFields(T, depth)) {
n += 1; // f
- for (@typeInfo(T).@"struct".fields) |f| {
- if (f.is_comptime or @sizeOf(f.type) == 0) continue;
- n += countNodes(f.type, depth + 1);
+ const info = @typeInfo(T).@"struct";
+ for (info.field_types, info.field_attrs) |F, attrs| {
+ if (attrs.@"comptime" or @sizeOf(F) == 0) continue;
+ n += countNodes(F, depth + 1);
}
}
return n;
@@ -137,9 +139,10 @@ fn fill(nodes: []Node, next: *usize, idx: usize, comptime T: type, name: []const
next.* += nf;
nodes[fdir] = .{ .name = "f", .kind = .fields, .parent = me, .first = @intCast(ffirst), .count = @intCast(nf), .offset = offset, .size = @sizeOf(T) };
var i: usize = 0;
- for (@typeInfo(T).@"struct".fields) |f| {
- if (f.is_comptime or @sizeOf(f.type) == 0) continue;
- fill(nodes, next, ffirst + i, f.type, f.name, offset + @offsetOf(T, f.name), depth + 1, fdir);
+ const info = @typeInfo(T).@"struct";
+ for (info.field_names, info.field_types, info.field_attrs) |field_name, F, attrs| {
+ if (attrs.@"comptime" or @sizeOf(F) == 0) continue;
+ fill(nodes, next, ffirst + i, F, field_name, offset + @offsetOf(T, field_name), depth + 1, fdir);
i += 1;
}
}
@@ -198,7 +201,7 @@ pub fn render(ptr: anytype, w: *Writer, depth: usize) Writer.Error!void {
fn isPlainStruct(comptime T: type) bool {
return switch (@typeInfo(T)) {
- .@"struct" => |s| !s.is_tuple and s.fields.len > 0,
+ .@"struct" => |s| !s.is_tuple and s.field_names.len > 0,
else => false,
};
}
@@ -210,25 +213,26 @@ fn renderStruct(comptime T: type, ptr: *const T, w: *Writer, depth: usize, inden
try w.writeAll("…\n");
return;
}
- const packed_layout = @typeInfo(T).@"struct".layout == .@"packed";
- inline for (@typeInfo(T).@"struct".fields) |f| {
+ const info = @typeInfo(T).@"struct";
+ const packed_layout = info.layout == .@"packed";
+ inline for (info.field_names, info.field_types, info.field_attrs) |field_name, F, attrs| {
try w.splatByteAll(' ', indent);
- try w.writeAll(f.name);
+ try w.writeAll(field_name);
try w.writeByte(':');
- if (comptime f.is_comptime) {
+ if (comptime attrs.@"comptime") {
try w.writeAll(" (comptime)\n");
} else if (comptime packed_layout) {
// Fields of a packed struct have no byte address: render a copy.
- const v = @field(ptr.*, f.name);
+ const v = @field(ptr.*, field_name);
try w.writeByte(' ');
- try renderValue(f.type, &v, w, depth - 1);
+ try renderValue(F, &v, w, depth - 1);
try w.writeByte('\n');
- } else if (comptime isPlainStruct(f.type)) {
+ } else if (comptime isPlainStruct(F)) {
try w.writeByte('\n');
- try renderStruct(f.type, &@field(ptr.*, f.name), w, depth - 1, indent + 2);
+ try renderStruct(F, &@field(ptr.*, field_name), w, depth - 1, indent + 2);
} else {
try w.writeByte(' ');
- try renderValue(f.type, &@field(ptr.*, f.name), w, depth - 1);
+ try renderValue(F, &@field(ptr.*, field_name), w, depth - 1);
try w.writeByte('\n');
}
}
@@ -255,15 +259,15 @@ fn renderValue(comptime T: type, ptr: *const T, w: *Writer, depth: usize) Writer
// value is a safety panic, and the value is caller memory we do not
// control. A load through the tag type would truncate to its bit
// width (a u2 tag in a byte), so the full storage width is read.
- if (@sizeOf(T) == 0) return w.writeAll(e.fields[0].name);
- const Raw = std.meta.Int(.unsigned, @sizeOf(T) * 8);
+ if (@sizeOf(T) == 0) return w.writeAll(e.field_names[0]);
+ const Raw = @Int(.unsigned, @sizeOf(T) * 8);
const raw = @as(*align(@alignOf(T)) const Raw, @ptrCast(ptr)).*;
- const TagU = std.meta.Int(.unsigned, @bitSizeOf(e.tag_type));
+ const TagU = @Int(.unsigned, @bitSizeOf(e.tag_type));
const padding: Raw = if (@bitSizeOf(TagU) == @bitSizeOf(Raw)) 0 else ~@as(Raw, std.math.maxInt(TagU));
if (raw & padding == 0) {
const low: TagU = @truncate(raw);
- inline for (e.fields) |f| {
- if (low == @as(TagU, @bitCast(@as(e.tag_type, f.value)))) return w.writeAll(f.name);
+ inline for (e.field_names, e.field_values) |field_name, value| {
+ if (low == @as(TagU, @bitCast(@as(e.tag_type, value)))) return w.writeAll(field_name);
}
}
try w.print("{d}", .{raw});
@@ -306,24 +310,24 @@ fn renderValue(comptime T: type, ptr: *const T, w: *Writer, depth: usize) Writer
try w.writeAll("…");
return;
}
- if (s.fields.len == 0) {
+ if (s.field_names.len == 0) {
try w.writeAll("{}");
return;
}
try w.writeAll("{ ");
- inline for (s.fields, 0..) |f, i| {
+ inline for (s.field_names, s.field_types, s.field_attrs, 0..) |field_name, F, attrs, i| {
if (i != 0) try w.writeAll(", ");
if (!s.is_tuple) {
- try w.writeAll(f.name);
+ try w.writeAll(field_name);
try w.writeAll(": ");
}
- if (comptime f.is_comptime) {
+ if (comptime attrs.@"comptime") {
try w.writeAll("(comptime)");
} else if (comptime s.layout == .@"packed") {
- const v = @field(ptr.*, f.name);
- try renderValue(f.type, &v, w, depth - 1);
+ const v = @field(ptr.*, field_name);
+ try renderValue(F, &v, w, depth - 1);
} else {
- try renderValue(f.type, &@field(ptr.*, f.name), w, depth - 1);
+ try renderValue(F, &@field(ptr.*, field_name), w, depth - 1);
}
}
try w.writeAll(" }");
@@ -338,13 +342,13 @@ fn renderValue(comptime T: type, ptr: *const T, w: *Writer, depth: usize) Writer
return;
}
// A switch on a corrupt tag is a safety panic: match the integer first.
- const raw = @intFromEnum(@as(Tag, ptr.*));
- inline for (u.fields) |f| {
- if (raw == @intFromEnum(@field(Tag, f.name))) {
- try w.writeAll(f.name);
- if (f.type != void) {
+ const raw = @backingInt(@as(Tag, ptr.*));
+ inline for (u.field_names, u.field_types) |field_name, F| {
+ if (raw == @backingInt(@field(Tag, field_name))) {
+ try w.writeAll(field_name);
+ if (F != void) {
try w.writeAll(": ");
- try renderValue(f.type, &@field(ptr.*, f.name), w, depth - 1);
+ try renderValue(F, &@field(ptr.*, field_name), w, depth - 1);
}
return;
}
@@ -421,9 +425,9 @@ pub fn set(ptr: anytype, text: []const u8) SetError!void {
.@"enum" => |e| {
if (std.meta.stringToEnum(T, s)) |v| {
ptr.* = v;
- } else if (!e.is_exhaustive) {
+ } else if (e.mode != .exhaustive) {
const raw = std.fmt.parseInt(e.tag_type, s, 0) catch return error.Invalid;
- ptr.* = @enumFromInt(raw);
+ ptr.* = @fromBackingInt(@intCast(raw));
} else return error.Invalid;
},
else => return error.Unsupported,
@@ -466,7 +470,7 @@ test "render scalars, strings, pointers, optionals, enums, arrays" {
const e: E = .green;
try testing.expectEqualStrings("green", try renderToBuf(&buf, &e));
const NE = enum(u8) { a, _ };
- const ne: NE = @enumFromInt(9);
+ const ne: NE = @fromBackingInt(@intCast(9));
try testing.expectEqualStrings("9", try renderToBuf(&buf, &ne));
const arr = [_]u16{ 1, 2, 3 };
try testing.expectEqualStrings("[1, 2, 3]", try renderToBuf(&buf, &arr));
@@ -508,7 +512,7 @@ test "render structs multi-line with nested indentation and depth limit" {
}
test "long strings and arrays are truncated" {
- const long = [_]u8{'x'} ** 300;
+ const long: [300]u8 = @splat('x');
var buf: [1024]u8 = undefined;
const s: []const u8 = &long;
const out = try renderToBuf(&buf, &s);
@@ -802,7 +806,7 @@ test "set rejects hostile input without partial writes" {
const NE = enum(u8) { a, _ };
var ne: NE = .a;
try set(&ne, "200");
- try testing.expectEqual(@as(u8, 200), @intFromEnum(ne));
+ try testing.expectEqual(@as(u8, 200), @backingInt(ne));
try testing.expectError(error.Invalid, set(&ne, "256"));
try testing.expectError(error.Invalid, set(&ne, "-1"));
try set(&ne, "a");