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
+15-18Lines changed: 15 additions & 18 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. Bear in mind that this field will be serialized by appending first a `u32` integer (number of bytes) than the raw bytes payload.
12
+
-`bytes` - field which is an amorphous blob of bytes serialized as `Bytes` in the "regular" `Binary Serialization Standard` schema.
13
13
14
14
A <spanid="field">`Field`</span> consists of:
15
15
@@ -79,9 +79,6 @@ 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 `tagged-union`s.
107
+
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.
111
108
112
109
## Serializing uniform data structures
113
110
@@ -123,15 +120,15 @@ struct A {
123
120
124
121
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.
125
122
126
-
## Serializing tagged-unions
123
+
## Serializing enums
127
124
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:
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:
129
126
130
127
- empty (tag variant)
131
128
- a struct
132
-
- a nested tagged-union
129
+
- a nested enum
133
130
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`):
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:
135
132
136
133
```rust
137
134
enumX {
@@ -141,13 +138,13 @@ As mentioned, there is a polymorphic aspect to these kinds of tagged-union and w
141
138
}
142
139
```
143
140
144
-
First we need to chose variant discriminator values for each of the tagged-union variants, let's select:
141
+
First we need to chose variant discriminator values for each of the enum variants, let's select:
145
142
146
143
- if variant `A` - the variant discriminator will be `0`
147
144
- if variant `B` - the variant discriminator will be `1`
148
145
- if variant `C` - the variant discriminator will be `2`
149
146
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.
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.
151
148
152
149
Next we need to assign field `serialization indices` for each variant:
153
150
@@ -160,8 +157,8 @@ Next we need to assign field `serialization indices` for each variant:
160
157
-`2` for the second tuple element (of type u32),
161
158
-`3` for the second tuple element (of type u64),
162
159
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:
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:
165
162
166
163
- when serializing variant `X::A` (assuming python notation):
0 commit comments