-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathall_example.d
More file actions
114 lines (91 loc) · 3.13 KB
/
all_example.d
File metadata and controls
114 lines (91 loc) · 3.13 KB
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
/+ dub.sdl:
dependency "mir-core" version="~>1.1"
dependency "sumtype" version="~>1.1"
dependency "taggedalgebraic" version="~>0.11"
dependency "sbin" path=".."
+/
import mir.algebraic;
import sumtype;
import taggedalgebraic;
import sbin;
struct Foo { string name; }
static union U
{
typeof(null) nil;
int count;
string str;
Foo foo;
}
alias MA = Algebraic!U;
static assert (isTagged!(MA).any);
static assert (isTagged!(MA).isMirAlgebraic);
union TABase
{
int count;
string str;
Foo foo;
}
alias TA = TaggedUnion!TABase;
static assert (isTagged!(TA).any);
static assert (isTagged!(TA).isTaggedAlgebraic);
alias ST = SumType!( typeof(null), int, string, Foo );
static assert (isTagged!(ST).any);
static assert (isTagged!(ST).isSumType);
struct Bar
{
int someInt;
MA[] madata;
TA[] tadata;
ST[] stdata;
}
void barTest()
{
auto bar = Bar(123,
[MA(42), MA("one"), MA(Foo("A")), MA(null)],
[TA(43), TA("two"), TA(Foo("B"))],
[ST(44), ST("tre"), ST(Foo("C")), ST(null)],
);
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 +/ + 3 + // str
byte.sizeof + 1 /+ length packed to 1 byte +/ + 1 + // Foo
byte.sizeof + /+ length packed to 1 byte +/ // null
1 /+ length packed to 1 byte +/ +
byte.sizeof + int.sizeof + // count
byte.sizeof + 1 /+ length packed to 1 byte +/ + 3 + // str
byte.sizeof + 1 /+ length packed to 1 byte +/ + 1 + // Foo
1 /+ length packed to 1 byte +/ +
byte.sizeof + int.sizeof + // count
byte.sizeof + 1 /+ length packed to 1 byte +/ + 3 + // str
byte.sizeof + 1 /+ length packed to 1 byte +/ + 1 + // Foo
byte.sizeof /+ length packed to 1 byte +/ // null
);
assert (sdbar_data == [123, 0, 0, 0, 4, 1, 42, 0, 0, 0, 2, 3, 111, 110, 101, 3, 1, 65,
0, 3, 0, 43, 0, 0, 0, 1, 3, 116, 119, 111, 2, 1, 66, 4, 1, 44,
0, 0, 0, 2, 3, 116, 114, 101, 3, 1, 67, 0]);
auto sdbar = sdbar_data.sbinDeserialize!Bar;
assert (sdbar.someInt == 123);
assert (sdbar.madata.length == 4);
assert (sdbar.madata[0].kind == MA.Kind.count);
assert (sdbar.madata[0].get!int == 42);
// deserialize to new memory
assert (bar.madata[1].get!string.ptr !=
sdbar.madata[1].get!string.ptr);
assert (sdbar.madata[1].kind == MA.Kind.str);
assert (sdbar.madata[1].get!string == "one");
assert (sdbar.tadata[1].value!(TA.Kind.str) == "two");
assert (sdbar.madata[2].kind == MA.Kind.foo);
assert (sdbar.madata[2].get!Foo == Foo("A"));
assert (sdbar.madata[2].get!Foo.name.ptr !=
bar.madata[2].get!Foo.name.ptr);
assert (sdbar.tadata[2].value!(TA.Kind.foo) == Foo("B"));
assert (sumtype.match!((Foo v) => v, _ => Foo.init)(sdbar.stdata[2]) == Foo("C"));
assert (sdbar.madata[3].isNull);
assert (sumtype.match!((typeof(null) _) => true, _ => false)(sdbar.stdata[3]));
}
void main()
{
barTest();
}