You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: versioned_docs/version-2.0.0/concepts/serialization/calltable-serialization.md
+18-15Lines changed: 18 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ As mentioned before, the `calltable serialization approach` requires serializing
9
9
-`fields` - field which is an ordered collection of [Field](#field). This collection is serialized as a Vec<Field> in the "regular" `Binary Serialization Standard` schema. The invariants for `fields` are:
10
10
- for each f<sub>i</sub>, f<sub>j</sub> given `i` < `j` implies f<sub>i</sub>.index < f<sub>j</sub>.index
11
11
- for each f<sub>i</sub>, f<sub>j</sub> given `i` < `j` implies f<sub>i</sub>.offset < f<sub>j</sub>.offset
12
-
-`bytes` - field which is an amorphous blob of bytes serialized as `Bytes` in the "regular" `Binary Serialization Standard` schema.
12
+
-`bytes` - field which is an amorphous blob of bytes serialized as `Bytes` in the "regular" `Binary Serialization Standard` schema. Bear in mind that this field will be serialized by appending first a `u32` integer (number of bytes) than the raw bytes payload.
13
13
14
14
A <spanid="field">`Field`</span> consists of:
15
15
@@ -79,6 +79,9 @@ Once we have the `fields` and `bytes` we can proceed to serialize those in the "
This concludes how we construct and byte-serialize an `envelope`. In the next paragraphs we will explain what are the assumptions and conventions when dealing with `struct`s and `enum`s.
110
+
This concludes how we construct and byte-serialize an `envelope`. In the next paragraphs we will explain what are the assumptions and conventions when dealing with `struct`s and `tagged-union`s.
108
111
109
112
## Serializing uniform data structures
110
113
@@ -120,15 +123,15 @@ struct A {
120
123
121
124
In the above example we could assign `a` index `0`, `b` index `1` and `c` index `2`. Knowing this and assuming that we know how to byte-serialize `OtherStruct` we should be able to create an `envelope` for this struct and serialize it in the `calltable` scheme.
122
125
123
-
## Serializing enums
126
+
## Serializing tagged-unions
124
127
125
-
By `enums` we understand polymorphic, but limited (an instance of an enum can be only one of N known `variants`) data structures that are unions of structures and/or other enums. An enum variant can be:
128
+
By `tagged-union` we understand polymorphic, but limited (an instance of an tagged-union can be only one of N known `variants`) data structures that are unions of structures and/or other tagged-unions. An tagged-union variant can be:
126
129
127
130
- empty (tag variant)
128
131
- a struct
129
-
- a nested enum
132
+
- a nested tagged-union
130
133
131
-
As mentioned, there is a polymorphic aspect to these kinds of enums and we handle them by convention - `serialization index``0` is always reserved for a 1 byte discriminator number which defines which enum variant is being serialized, the next indices are used to serialize the fields of specific variants (for empty tag variants there will be no more indices). So, given an enum:
134
+
As mentioned, there is a polymorphic aspect to these kinds of tagged-union and we handle them by convention - `serialization index``0` is always reserved for a 1 byte discriminator number which defines which tagged-union variant is being serialized. The value of this specific pseudo-field will be called `variant discriminator`. Subsequent indices are used to serialize the fields of specific variants (for empty tag variants there will be no more indices). So, given an example tagged-union (implemented in rust, rust equivalent of tagged-union is `enum`):
132
135
133
136
```rust
134
137
enumX {
@@ -138,13 +141,13 @@ As mentioned, there is a polymorphic aspect to these kinds of enums and we handl
138
141
}
139
142
```
140
143
141
-
First we need to chose variant discriminator values for each of the enum variants, let's select:
144
+
First we need to chose variant discriminator values for each of the tagged-union variants, let's select:
142
145
143
146
- if variant `A` - the variant discriminator will be `0`
144
147
- if variant `B` - the variant discriminator will be `1`
145
148
- if variant `C` - the variant discriminator will be `2`
146
149
147
-
Again, as with fields in `Serializing enums` the variant discriminator values don't need to start from `0` and don't need to be contiguous, but that is our convention and any "holes" in the definition would indicate a retired enum variant.
150
+
Again, as with fields in `Serializing tagged-unions` the variant discriminator values does not need to start from `0` and does not need to be contiguous, but that is our convention and any "discontinuities" in the value set for variant disciminator would indicate a retired tagged-union variant or variants.
148
151
149
152
Next we need to assign field `serialization indices` for each variant:
150
153
@@ -157,8 +160,8 @@ Next we need to assign field `serialization indices` for each variant:
157
160
-`2` for the second tuple element (of type u32),
158
161
-`3` for the second tuple element (of type u64),
159
162
160
-
As you can see, `serialization indices` for fields need to be unique in scope of a particular enum variant.
161
-
Knowing the above, let's see how the `I` and `B` collections would look like for different instances of this enum:
163
+
As you can see, `serialization indices` for fields need to be unique in scope of a particular tagged-union variant.
164
+
Knowing the above, let's see how the `I` and `B` collections would look like for different instances of this tagged-union:
162
165
163
166
- when serializing variant `X::A` (assuming python notation):
0 commit comments