-
Notifications
You must be signed in to change notification settings - Fork 3
/
borsh.zig
317 lines (291 loc) · 10.6 KB
/
borsh.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
const std = @import("std");
const testing = std.testing;
const borsh = @This();
/// An optional type whose enum tag is 32 bits wide.
pub fn Option(comptime T: type) type {
return union(enum(u32)) {
none: void,
some: T,
pub fn from(inner: ?T) @This() {
if (inner) |payload| {
return .{ .some = payload };
}
return .none;
}
pub fn into(self: @This()) ?T {
return switch (self) {
.some => |payload| payload,
.none => null,
};
}
pub fn format(self: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
return switch (self) {
.none => writer.writeAll("null"),
.some => |payload| writer.print("{any}", .{payload}),
};
}
};
}
pub fn sizeOf(data: anytype) usize {
var stream = std.io.countingWriter(std.io.null_writer);
borsh.write(stream.writer(), data) catch unreachable;
return @as(usize, @intCast(stream.bytes_written));
}
pub fn readFromSlice(gpa: std.mem.Allocator, comptime T: type, slice: []const u8) !T {
var stream = std.io.fixedBufferStream(slice);
return borsh.read(gpa, T, stream.reader());
}
pub fn writeToSlice(slice: []u8, data: anytype) ![]u8 {
var stream = std.io.fixedBufferStream(slice);
try borsh.write(stream.writer(), data);
return stream.getWritten();
}
pub inline fn writeAlloc(gpa: std.mem.Allocator, data: anytype) ![]u8 {
const buffer = try gpa.alloc(u8, borsh.sizeOf(data));
errdefer gpa.free(buffer);
return try borsh.writeToSlice(buffer, data);
}
pub fn read(gpa: std.mem.Allocator, comptime T: type, reader: anytype) !T {
switch (@typeInfo(T)) {
.Void => return {},
.Bool => return switch (try reader.readByte()) {
0 => false,
1 => true,
else => error.BadBoolean,
},
.Enum => |info| {
const tag = try borsh.read(gpa, u8, reader);
return std.meta.intToEnum(T, @as(info.tag_type, @intCast(tag)));
},
.Union => |info| {
const tag_type = info.tag_type orelse @compileError("Only tagged unions may be read.");
const raw_tag = try borsh.read(gpa, tag_type, reader);
inline for (info.fields) |field| {
if (raw_tag == @field(tag_type, field.name)) {
// https://github.com/ziglang/zig/issues/7866
if (field.type == void) return @unionInit(T, field.name, {});
const payload = try borsh.read(gpa, field.type, reader);
return @unionInit(T, field.name, payload);
}
}
return error.UnknownUnionTag;
},
.Struct => |info| {
var data: T = undefined;
inline for (info.fields) |field| {
if (!field.is_comptime) {
@field(data, field.name) = try borsh.read(gpa, field.type, reader);
}
}
return data;
},
.Optional => |info| {
return switch (try reader.readByte()) {
0 => null,
1 => try borsh.read(gpa, info.child, reader),
else => error.BadOptionalBoolean,
};
},
.Array => |info| {
var data: T = undefined;
for (&data) |*element| {
element.* = try borsh.read(gpa, info.child, reader);
}
return data;
},
.Vector => |info| {
var data: T = undefined;
for (&data) |*element| {
element.* = try borsh.read(gpa, info.child, reader);
}
return data;
},
.Pointer => |info| {
switch (info.size) {
.One => {
const data = try gpa.create(info.child);
errdefer gpa.destroy(data);
data.* = try borsh.read(gpa, info.child, reader);
return data;
},
.Slice => {
const entries = try gpa.alloc(info.child, try borsh.read(gpa, u32, reader));
errdefer gpa.free(entries);
for (entries) |*entry| {
entry.* = try borsh.read(gpa, info.child, reader);
}
return entries;
},
else => {},
}
},
.ComptimeFloat => return borsh.read(gpa, f64, reader),
.Float => |info| {
const data = @as(T, @bitCast(try reader.readBytesNoEof((info.bits + 7) / 8)));
if (std.math.isNan(data)) {
return error.FloatIsNan;
}
return data;
},
.ComptimeInt => return borsh.read(gpa, u64, reader),
.Int => return reader.readIntLittle(T),
else => {},
}
@compileError("Deserializing '" ++ @typeName(T) ++ "' is unsupported.");
}
pub fn readFree(gpa: std.mem.Allocator, value: anytype) void {
const T = @TypeOf(value);
switch (@typeInfo(T)) {
.Array, .Vector => {
for (value) |element| {
borsh.readFree(gpa, element);
}
},
.Struct => |info| {
inline for (info.fields) |field| {
if (!field.is_comptime) {
borsh.readFree(gpa, @field(value, field.name));
}
}
},
.Optional => {
if (value) |v| {
borsh.readFree(gpa, v);
}
},
.Union => |info| {
inline for (info.fields) |field| {
if (value == @field(T, field.name)) {
return borsh.readFree(gpa, @field(value, field.name));
}
}
},
.Pointer => |info| {
switch (info.size) {
.One => gpa.destroy(value),
.Slice => {
for (value) |item| {
borsh.readFree(gpa, item);
}
gpa.free(value);
},
else => {},
}
},
else => {},
}
}
pub fn write(writer: anytype, data: anytype) !void {
const T = @TypeOf(data);
switch (@typeInfo(T)) {
.Type, .Void, .NoReturn, .Undefined, .Null, .Fn, .Opaque, .Frame, .AnyFrame => return,
.Bool => return writer.writeByte(@intFromBool(data)),
.Enum => return borsh.write(writer, std.math.cast(u8, @intFromEnum(data)) orelse return error.EnumTooLarge),
.Union => |info| {
try borsh.write(writer, std.math.cast(u8, @intFromEnum(data)) orelse return error.EnumTooLarge);
inline for (info.fields) |field| {
if (data == @field(T, field.name)) {
return borsh.write(writer, @field(data, field.name));
}
}
return;
},
.Struct => |info| {
var maybe_err: anyerror!void = {};
inline for (info.fields) |field| {
if (!field.is_comptime) {
if (@as(?anyerror!void, maybe_err catch null) != null) {
maybe_err = borsh.write(writer, @field(data, field.name));
}
}
}
return maybe_err;
},
.Optional => {
if (data) |value| {
try writer.writeByte(1);
try borsh.write(writer, value);
} else {
try writer.writeByte(0);
}
return;
},
.Array, .Vector => {
for (data) |element| {
try borsh.write(writer, element);
}
return;
},
.Pointer => |info| {
switch (info.size) {
.One => return borsh.write(writer, data.*),
.Many => return borsh.write(writer, std.mem.span(data)),
.Slice => {
try borsh.write(writer, std.math.cast(u32, data.len) orelse return error.DataTooLarge);
for (data) |element| {
try borsh.write(writer, element);
}
return;
},
else => {},
}
},
.ComptimeFloat => return borsh.write(writer, @as(f64, data)),
.Float => {
if (std.math.isNan(data)) {
return error.FloatsMayNotBeNan;
}
return writer.writeAll(std.mem.asBytes(&data));
},
.ComptimeInt => {
if (data < 0) {
@compileError("Signed comptime integers can not be serialized.");
}
return borsh.write(writer, @as(u64, data));
},
.Int => return writer.writeIntLittle(T, data),
else => {},
}
@compileError("Serializing '" ++ @typeName(T) ++ "' is unsupported.");
}
test "borsh: serialize and deserialize" {
var buffer = std.ArrayList(u8).init(testing.allocator);
defer buffer.deinit();
inline for (.{
@as(i8, std.math.minInt(i8)),
@as(i16, std.math.minInt(i16)),
@as(i32, std.math.minInt(i32)),
@as(i64, std.math.minInt(i64)),
@as(i8, std.math.maxInt(i8)),
@as(i16, std.math.maxInt(i16)),
@as(i32, std.math.maxInt(i32)),
@as(i64, std.math.maxInt(i64)),
@as(u8, std.math.maxInt(u8)),
@as(u16, std.math.maxInt(u16)),
@as(u32, std.math.maxInt(u32)),
@as(u64, std.math.maxInt(u64)),
@as(f32, std.math.floatMin(f32)),
@as(f64, std.math.floatMin(f64)),
@as(f32, std.math.floatMax(f32)),
@as(f64, std.math.floatMax(f64)),
[_]u8{ 0, 1, 2, 3 },
}) |expected| {
try borsh.write(buffer.writer(), expected);
var stream = std.io.fixedBufferStream(buffer.items);
const actual = try borsh.read(testing.allocator, @TypeOf(expected), stream.reader());
defer borsh.readFree(testing.allocator, actual);
try testing.expectEqual(expected, actual);
buffer.clearRetainingCapacity();
}
inline for (.{
"hello world",
@as([]const u8, "hello world"),
}) |expected| {
try borsh.write(buffer.writer(), expected);
var stream = std.io.fixedBufferStream(buffer.items);
const actual = try borsh.read(testing.allocator, @TypeOf(expected), stream.reader());
defer borsh.readFree(testing.allocator, actual);
try testing.expectEqualSlices(std.meta.Elem(@TypeOf(expected)), expected, actual);
buffer.clearRetainingCapacity();
}
}