summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/walk.zig44
1 files changed, 38 insertions, 6 deletions
diff --git a/src/walk.zig b/src/walk.zig
index 6f9cc34b..5060de3d 100644
--- a/src/walk.zig
+++ b/src/walk.zig
@@ -508,10 +508,36 @@ fn dumpFields(comptime T: type) []const [:0]const u8 {
/// The type a dump walks field `name` of `T` as.
fn FieldDump(comptime T: type, comptime name: []const u8) type {
const F = @FieldType(T, name);
- if (comptime lengthOf(T, name) != null) return Prefix(Canon(@typeInfo(F).array.child));
+ if (comptime cutArray(T, name)) return Prefix(Canon(@typeInfo(F).array.child));
return Canon(F);
}
+/// A fixed array a `len` cuts, which a dump writes as far as it says. A
+/// slice it cuts (a text's room for its ranges, made whole) is written
+/// whole, room and all, which only plain numbers may be: a load gives it
+/// the room it had.
+fn cutArray(comptime T: type, comptime name: []const u8) bool {
+ const F = @FieldType(T, name);
+ if (comptime lengthOf(T, name) == null) return false;
+ if (@typeInfo(F) == .array) return true;
+ if (!plain(Element(F))) @compileError("walk: " ++ @typeName(T) ++ "." ++ name ++ " is a slice a `len` cuts, of what a dump cannot write past the cut");
+ return false;
+}
+
+/// Every bit pattern one of its values: what a dump may write where
+/// nothing was ever written.
+fn plain(comptime T: type) bool {
+ return switch (@typeInfo(T)) {
+ .int, .float => true,
+ .array => |a| plain(a.child),
+ .@"struct" => |st| st.layout != .@"packed" and blk: {
+ for (st.field_types) |F| if (!plain(F)) break :blk false;
+ break :blk true;
+ },
+ else => false,
+ };
+}
+
fn children(comptime T: type) []const type {
comptime {
return switch (kindOf(T)) {
@@ -774,9 +800,9 @@ pub fn Dump(comptime roots: []const type) type {
const packed_struct = comptime @typeInfo(T).@"struct".layout == .@"packed";
inline for (comptime dumpFields(T)) |name| {
const F = @FieldType(T, name);
- if (comptime lengthOf(T, name)) |len| {
+ if (comptime cutArray(T, name)) {
const all = &@field(v, name);
- try d.items(@typeInfo(F).array.child, all[0..@min(all.len, lengthIn(v, len))]);
+ try d.items(@typeInfo(F).array.child, all[0..@min(all.len, lengthIn(v, comptime lengthOf(T, name).?))]);
} else if (packed_struct) {
const copy: F = @field(v, name);
try d.value(F, &copy);
@@ -1356,7 +1382,7 @@ pub fn Load(comptime roots: []const type) type {
if (comptime allocOf(T, field)) |which| if (@field(l.allocs, which)) |a| {
l.gpa = a;
};
- if (comptime lengthOf(T, field) != null) {
+ if (comptime cutArray(T, field)) {
return l.value(F, ty, &@field(out, field));
}
// A struct held in place in one read as it stands (the root's):
@@ -1454,17 +1480,20 @@ test "a dump comes back whole: shared things shared, lists and runs as long as t
either: union(enum) { none, some: u32, many: []const u16 } = .none,
ring: [4]u8 = @splat(0),
ring_len: u8 = 0,
+ room: []i32 = &.{},
+ room_len: u8 = 0,
cache: u64 = 7,
alloc: std.mem.Allocator,
flags: packed struct { x: bool = false, y: u3 = 0 } = .{},
- pub const replica = .{ .derived = .{"cache"}, .len = .{.{ "ring", "ring_len" }} };
+ pub const replica = .{ .derived = .{"cache"}, .len = .{ .{ "ring", "ring_len" }, .{ "room", "room_len" } } };
};
var leaf: Leaf = .{ .n = 0x1fffff, .name = "leaf" };
+ var room = [_]i32{ 4, 5, -1, -1 };
var text = "shared text".*;
var list: std.ArrayList(u16) = .empty;
defer list.deinit(gpa);
try list.appendSlice(gpa, &.{ 1, 2, 3 });
- const x: S = .{ .mode = .c, .leaf = &leaf, .same_leaf = &leaf, .text = &text, .same_text = &text, .list = list, .maybe = .{ .n = 3, .name = "maybe" }, .either = .{ .many = &.{ 9, 8 } }, .ring = .{ 5, 6, 0xAA, 0xAA }, .ring_len = 2, .cache = 99, .alloc = gpa, .flags = .{ .x = true, .y = 5 } };
+ const x: S = .{ .mode = .c, .leaf = &leaf, .same_leaf = &leaf, .text = &text, .same_text = &text, .list = list, .maybe = .{ .n = 3, .name = "maybe" }, .either = .{ .many = &.{ 9, 8 } }, .ring = .{ 5, 6, 0xAA, 0xAA }, .ring_len = 2, .room = &room, .room_len = 2, .cache = 99, .alloc = gpa, .flags = .{ .x = true, .y = 5 } };
const bytes = try save(S, gpa, &x);
defer gpa.free(bytes);
var arena: std.heap.ArenaAllocator = .init(gpa);
@@ -1475,6 +1504,9 @@ test "a dump comes back whole: shared things shared, lists and runs as long as t
try std.testing.expectEqual(@as(u64, 7), y.cache);
try std.testing.expectEqualSlices(u16, &.{ 1, 2, 3 }, y.list.items);
try std.testing.expectEqual(true, y.flags.x);
+ // A slice its `len` cuts keeps its room.
+ try std.testing.expectEqual(@as(usize, 4), y.room.len);
+ try std.testing.expectEqualSlices(i32, &.{ 4, 5 }, y.room[0..2]);
// A fixpoint: what was read is written as it was.
const again = try save(S, gpa, &y);
defer gpa.free(again);