Skip to content

Commit 75296fb

Browse files
committed
chore: comptime sanity type check
1 parent 4329481 commit 75296fb

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
@@ -81,6 +81,10 @@ pub fn FixedContainerType(comptime ST: type) type {
8181
/// Caller owns the memory.
8282
pub fn clone(value: *const Type, out: anytype) !void {
8383
const OutType = @TypeOf(out.*);
84+
comptime {
85+
const OutInfo = @typeInfo(@TypeOf(out));
86+
std.debug.assert(OutInfo == .pointer);
87+
}
8488
if (OutType == Type) {
8589
out.* = value.*;
8690
} else {
@@ -357,6 +361,11 @@ pub fn VariableContainerType(comptime ST: type) type {
357361
value: *const Type,
358362
out: anytype,
359363
) !void {
364+
comptime {
365+
const OutInfo = @typeInfo(@TypeOf(out));
366+
std.debug.assert(OutInfo == .pointer);
367+
}
368+
360369
inline for (fields) |field| {
361370
if (comptime isFixedType(field.type)) {
362371
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
@@ -77,6 +77,11 @@ pub fn FixedListType(comptime ST: type, comptime _limit: comptime_int) type {
7777
///
7878
/// Caller owns the memory.
7979
pub fn clone(allocator: std.mem.Allocator, value: *const Type, out: anytype) !void {
80+
comptime {
81+
const OutInfo = @typeInfo(@TypeOf(out));
82+
std.debug.assert(OutInfo == .pointer);
83+
}
84+
8085
try out.resize(allocator, value.items.len);
8186

8287
for (value.items, 0..) |v, i| {
@@ -327,6 +332,11 @@ pub fn VariableListType(comptime ST: type, comptime _limit: comptime_int) type {
327332
/// Clones the underlying `ArrayList`.
328333
/// Caller owns the memory.
329334
pub fn clone(allocator: std.mem.Allocator, value: *const Type, out: anytype) !void {
335+
comptime {
336+
const OutInfo = @typeInfo(@TypeOf(out));
337+
std.debug.assert(OutInfo == .pointer);
338+
}
339+
330340
try out.resize(allocator, value.items.len);
331341
for (0..value.items.len) |i|
332342
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
@@ -57,6 +57,12 @@ pub fn FixedVectorType(comptime ST: type, comptime _length: comptime_int) type {
5757
}
5858

5959
pub fn clone(value: *const Type, out: anytype) !void {
60+
comptime {
61+
const OutInfo = @typeInfo(@TypeOf(out.*));
62+
std.debug.assert(OutInfo == .array);
63+
std.debug.assert(OutInfo.array.len == length);
64+
}
65+
6066
const OutType = @TypeOf(out.*);
6167
if (OutType == Type) {
6268
out.* = value.*;
@@ -238,6 +244,12 @@ pub fn VariableVectorType(comptime ST: type, comptime _length: comptime_int) typ
238244
}
239245

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

0 commit comments

Comments
 (0)