Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions s2s.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,25 @@ fn serializeRecursive(stream: anytype, comptime T: type, value: T) @TypeOf(strea
.One => try serializeRecursive(stream, ptr.child, value.*),
.Slice => {
try stream.writeIntLittle(u64, value.len);
for (value) |item| {
try serializeRecursive(stream, ptr.child, item);
if (ptr.child == u8) {
try stream.writeAll(value);
} else {
for (value) |item| {
try serializeRecursive(stream, ptr.child, item);
}
}
},
.C => unreachable,
.Many => unreachable,
}
},
.Array => |arr| {
for (value) |item| {
try serializeRecursive(stream, arr.child, item);
if (arr.child == u8) {
try stream.writeAll(&value);
} else {
for (value) |item| {
try serializeRecursive(stream, arr.child, item);
}
}
if (arr.sentinel != null) @compileError("Sentinels are not supported yet!");
},
Expand Down Expand Up @@ -221,8 +229,12 @@ fn recursiveDeserialize(stream: anytype, comptime T: type, allocator: ?std.mem.A
const slice = try allocator.?.alloc(ptr.child, length);
errdefer allocator.?.free(slice);

for (slice) |*item| {
try recursiveDeserialize(stream, ptr.child, allocator, item);
if (ptr.child == u8) {
_ = try stream.readAll(slice);
} else {
for (slice) |*item| {
try recursiveDeserialize(stream, ptr.child, allocator, item);
}
}

target.* = slice;
Expand All @@ -232,8 +244,12 @@ fn recursiveDeserialize(stream: anytype, comptime T: type, allocator: ?std.mem.A
}
},
.Array => |arr| {
for (target.*) |*item| {
try recursiveDeserialize(stream, arr.child, allocator, item);
if (arr.child == u8) {
_ = try stream.readAll(target);
} else {
for (target.*) |*item| {
try recursiveDeserialize(stream, arr.child, allocator, item);
}
}
},
.Struct => |str| {
Expand Down