kdb_codec 1.1.0

Kdb+ IPC codec library for handling kdb+ wire protocol data with Rust.
Documentation
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
//++++++++++++++++++++++++++++++++++++++++++++++++++//
// >> Load Libraries
//++++++++++++++++++++++++++++++++++++++++++++++++++//

use super::*;

//++++++++++++++++++++++++++++++++++++++++++++++++++//
// >> Global Variable
//++++++++++++++++++++++++++++++++++++++++++++++++++//

// %% System encoding %%//vvvvvvvvvvvvvvvvvvvvvvvvvvv/

/// Endian of OS used to serialize `K` object.
/// - 0: Big Endian
/// - 1: Little Endian
#[cfg(target_endian = "big")]
pub const ENCODING: u8 = 0;

/// Endian of OS used to serialize `K` object.
/// - 0: Big Endian
/// - 1: Little Endian
#[cfg(target_endian = "little")]
pub const ENCODING: u8 = 1;

//++++++++++++++++++++++++++++++++++++++++++++++++++//
// >> Implementation
//++++++++++++++++++++++++++++++++++++++++++++++++++//

//%% K %%//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv/

impl K {
    /// Serialize q object to bytes in a manner of q function `-8!` without the IPC message
    ///  header (encoding, message type, compressed, reserved null byte and total message length).
    pub fn q_ipc_encode(&self) -> Vec<u8> {
        let mut stream = Vec::new();
        serialize_q(self, &mut stream);
        stream
    }

    /// Serialize q object to complete IPC message bytes including the 8-byte IPC message header,
    /// optionally attempting kdb+ IPC compression.
    ///
    /// When `compress` is true, this will attempt to compress the message using the kdb+ IPC
    /// compression algorithm (equivalent to q `-18!`). If compression does not reduce the message
    /// to less than half its original size, the uncompressed message is returned.
    pub fn ipc_msg_encode(&self, msg_type: u8, compress: bool) -> Vec<u8> {
        let payload_bytes = self.q_ipc_encode();
        let message_length = payload_bytes.len();
        let total_length = (MessageHeader::size() + message_length) as u32;

        if compress {
            // Prepare raw message with placeholder header and payload
            let mut raw = Vec::with_capacity(MessageHeader::size() + message_length);
            raw.extend_from_slice(&[ENCODING, msg_type, 0, 0, 0, 0, 0, 0]);
            raw.extend_from_slice(&payload_bytes);

            // Try to compress
            let (was_compressed, mut bytes) = compress_sync(raw);
            if was_compressed {
                return bytes;
            }

            // Not compressed: write correct total length into header
            let total_length_bytes = match ENCODING {
                0 => total_length.to_be_bytes(),
                _ => total_length.to_le_bytes(),
            };
            bytes[4..8].copy_from_slice(&total_length_bytes);
            return bytes;
        }

        // Uncompressed message
        let header = MessageHeader {
            encoding: ENCODING,
            message_type: msg_type,
            compressed: 0,
            _unused: 0,
            length: total_length,
        };

        let mut out = Vec::with_capacity(MessageHeader::size() + message_length);
        out.extend_from_slice(&header.to_bytes());
        out.extend_from_slice(&payload_bytes);
        out
    }
    
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::connection::qmsg_type;

    fn read_u32(bytes: &[u8]) -> u32 {
        match ENCODING {
            0 => u32::from_be_bytes(bytes.try_into().unwrap()),
            _ => u32::from_le_bytes(bytes.try_into().unwrap()),
        }
    }

    #[test]
    fn ipc_msg_encode_uncompressed_has_valid_header_and_length() {
        let k = K::new_int(42);
        let payload = k.q_ipc_encode();
        let msg = k.ipc_msg_encode(qmsg_type::synchronous, false);

        assert_eq!(msg[0], ENCODING);
        assert_eq!(msg[1], qmsg_type::synchronous);
        assert_eq!(msg[2], 0);
        assert_eq!(msg[3], 0);

        let length = read_u32(&msg[4..8]);
        assert_eq!(length as usize, msg.len());
        assert_eq!(length as usize, MessageHeader::size() + payload.len());

        assert_eq!(&msg[MessageHeader::size()..], payload.as_slice());
    }

