-
Notifications
You must be signed in to change notification settings - Fork 1
/
taggedalgebraic_example.d
97 lines (79 loc) · 2.76 KB
/
taggedalgebraic_example.d
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
/+ dub.sdl:
dependency "taggedalgebraic" version="~>0.11.18"
dependency "sbin" path=".."
+/
import taggedalgebraic;
import sbin;
struct Foo { string name; }
union Base
{
int count;
string str;
Foo foo;
}
alias TUnion = TaggedUnion!Base;
struct Bar
{
int someInt;
TUnion[] data;
}
void barTest()
{
auto bar = Bar(4, [TUnion.count(42), TUnion.str("Hello"), TUnion.foo(Foo("ABC"))]);
auto sdbar_data = bar.sbinSerialize;
assert (sdbar_data.length == int.sizeof + 1 /+ length packed to 1 byte +/ +
byte.sizeof + int.sizeof + // count
byte.sizeof + 1 /+ length packed to 1 byte +/ + 5 + // str
byte.sizeof + 1 /+ length packed to 1 byte +/ + 3 // Foo
);
assert (sdbar_data == [4, 0, 0, 0, 3, 0, 42, 0, 0, 0, 1, 5, 72, 101, 108, 108, 111, 2, 3, 65, 66, 67]);
auto sdbar = sdbar_data.sbinDeserialize!Bar;
assert (sdbar.someInt == 4);
assert (sdbar.data.length == 3);
assert (sdbar.data[0].isCount);
assert (sdbar.data[0].kind == TUnion.Kind.count);
assert (sdbar.data[0].value!(TUnion.Kind.count) == 42);
// deserialize to new memory
assert (bar.data[1].value!(TUnion.Kind.str).ptr !=
sdbar.data[1].value!(TUnion.Kind.str).ptr);
assert (sdbar.data[1].isStr);
assert (sdbar.data[1].kind == TUnion.Kind.str);
assert (sdbar.data[1].value!(TUnion.Kind.str) == "Hello");
assert (sdbar.data[2].isFoo);
assert (sdbar.data[2].kind == TUnion.Kind.foo);
assert (sdbar.data[2].value!(TUnion.Kind.foo) == Foo("ABC"));
assert (sdbar.data[2].value!(TUnion.Kind.foo).name.ptr !=
bar.data[2].value!(TUnion.Kind.foo).name.ptr);
}
alias TAlg = TaggedAlgebraic!Base;
struct Bar2 { TAlg[] data; }
void bar2Test()
{
TAlg t_count = 42;
TAlg t_str = "Hello";
TAlg t_foo = Foo("ABC");
auto bar = Bar2([t_count, t_str, t_foo]);
auto sdbar_data = bar.sbinSerialize;
assert (sdbar_data.length == 1 /+ length packed to 1 byte +/ +
byte.sizeof + int.sizeof + // count
byte.sizeof + 1 /+ length packed to 1 byte +/ + 5 + // str
byte.sizeof + 1 /+ length packed to 1 byte +/ + 3 // Foo
);
assert (sdbar_data == [3, 0, 42, 0, 0, 0, 1, 5, 72, 101, 108, 108, 111, 2, 3, 65, 66, 67]);
auto sdbar = sdbar_data.sbinDeserialize!Bar2;
assert (sdbar.data.length == 3);
assert (sdbar.data[0].kind == TAlg.Kind.count);
assert (sdbar.data[0] == 42);
// deserialize to new memory
assert (bar.data[1].ptr != sdbar.data[1].ptr);
assert (sdbar.data[1].kind == TAlg.Kind.str);
assert (sdbar.data[1] == "Hello");
assert (sdbar.data[2].kind == TAlg.Kind.foo);
assert (sdbar.data[2] == Foo("ABC"));
assert (sdbar.data[2].name.ptr != bar.data[2].name.ptr);
}
void main()
{
barTest();
bar2Test();
}