summaryrefslogtreecommitdiff
path: root/src/walk.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 15:57:57 -0300
committerGabriel Schneider <[email protected]>2026-10-30 16:29:54 -0300
commitf7a0b6d902f001cff0343e11263868c5356f33c6 (patch)
tree5d063c3f6a7d08a360b7bd3f7955221414101299 /src/walk.zig
parent5b120f481a5e581d5252c41a4ddcc3e28c8a07f3 (diff)
downloadpardes-f7a0b6d902f001cff0343e11263868c5356f33c6.tar.gz
pardes-f7a0b6d902f001cff0343e11263868c5356f33c6.zip
tape: an error crosses by its name, a stat as fs.OsStat, and the tape's shape is in wire.semantics: a replica of another build read its session's units as its own build's, so a window client of a terminal's session, or of the box's, read every error by a number its build gives another (451 of 505 names numbered otherwise in the Linux terminal's and window's builds, 244 between Linux and FreeBSD: pardes-gui replaying a terminal's tape panicked at `unexpected error code, found error.NonLocalPath`, and so did main's window client of a session on the box at its Look of a fifo) and a FreeBSD stat out of step (nlink 8 bytes there, 4 here); a name this build's set lacks is a divergence (error.NoSuchError), never another error; an anyerror (a host's report) reads back as one of tape.Failure's, a Debug build panicking at one outside it as it is taped; tape.shape (each taped type's fields, tags, enum names and numbers) in semantics refuses, in words, builds whose tapes differ (the stat taped as before: the Linux and FreeBSD builds hash to two), and a site whose result tape.results does not list is a compile error; walk's test of an error by its name
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/walk.zig')
-rw-r--r--src/walk.zig132
1 files changed, 123 insertions, 9 deletions
diff --git a/src/walk.zig b/src/walk.zig
index 1c5bbbfc..88f62736 100644
--- a/src/walk.zig
+++ b/src/walk.zig
@@ -388,9 +388,14 @@ fn hashMap(comptime T: type, v: *const T, h: *Hasher) void {
h.update(std.mem.asBytes(&sum));
}
-/// The value as bytes, the same walk: what the tape carries (src/tape.zig).
-/// Only for the process that wrote it, or one of the same build: native
-/// byte order, and an error as its number in this build.
+/// The value as bytes, the same walk: what the tape carries (src/tape.zig),
+/// to a replica of another build too (a window of a terminal's session, a
+/// Linux client of a FreeBSD one). Native byte order, which every machine
+/// pardes runs on shares, and an error by its name: its number is its
+/// build's own (a Linux terminal's FileNotFound is a window's
+/// NoDefaultValue). What a field is laid out as (a std.Io.File.Stat's
+/// `nlink` is another width on FreeBSD) is the tape's shape, which
+/// wire.semantics hashes (`shape`).
pub fn encode(comptime T: type, v: *const T, w: *std.Io.Writer) std.Io.Writer.Error!void {
switch (@typeInfo(T)) {
.void, .null, .undefined => {},
@@ -424,18 +429,91 @@ pub fn encode(comptime T: type, v: *const T, w: *std.Io.Writer) std.Io.Writer.Er
inline else => |*payload| try encode(@TypeOf(payload.*), payload, w),
}
},
- .error_set => try w.writeAll(std.mem.asBytes(&@as(u16, @intFromError(v.*)))),
+ .error_set => try encodeError(T, v.*, w),
.error_union => |e| if (v.*) |*payload| {
try w.writeByte(1);
try encode(e.payload, payload, w);
} else |err| {
try w.writeByte(0);
- try w.writeAll(std.mem.asBytes(&@as(u16, @intFromError(err))));
+ try encodeError(e.error_set, err, w);
},
else => @compileError("walk: cannot encode " ++ @typeName(T)),
}
}
+/// What `encode` lays each of `types` out as, in words: each field, element
+/// and tag where it goes, an enum's names and numbers, an error as an
+/// error (it goes by its name). wire.semantics hashes the tape's: builds
+/// whose tapes differ in shape, as a FreeBSD std.Io.File.Stat differs from
+/// a Linux one, refuse each other's replicas in words, not read each other
+/// out of step.
+pub fn shape(comptime types: []const type) []const u8 {
+ comptime {
+ @setEvalBranchQuota(10_000_000);
+ var out: []const u8 = "";
+ for (types) |T| out = out ++ shapeOf(T, 0) ++ "\n";
+ return out;
+ }
+}
+
+fn shapeOf(comptime T: type, comptime depth: u8) []const u8 {
+ comptime {
+ if (depth > 32) @compileError("walk: the tape's shape nests past 32 deep at " ++ @typeName(T) ++ ": a type that holds itself?");
+ return switch (@typeInfo(T)) {
+ .void, .null, .undefined => "v",
+ .bool => "b",
+ .int => |i| std.fmt.comptimePrint("{t}{d}", .{ i.signedness, i.bits }),
+ .float => |f| std.fmt.comptimePrint("f{d}", .{f.bits}),
+ .@"enum" => |e| blk: {
+ var out: []const u8 = shapeOf(e.tag_type, depth + 1) ++ "{";
+ for (e.field_names, e.field_values) |n, v| out = out ++ std.fmt.comptimePrint("{s}={d},", .{ n, v });
+ break :blk out ++ "}";
+ },
+ .optional => |o| if (machinery(o.child)) "0" else "?" ++ shapeOf(o.child, depth + 1),
+ .array => |a| std.fmt.comptimePrint("[{d}]", .{a.len}) ++ shapeOf(a.child, depth + 1),
+ .pointer => |p| switch (p.size) {
+ .one => shapeOf(p.child, depth + 1),
+ .slice => "[]" ++ shapeOf(p.child, depth + 1),
+ else => @compileError("walk: a many-pointer has no length: " ++ @typeName(T)),
+ },
+ .@"struct" => |s| blk: {
+ if (machinery(T)) break :blk "";
+ var out: []const u8 = if (s.layout == .@"packed") "p{" else "{";
+ for (s.field_names, s.field_types, s.field_attrs) |name, F, attr| {
+ if (attr.@"comptime" or excluded(T, name)) continue;
+ out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ",";
+ }
+ break :blk out ++ "}";
+ },
+ .@"union" => |u| blk: {
+ var out: []const u8 = shapeOf(u.tag_type.?, depth + 1) ++ "(";
+ for (u.field_names, u.field_types) |name, F| out = out ++ name ++ ":" ++ shapeOf(F, depth + 1) ++ ",";
+ break :blk out ++ ")";
+ },
+ .error_set => "error",
+ .error_union => |e| "!" ++ shapeOf(e.payload, depth + 1),
+ else => @compileError("walk: cannot encode " ++ @typeName(T)),
+ };
+ }
+}
+
+/// An error by its name. An `anyerror` must be one `decode` can read back
+/// (`errorNamed`): a Debug build stops at one that is not, as it is taped.
+fn encodeError(comptime E: type, err: E, w: *std.Io.Writer) std.Io.Writer.Error!void {
+ if (comptime E == anyerror and builtin.mode == .debug) if (errorNamed(E, @errorName(err)) == null)
+ std.debug.panic("walk: error.{s} is taped as an anyerror, which a replica reads back only as one of tape.Failure's: add the set it comes from", .{@errorName(err)});
+ try writeName(w, @errorName(err));
+}
+
+/// The error of `E` called `name`. `anyerror` keeps no list of its names
+/// to look one up in: one is read back as one of tape.Failure's, what a
+/// host reports as failed (the tape's only anyerrors).
+fn errorNamed(comptime E: type, name: []const u8) ?E {
+ const Named = if (E == anyerror) @import("tape.zig").Failure else E;
+ inline for (@typeInfo(Named).error_set.error_names.?) |n| if (std.mem.eql(u8, name, n)) return @field(Named, n);
+ return null;
+}
+
pub const Reader = struct {
bytes: []const u8,
at: usize = 0,
@@ -451,9 +529,13 @@ pub const Reader = struct {
}
};
+/// What an error read back is when this build has none of that name (an
+/// OS's own, a newer build's): the tape is another build's, and diverges.
+pub const TapeError = error{ EndOfTape, OutOfMemory, NoSuchError };
+
/// The inverse of `encode`; slices (and what a pointer points at) are made
/// in `a`. A field `encode` left out is its default.
-pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTape, OutOfMemory }!T {
+pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) TapeError!T {
switch (@typeInfo(T)) {
.void, .null, .undefined => return {},
.bool => return (try r.take(1))[0] != 0,
@@ -500,7 +582,7 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTa
@field(out, name) = @as(*const F, @ptrCast(@alignCast(default))).*;
} else if (comptime @typeInfo(F) == .error_set) {
// No `!anyerror` to return it through: read in place.
- @field(out, name) = @errorCast(@errorFromInt(try r.scalar(u16)));
+ try decodeError(F, r, &@field(out, name));
} else @field(out, name) = try decode(F, r, a);
}
return out;
@@ -512,17 +594,24 @@ pub fn decode(comptime T: type, r: *Reader, a: std.mem.Allocator) error{ EndOfTa
};
unreachable;
},
- .error_set => return @errorCast(@errorFromInt(try r.scalar(u16))),
.error_union => |e| {
if ((try r.take(1))[0] != 0) return try decode(e.payload, r, a);
// The value's error, not decode's own.
- const err: e.error_set = @errorCast(@errorFromInt(try r.scalar(u16)));
+ var err: e.error_set = undefined;
+ try decodeError(e.error_set, r, &err);
return @as(T, err);
},
else => @compileError("walk: cannot decode " ++ @typeName(T)),
}
}
+/// An error by its name, as `encode` wrote it, into `out`: no error union
+/// holds an error set.
+fn decodeError(comptime E: type, r: *Reader, out: *E) TapeError!void {
+ const len = try r.scalar(u16);
+ out.* = errorNamed(E, try r.take(len)) orelse return error.NoSuchError;
+}
+
test "a value comes back from its bytes" {
const E = union(enum) { key: struct { cp: u21, text: []const u8 }, tick, failed: anyerror!u32 };
var buf: [256]u8 = undefined;
@@ -536,6 +625,31 @@ test "a value comes back from its bytes" {
try std.testing.expectEqual(r.bytes.len, r.at);
}
+test "an error is taped by its name, which another build reads back as its own" {
+ // The build's number for an error is its own: 451 of 505 names were
+ // numbered otherwise in a Linux window's build than a terminal's, so a
+ // window replica read a terminal session's FileNotFound as another.
+ var buf: [64]u8 = undefined;
+ var w: std.Io.Writer = .fixed(&buf);
+ const failed: error{ FileNotFound, NotDir }!u32 = error.NotDir;
+ try encode(@TypeOf(failed), &failed, &w);
+ try std.testing.expectEqualSlices(u8, "\x00\x06\x00NotDir", w.buffered());
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ var r: Reader = .{ .bytes = w.buffered() };
+ try std.testing.expectError(error.NotDir, try decode(@TypeOf(failed), &r, arena.allocator()));
+ // A name this build's set lacks (an OS's own): the tape is another
+ // build's, and the replay diverges rather than read some other error.
+ r = .{ .bytes = "\x00\x0c\x00FileTooLarge" };
+ try std.testing.expectError(error.NoSuchError, decode(@TypeOf(failed), &r, arena.allocator()));
+ // An anyerror (a host's report) is read back as one of tape.Failure's.
+ const Report = struct { err: anyerror };
+ w = .fixed(&buf);
+ try encode(Report, &.{ .err = error.FileNotFound }, &w);
+ r = .{ .bytes = w.buffered() };
+ try std.testing.expectEqual(error.FileNotFound, (try decode(Report, &r, arena.allocator())).err);
+}
+
test "a digest is of values: the same text at two addresses is one digest" {
const S = struct { name: []const u8, n: u32, next: ?*const u32 };
const a = "same text";