    #[test]
    fn ipc_msg_encode_with_compression_produces_compressed_frame_and_roundtrips() {
        // Highly compressible payload
        let k = K::new_byte_list(vec![0u8; 20_000], qattribute::NONE);
        let payload = k.q_ipc_encode();
        let msg = k.ipc_msg_encode(qmsg_type::synchronous, true);

        assert_eq!(msg[0], ENCODING);
        assert_eq!(msg[1], qmsg_type::synchronous);
        assert_eq!(msg[2], 1);
        assert_eq!(msg[3], 0);

        let compressed_total_len = read_u32(&msg[4..8]) as usize;
        assert_eq!(compressed_total_len, msg.len());

        let uncompressed_total_len = read_u32(&msg[8..12]) as usize;
        assert_eq!(uncompressed_total_len, MessageHeader::size() + payload.len());

        let decompressed_payload = decompress_sync(msg[8..].to_vec(), msg[0], None).unwrap();
        assert_eq!(decompressed_payload, payload);
    }

    #[test]
    fn ipc_msg_encode_with_compression_falls_back_to_uncompressed_when_not_worth_it() {
        // Pseudo-random-ish bytes should not compress to < half.
        let data: Vec<u8> = (0u32..5000)
            .map(|i| ((i.wrapping_mul(31).wrapping_add(7)) % 256) as u8)
            .collect();
        let k = K::new_byte_list(data, qattribute::NONE);
        let payload = k.q_ipc_encode();

        let msg = k.ipc_msg_encode(qmsg_type::synchronous, true);

        assert_eq!(msg[0], ENCODING);
        assert_eq!(msg[1], qmsg_type::synchronous);
        assert_eq!(msg[2], 0);
        assert_eq!(msg[3], 0);

        let total_len = read_u32(&msg[4..8]) as usize;
        assert_eq!(total_len, msg.len());
        assert_eq!(total_len, MessageHeader::size() + payload.len());
        assert_eq!(&msg[MessageHeader::size()..], payload.as_slice());
    }

    #[test]
    fn ipc_msg_decode_uncompressed_roundtrips() {
        let original = K::new_int(42);
        let msg = original.ipc_msg_encode(qmsg_type::synchronous, false);

        let (header, decoded) = K::ipc_msg_decode(&msg).unwrap();

        assert_eq!(header.encoding, ENCODING);
        assert_eq!(header.message_type, qmsg_type::synchronous);
        assert_eq!(header.compressed, 0);
        assert_eq!(header.length as usize, msg.len());

        assert_eq!(decoded.get_type(), qtype::INT_ATOM);
        assert_eq!(decoded.get_int().unwrap(), 42);
    }

    #[test]
    fn ipc_msg_decode_compressed_roundtrips() {
        // Highly compressible payload
        let original = K::new_byte_list(vec![0u8; 20_000], qattribute::NONE);
        let msg = original.ipc_msg_encode(qmsg_type::asynchronous, true);

        let (header, decoded) = K::ipc_msg_decode(&msg).unwrap();

        assert_eq!(header.encoding, ENCODING);
        assert_eq!(header.message_type, qmsg_type::asynchronous);
        assert_eq!(header.compressed, 1);

        assert_eq!(decoded.get_type(), qtype::BYTE_LIST);
        let decoded_list = decoded.as_vec::<u8>().unwrap();
        assert_eq!(decoded_list.len(), 20_000);
        assert!(decoded_list.iter().all(|&b| b == 0));
    }

