Skip to content

Commit 979862a

Browse files
committed
chore: comptime sanity type check
1 parent 83d6ab8 commit 979862a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/ssz/type/container.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ pub fn FixedContainerType(comptime ST: type) type {
8383
/// Caller owns the memory.
8484
pub fn clone(value: *const Type, out: anytype) !void {
8585
const OutType = @TypeOf(out.*);
86+
comptime {
87+
const OutInfo = @typeInfo(@TypeOf(out));
88+
std.debug.assert(OutInfo == .pointer);
89+
}
8690
if (OutType == Type) {
8791
out.* = value.*;
8892
} else {
@@ -360,6 +364,11 @@ pub fn VariableContainerType(comptime ST: type) type {
360364
value: *const Type,
361365
out: anytype,
362366
) !void {
367+
comptime {
368+
const OutInfo = @typeInfo(@TypeOf(out));
369+
std.debug.assert(OutInfo == .pointer);
370+
}
371+
363372
inline for (fields) |field| {
364373
if (comptime isFixedType(field.type)) {
365374
try field.type.clone(&@field(value, field.name), &@field(out, field.name));

src/ssz/type/list.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ pub fn FixedListType(comptime ST: type, comptime _limit: comptime_int) type {
7979
///
8080
/// Caller owns the memory.
8181
pub fn clone(allocator: std.mem.Allocator, value: *const Type, out: anytype) !void {
82+
comptime {
83+
const OutInfo = @typeInfo(@TypeOf(out));
84+
std.debug.assert(OutInfo == .pointer);
85+
}
86+
8287
try out.resize(allocator, value.items.len);
8388

8489
for (value.items, 0..) |v, i| {
@@ -329,6 +334,11 @@ pub fn VariableListType(comptime ST: type, comptime _limit: comptime_int) type {
329334
/// Clones the underlying `ArrayList`.
330335
/// Caller owns the memory.
331336
pub fn clone(allocator: std.mem.Allocator, value: *const Type, out: anytype) !void {
337+
comptime {
338+
const OutInfo = @typeInfo(@TypeOf(out));
339+
std.debug.assert(OutInfo == .pointer);
340+
}
341+
332342
try out.resize(allocator, value.items.len);
333343
for (0..value.items.len) |i|
334344
try Element.clone(allocator, &value.items[i], &out.items[i]);

src/ssz/type/vector.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ pub fn FixedVectorType(comptime ST: type, comptime _length: comptime_int) type {
5959
}
6060

6161
pub fn clone(value: *const Type, out: anytype) !void {
62+
comptime {
63+
const OutInfo = @typeInfo(@TypeOf(out.*));
64+
std.debug.assert(OutInfo == .array);
65+
std.debug.assert(OutInfo.array.len == length);
66+
}
67+
6268
const OutType = @TypeOf(out.*);
6369
if (OutType == Type) {
6470
out.* = value.*;
@@ -240,6 +246,12 @@ pub fn VariableVectorType(comptime ST: type, comptime _length: comptime_int) typ
240246
}
241247

242248
pub fn clone(allocator: std.mem.Allocator, value: *const Type, out: anytype) !void {
249+
comptime {
250+
const OutInfo = @typeInfo(@TypeOf(out.*));
251+
std.debug.assert(OutInfo == .array);
252+
std.debug.assert(OutInfo.array.len == length);
253+
}
254+
243255
for (value, 0..) |*element, i| {
244256
try Element.clone(allocator, element, &out[i]);
245257
}

0 commit comments

Comments
 (0)