@@ -28,8 +28,8 @@ enum Value {
28
28
idx : BigInt ,
29
29
data : Value ,
30
30
},
31
- /// A "bag of bytes ", used for unions .
32
- Bytes (List <AbstractByte >),
31
+ /// Unions are represented as "lists of chunks ", where each chunk is just a raw list of bytes .
32
+ Union (List <List < AbstractByte > >),
33
33
}
34
34
```
35
35
@@ -192,19 +192,19 @@ For simplicity, we only define pairs for now.
192
192
impl Type {
193
193
fn decode (Tuple { fields : [field1 , field2 ], size }: Self , bytes : List <AbstractByte >) -> Option <Value > {
194
194
if bytes . len () != size { yeet! (); }
195
- let (size1 , type1 ) = field1 ;
196
- let val1 = type1 . decode (bytes [size1 .. ][.. type1 . size ()]);
197
- let (size2 , type2 ) = field2 ;
198
- let val2 = type1 . decode (bytes [size2 .. ][.. type2 . size ()]);
195
+ let (offset1 , type1 ) = field1 ;
196
+ let val1 = type1 . decode (bytes [offset1 .. ][.. type1 . size ()]);
197
+ let (offset2 , type2 ) = field2 ;
198
+ let val2 = type1 . decode (bytes [offset2 .. ][.. type2 . size ()]);
199
199
Value :: Tuple ([val1 , val2 ])
200
200
}
201
201
fn encode (Tuple { fields : [field1 , field2 ], size }: Self , val : Value ) -> List <AbstractByte > {
202
202
let Value :: Tuple ([val1 , val2 ]) = val else { panic! () };
203
203
let mut bytes = [AbstractByte :: Uninit ; size ];
204
- let (size1 , type1 ) = field1 ;
205
- bytes [size1 .. ][.. type1 . size ()] = type1 . encode (val1 );
206
- let (size2 , type2 ) = field2 ;
207
- bytes [size2 .. ][.. type2 . size ()] = type2 . encode (val2 );
204
+ let (offset1 , type1 ) = field1 ;
205
+ bytes [offset1 .. ][.. type1 . size ()] = type1 . encode (val1 );
206
+ let (offset2 , type2 ) = field2 ;
207
+ bytes [offset2 .. ][.. type2 . size ()] = type2 . encode (val2 );
208
208
bytes
209
209
}
210
210
}
@@ -224,12 +224,24 @@ A union simply stores the bytes directly, no high-level interpretation of data h
224
224
225
225
``` rust
226
226
impl Type {
227
- fn decode (Union { size , .. }: Self , bytes : List <AbstractByte >) -> Option <Value > {
227
+ fn decode (Union { size , chunks , .. }: Self , bytes : List <AbstractByte >) -> Option <Value > {
228
228
if bytes . len () != size { yeet! (); }
229
- Value :: Bytes (bytes )
229
+ let mut chunk_data = list! [];
230
+ // Store the data from each chunk.
231
+ for (offset , size ) in chunks {
232
+ chunk_data . push (bytes [offset .. ][.. size ]);
233
+ }
234
+ Value :: Union (chunk_data )
230
235
}
231
- fn encode (Union { size , .. }: Self , value : Value ) -> List <AbstractByte > {
232
- let Value :: Bytes (bytes ) = val else { panic! () };
236
+ fn encode (Union { size , chunks , .. }: Self , value : Value ) -> List <AbstractByte > {
237
+ let Value :: Union (chunk_data ) = val else { panic! () };
238
+ assert_eq! (chunk_data . len (), chunks . len ());
239
+ let mut bytes = [AbstractByte :: Uninit ; size ];
240
+ // Restore the data from each chunk.
241
+ for ((offset , size ), data ) in chunks . iter (). zip (chunk_data . iter ()) {
242
+ assert_eq! (data . len (), size );
243
+ bytes [offset .. ][.. size ] = data ;
244
+ }
233
245
bytes
234
246
}
235
247
}
0 commit comments