    #[test]
    fn ipc_msg_decode_complex_object_roundtrips() {
        // Test with a symbol list
        let original = K::new_symbol_list(
            vec!["hello".to_string(), "world".to_string(), "kdb".to_string()],
            qattribute::NONE,
        );
        let msg = original.ipc_msg_encode(qmsg_type::response, false);

        let (header, decoded) = K::ipc_msg_decode(&msg).unwrap();

        assert_eq!(header.message_type, qmsg_type::response);
        assert_eq!(decoded.get_type(), qtype::SYMBOL_LIST);

        let decoded_list = decoded.as_vec::<String>().unwrap();
        assert_eq!(*decoded_list, vec!["hello".to_string(), "world".to_string(), "kdb".to_string()]);
    }

    #[test]
    fn ipc_msg_decode_fails_on_invalid_header() {
        let invalid_msg = vec![1, 2, 3]; // Too short for a header
        let result = K::ipc_msg_decode(&invalid_msg);
        assert!(result.is_err());
    }
}

//++++++++++++++++++++++++++++++++++++++++++++++++++//
// >> Private Functions
//++++++++++++++++++++++++++++++++++++++++++++++++++//

fn serialize_q(obj: &K, stream: &mut Vec<u8>) {
    match obj.0.qtype {
        qtype::BOOL_ATOM | qtype::BYTE_ATOM | qtype::CHAR => serialize_byte(obj, stream),
        qtype::GUID_ATOM => serialize_guid(obj, stream),
        qtype::SHORT_ATOM => serialize_short(obj, stream),
        qtype::INT_ATOM
        | qtype::MONTH_ATOM
        | qtype::DATE_ATOM
        | qtype::MINUTE_ATOM
        | qtype::SECOND_ATOM
        | qtype::TIME_ATOM => serialize_int(obj, stream),
        qtype::LONG_ATOM | qtype::TIMESTAMP_ATOM | qtype::TIMESPAN_ATOM => {
            serialize_long(obj, stream)
        }
        qtype::REAL_ATOM => serialize_real(obj, stream),
        qtype::FLOAT_ATOM | qtype::DATETIME_ATOM => serialize_float(obj, stream),
        qtype::SYMBOL_ATOM => serialize_symbol(obj, stream),
        qtype::COMPOUND_LIST => serialize_compound_list(obj, stream),
        qtype::BOOL_LIST | qtype::BYTE_LIST => serialize_byte_list(obj, stream),
        qtype::GUID_LIST => serialize_guid_list(obj, stream),
        qtype::SHORT_LIST => serialize_short_list(obj, stream),
        qtype::INT_LIST
        | qtype::MONTH_LIST
        | qtype::DATE_LIST
        | qtype::MINUTE_LIST
        | qtype::SECOND_LIST
        | qtype::TIME_LIST => serialize_int_list(obj, stream),
        qtype::LONG_LIST | qtype::TIMESTAMP_LIST | qtype::TIMESPAN_LIST => {
            serialize_long_list(obj, stream)
        }
        qtype::REAL_LIST => serialize_real_list(obj, stream),
        qtype::FLOAT_LIST | qtype::DATETIME_LIST => serialize_float_list(obj, stream),
        qtype::STRING => serialize_string(obj, stream),
        qtype::SYMBOL_LIST => serialize_symbol_list(obj, stream),
        qtype::TABLE => serialize_table(obj, stream),
        qtype::DICTIONARY | qtype::SORTED_DICTIONARY => serialize_dictionary(obj, stream),
        qtype::LAMBDA => serialize_lambda(obj, stream),
        qtype::UNARY_PRIMITIVE => serialize_unary_primitive_or_null(obj, stream),
        qtype::BINARY_PRIMITIVE => serialize_opaque_payload_type(obj, stream),
        qtype::PROJECTION => serialize_opaque_payload_type(obj, stream),
        qtype::COMPOSITION => serialize_opaque_payload_type(obj, stream),
        qtype::EACH => serialize_opaque_payload_type(obj, stream),
        qtype::OVER => serialize_opaque_payload_type(obj, stream),
        qtype::SCAN => serialize_opaque_payload_type(obj, stream),
        qtype::EACH_PRIOR => serialize_opaque_payload_type(obj, stream),
        qtype::EACH_LEFT => serialize_opaque_payload_type(obj, stream),
        qtype::EACH_RIGHT => serialize_opaque_payload_type(obj, stream),
        qtype::FOREIGN => serialize_opaque_payload_type(obj, stream),
        _ => unimplemented!(),
    };
}

