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
|
pub fn isArrayListUnmanaged(comptime T: type) bool {
if (@typeInfo(T) != .@"struct" or !@hasDecl(T, "Slice"))
return false;
const ptr_info = switch (@typeInfo(T.Slice)) {
.pointer => |info| info,
else => return false,
};
return T == std.ArrayListAlignedUnmanaged(ptr_info.child, null) or
T == std.ArrayListAlignedUnmanaged(ptr_info.child, ptr_info.alignment);
}
test isArrayListUnmanaged {
try std.testing.expect(!isArrayListUnmanaged(u8));
try std.testing.expect(!isArrayListUnmanaged([]const u8));
try std.testing.expect(!isArrayListUnmanaged(struct {
pub const Slice = []const u8;
}));
try std.testing.expect(isArrayListUnmanaged(std.ArrayListUnmanaged(u8)));
}
pub fn allFieldsHaveDefaults(comptime T: type) bool {
const info = switch (@typeInfo(T)) {
.@"struct" => |s| s,
else => return false,
};
inline for (info.fields) |field| {
if (field.default_value == null)
return false;
}
return true;
}
test allFieldsHaveDefaults {
try std.testing.expect(!allFieldsHaveDefaults(u8));
try std.testing.expect(!allFieldsHaveDefaults([]const u8));
try std.testing.expect(allFieldsHaveDefaults(struct {}));
try std.testing.expect(allFieldsHaveDefaults(struct {
a: u8 = 0,
}));
try std.testing.expect(!allFieldsHaveDefaults(struct {
a: u8,
}));
try std.testing.expect(!allFieldsHaveDefaults(struct {
a: u8,
b: u8 = 0,
c: u8,
}));
try std.testing.expect(allFieldsHaveDefaults(struct {
a: u8 = 1,
b: u8 = 0,
c: u8 = 3,
}));
}
const std = @import("std");
|