fn serialize_unary_primitive_or_null(obj: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(qtype::UNARY_PRIMITIVE as u8);

    // Data
    match &obj.0.value {
        k0_inner::null(()) => {
            // (::) encodes as unary primitive id 0
            stream.push(0x00);
        }
        k0_inner::opaque(payload) => {
            stream.extend_from_slice(payload);
        }
        _ => {
            // Preserve historical behavior: treat qtype 101 as null if caller constructed it
            // without the opaque payload.
            stream.push(0x00);
        }
    }
}

fn serialize_opaque_payload_type(obj: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(obj.0.qtype as u8);

    // Data
    if let k0_inner::opaque(payload) = &obj.0.value {
        stream.extend_from_slice(payload);
    } else {
        // No payload stored; encode as just the type byte.
        // This is roundtrip-unsafe but avoids panicking.
    }
}

fn serialize_lambda(lambda: &K, stream: &mut Vec<u8>) {
    let (context, body) = lambda.as_lambda().unwrap();

    // Type
    stream.push(qtype::LAMBDA as u8);

    // Context: null terminated string ("" for root)
    stream.extend_from_slice(context.as_bytes());
    stream.push(0x00);

    // Body: char vector (type 10)
    stream.push(qtype::STRING as u8);
    stream.push(qattribute::NONE as u8);

    let bytes = body.as_bytes();
    let length = match ENCODING {
        0 => (bytes.len() as u32).to_be_bytes(),
        _ => (bytes.len() as u32).to_le_bytes(),
    };
    stream.extend_from_slice(&length);
    stream.extend_from_slice(bytes);
}

fn serialize_guid(guid: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0xfe);
    // Element
    stream.extend_from_slice(&guid.get_guid().unwrap());
}

fn serialize_byte(byte: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(byte.0.qtype as u8);
    // Element
    stream.push(byte.get_byte().unwrap());
}

fn serialize_short(short: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0xfb);
    // Element
    stream.extend_from_slice(&match ENCODING {
        0 => short.get_short().unwrap().to_be_bytes(),
        _ => short.get_short().unwrap().to_le_bytes(),
    });
}

fn serialize_int(int: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(int.0.qtype as u8);
    // Element
    stream.extend_from_slice(&match ENCODING {
        0 => int.get_int().unwrap().to_be_bytes(),
        _ => int.get_int().unwrap().to_le_bytes(),
    });
}

fn serialize_long(long: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(long.0.qtype as u8);
    // Element
    stream.extend_from_slice(&match ENCODING {
        0 => long.get_long().unwrap().to_be_bytes(),
        _ => long.get_long().unwrap().to_le_bytes(),
    });
}

fn serialize_real(real: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0xf8);
    // Element
    stream.extend_from_slice(&match ENCODING {
        0 => real.get_real().unwrap().to_be_bytes(),
        _ => real.get_real().unwrap().to_le_bytes(),
    });
}

fn serialize_float(float: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(float.0.qtype as u8);
    // Element
    stream.extend_from_slice(&match ENCODING {
        0 => float.get_float().unwrap().to_be_bytes(),
        _ => float.get_float().unwrap().to_le_bytes(),
    });
}

fn serialize_symbol(symbol: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0xf5);
    // Element
    stream.extend_from_slice(symbol.get_symbol().unwrap().as_bytes());
    // Null byte
    stream.push(0x00);
}

fn serialize_guid_list(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0x02);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_vec::<U>().unwrap();
    // Length of vector
    let length = match ENCODING {
        0 => (vector.len() as u32).to_be_bytes(),
        _ => (vector.len() as u32).to_le_bytes(),
    };
    stream.extend_from_slice(&length);
    vector
        .iter()
        .for_each(|element| stream.extend_from_slice(element));
}

fn serialize_byte_list(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(list.0.qtype as u8);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_vec::<G>().unwrap();
    // Length of vector
    let length = match ENCODING {
        0 => (vector.len() as u32).to_be_bytes(),
        _ => (vector.len() as u32).to_le_bytes(),
    };
    stream.extend_from_slice(&length);
    stream.extend_from_slice(vector.as_slice());
}

fn serialize_short_list(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0x05);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_vec::<H>().unwrap();
    match ENCODING {
        0 => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_be_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_be_bytes());
            });
        }
        _ => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_le_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_le_bytes());
            });
        }
    }
}

fn serialize_int_list(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(list.0.qtype as u8);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_vec::<I>().unwrap();
    match ENCODING {
        0 => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_be_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_be_bytes());
            });
        }
        _ => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_le_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_le_bytes());
            });
        }
    }
}

fn serialize_long_list(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(list.0.qtype as u8);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_vec::<J>().unwrap();
    match ENCODING {
        0 => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_be_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_be_bytes());
            });
        }
        _ => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_le_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_le_bytes());
            });
        }
    }
}

fn serialize_real_list(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0x08);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_vec::<E>().unwrap();
    match ENCODING {
        0 => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_be_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_be_bytes());
            });
        }
        _ => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_le_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_le_bytes());
            });
        }
    }
}

fn serialize_float_list(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(list.0.qtype as u8);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_vec::<F>().unwrap();
    match ENCODING {
        0 => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_be_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_be_bytes());
            });
        }
        _ => {
            // Length of vector
            stream.extend_from_slice(&(vector.len() as u32).to_le_bytes());
            // Data
            vector.iter().for_each(|element| {
                stream.extend_from_slice(&element.to_le_bytes());
            });
        }
    }
}

fn serialize_string(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0x0a);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_string().unwrap().as_bytes();
    // Length of vector
    stream.extend_from_slice(&match ENCODING {
        0 => (vector.len() as u32).to_be_bytes(),
        _ => (vector.len() as u32).to_le_bytes(),
    });
    // Data
    stream.extend_from_slice(&vector);
}

fn serialize_symbol_list(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0x0b);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_vec::<S>().unwrap();
    // Length of vector
    stream.extend_from_slice(&match ENCODING {
        0 => (vector.len() as u32).to_be_bytes(),
        _ => (vector.len() as u32).to_le_bytes(),
    });
    // Data
    vector.iter().for_each(|element| {
        stream.extend_from_slice(&element.as_bytes());
        stream.push(0x00);
    });
}

fn serialize_compound_list(list: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(list.0.qtype as u8);
    // Attribute
    stream.push(list.0.attribute as u8);
    // Length and data
    let vector = list.as_vec::<K>().unwrap();
    // Length and data
    stream.extend_from_slice(&match ENCODING {
        0 => (vector.len() as u32).to_be_bytes(),
        _ => (vector.len() as u32).to_le_bytes(),
    });
    // Data
    vector.iter().for_each(|element| {
        serialize_q(element, stream);
    });
}

fn serialize_table(table: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(0x62);
    // Attribute (e.g. `s#` for sorted table)
    stream.push(table.0.attribute as u8);
    // Dictionary qtype marker (99)
    stream.push(0x63);
    // Retrieve underying dictionary
    let vector = table.get_dictionary().unwrap().as_vec::<K>().unwrap();
    // Serialize keys
    serialize_symbol_list(&vector[0], stream);
    // Serialize values
    serialize_compound_list(&vector[1], stream);
}

fn serialize_dictionary(dictionary: &K, stream: &mut Vec<u8>) {
    // Type
    stream.push(dictionary.0.qtype as u8);
    // Data
    let vector = dictionary.as_vec::<K>().unwrap();
    // Serialize keys
    serialize_q(&vector[0], stream);
    // Serialize values
    serialize_q(&vector[1], stream);
}

fn serialize_null(stream: &mut Vec<u8>) {
    // Backwards-compatible helper for historical callers.
    stream.push(0x65);
    stream.push(0x00);
}