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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
pub use super::impls::*;
use anyhow::{bail, Context};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use concordium_contracts_common::ExchangeRate;
use core::cmp;
use sha2::Digest;
use std::{
    collections::btree_map::BTreeMap,
    convert::{TryFrom, TryInto},
    marker::PhantomData,
};

static MAX_PREALLOCATED_CAPACITY: usize = 4096;

/// Result when deserializing a value. This is a simple wrapper around `Result`
/// that fixes the error type to be [anyhow::Error].
pub type ParseResult<T> = anyhow::Result<T>;

/// As Vec::with_capacity, but only allocate maximum MAX_PREALLOCATED_CAPACITY
/// elements.
#[inline]
pub fn safe_with_capacity<T>(capacity: usize) -> Vec<T> {
    // TODO: This should probably use the size of the type T as well.
    // As long as sizeof(T) is not excessive it does not matter very much, but it
    // would be cleaner.
    Vec::with_capacity(cmp::min(capacity, MAX_PREALLOCATED_CAPACITY))
}

/// Trait for types which can be recovered from byte sources.
pub trait Deserial: Sized {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self>;
}

impl Deserial for u128 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<u128> {
        Ok(source.read_u128::<BigEndian>()?)
    }
}

impl Deserial for u64 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<u64> {
        Ok(source.read_u64::<BigEndian>()?)
    }
}

impl Deserial for u32 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<u32> {
        Ok(source.read_u32::<BigEndian>()?)
    }
}

impl Deserial for u16 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<u16> {
        Ok(source.read_u16::<BigEndian>()?)
    }
}

impl Deserial for bool {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let x: u8 = source.read_u8()?;
        match x {
            0 => Ok(false),
            1 => Ok(true),
            _ => anyhow::bail!("Unrecognized boolean value {}", x),
        }
    }
}

impl Deserial for u8 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<u8> { Ok(source.read_u8()?) }
}

impl Deserial for i128 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<i128> {
        Ok(source.read_i128::<BigEndian>()?)
    }
}

impl Deserial for i64 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<i64> {
        Ok(source.read_i64::<BigEndian>()?)
    }
}

impl Deserial for i32 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<i32> {
        Ok(source.read_i32::<BigEndian>()?)
    }
}

impl Deserial for i16 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<i16> {
        Ok(source.read_i16::<BigEndian>()?)
    }
}

impl Deserial for i8 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<i8> { Ok(source.read_i8()?) }
}

impl Deserial for std::num::NonZeroU8 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}

impl Deserial for std::num::NonZeroU16 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}
impl Deserial for std::num::NonZeroU32 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}

impl Deserial for std::num::NonZeroU64 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}

impl Deserial for std::num::NonZeroU128 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}

impl Deserial for std::num::NonZeroI8 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}

impl Deserial for std::num::NonZeroI16 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}
impl Deserial for std::num::NonZeroI32 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}

impl Deserial for std::num::NonZeroI64 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}

impl Deserial for std::num::NonZeroI128 {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let value = source.get()?;
        Self::new(value).context("Zero is not valid.")
    }
}

/// Read a vector where the first 8 bytes are taken as length in big endian.
impl<T: Deserial> Deserial for Vec<T> {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let len: u64 = u64::deserial(source)?;
        deserial_vector_no_length(source, usize::try_from(len)?)
    }
}

/// Read a set where the first 8 bytes are taken as length in big endian.
/// The values must be serialized in strictly increasing order.
impl<T: Deserial + std::cmp::Ord> Deserial for BTreeSet<T> {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let len: u64 = u64::deserial(source)?;
        deserial_set_no_length(source, usize::try_from(len)?)
    }
}

/// Read a map where the first 8 bytes are taken as length in big endian.
/// The values must be serialized in strictly increasing order of keys.
impl<K: Deserial + std::cmp::Ord, V: Deserial> Deserial for BTreeMap<K, V> {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let len: u64 = u64::deserial(source)?;
        deserial_map_no_length(source, usize::try_from(len)?)
    }
}

impl<T: Deserial, U: Deserial> Deserial for (T, U) {
    #[inline]
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let x = T::deserial(source)?;
        let y = U::deserial(source)?;
        Ok((x, y))
    }
}

impl<T: Deserial, S: Deserial, U: Deserial> Deserial for (T, S, U) {
    #[inline]
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let x = T::deserial(source)?;
        let y = S::deserial(source)?;
        let z = U::deserial(source)?;
        Ok((x, y, z))
    }
}

/// Read a string of given size.
/// NB: Be aware that this allocates a buffer of the given length, and so this
/// must only be used when the size is bounded, otherwise it will lead to a
/// memory allocation failure, and panic.
pub fn deserial_string<R: ReadBytesExt>(reader: &mut R, l: usize) -> ParseResult<String> {
    let mut svec = vec![0; l];
    reader.read_exact(&mut svec)?;
    Ok(String::from_utf8(svec)?)
}

/// Write a string directly to the provided sink (without encoding its length).
/// The string is utf8 encoded.
pub fn serial_string<R: Buffer>(s: &str, out: &mut R) {
    out.write_all(s.as_bytes())
        .expect("Writing to buffer should succeed.")
}

/// Read a vector of a given size. This protects against excessive memory
/// allocation by only pre-allocating a maximum safe size.
pub fn deserial_vector_no_length<R: ReadBytesExt, T: Deserial>(
    reader: &mut R,
    len: usize,
) -> ParseResult<Vec<T>> {
    let mut vec = safe_with_capacity(len);
    for _ in 0..len {
        vec.push(T::deserial(reader)?);
    }
    Ok(vec)
}

/// Read a vector of the given size.
/// NB: Be aware that this allocates a buffer of the given length, and so this
/// must only be used when the size is bounded, otherwise it will lead to a
/// memory allocation failure, and panic.
pub fn deserial_bytes<R: ReadBytesExt>(reader: &mut R, l: usize) -> ParseResult<Vec<u8>> {
    let mut svec = vec![0; l];
    reader.read_exact(&mut svec)?;
    Ok(svec)
}

impl<T> Deserial for PhantomData<T> {
    #[inline]
    fn deserial<R: ReadBytesExt>(_source: &mut R) -> ParseResult<Self> { Ok(Default::default()) }
}

impl<T: Deserial> Deserial for Box<T> {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let x = T::deserial(source)?;
        Ok(Box::new(x))
    }
}

/// Trait for writers which will not fail in normal operation with
/// small amounts of data, e.g., [`Vec<u8>`](Vec).
/// Moreover having a special trait allows us to implement it for
/// other types, such as the SHA Digest.
pub trait Buffer: Sized + WriteBytesExt {
    type Result;
    fn start() -> Self;
    fn start_hint(_l: usize) -> Self { Self::start() }
    fn result(self) -> Self::Result;
}

impl Buffer for Vec<u8> {
    type Result = Vec<u8>;

    fn start() -> Vec<u8> { Vec::new() }

    fn start_hint(l: usize) -> Vec<u8> { Vec::with_capacity(l) }

    fn result(self) -> Self::Result { self }
}

impl Buffer for sha2::Sha256 {
    type Result = [u8; 32];

    fn start() -> Self { sha2::Sha256::new() }

    fn result(self) -> Self::Result { self.finalize().into() }
}

/// Trait implemented by types which can be encoded into byte arrays.
/// The intention is that the encoding is binary and not human readable.
pub trait Serial {
    fn serial<B: Buffer>(&self, _out: &mut B);
}

impl Serial for u64 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_u64::<BigEndian>(*self)
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for u32 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_u32::<BigEndian>(*self)
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for u16 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_u16::<BigEndian>(*self)
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for bool {
    fn serial<B: Buffer>(&self, out: &mut B) {
        (if *self {
            out.write_u8(1)
        } else {
            out.write_u8(0)
        })
        .expect("Writing to a buffer should not fail.");
    }
}

impl Serial for u8 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_u8(*self)
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for i64 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_i64::<BigEndian>(*self)
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for i32 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_i32::<BigEndian>(*self)
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for i16 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_i16::<BigEndian>(*self)
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for i8 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_i8(*self)
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroU8 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_u8(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroU16 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_u16::<BigEndian>(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroU32 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_u32::<BigEndian>(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroU64 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_u64::<BigEndian>(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroU128 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_u128::<BigEndian>(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroI8 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_i8(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroI16 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_i16::<BigEndian>(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroI32 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_i32::<BigEndian>(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroI64 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_i64::<BigEndian>(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

impl Serial for std::num::NonZeroI128 {
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_i128::<BigEndian>(self.get())
            .expect("Writing to a buffer should not fail.")
    }
}

/// Serialize a vector by encoding its length as a u64 in big endian and then
/// the list of elements in sequence.
impl<T: Serial> Serial for Vec<T> {
    fn serial<B: Buffer>(&self, out: &mut B) {
        (self.len() as u64).serial(out);
        serial_vector_no_length(self, out)
    }
}

/// Serialize a set by encoding its size as a u64 in big endian and then
/// the list of elements in increasing order.
impl<V: Serial> Serial for BTreeSet<V> {
    fn serial<B: Buffer>(&self, out: &mut B) {
        (self.len() as u64).serial(out);
        serial_set_no_length(self, out)
    }
}

/// Serialize a map by encoding its size as a u64 in big endian and then
/// the list of key-value pairs in increasing order of keys.
impl<K: Serial, V: Serial> Serial for BTreeMap<K, V> {
    fn serial<B: Buffer>(&self, out: &mut B) {
        (self.len() as u64).serial(out);
        serial_map_no_length(self, out)
    }
}

/// Serialize all of the elements in the iterator.
pub fn serial_iter<'a, B: Buffer, T: Serial + 'a, I: Iterator<Item = &'a T>>(xs: I, out: &mut B) {
    for x in xs {
        x.serial(out);
    }
}

/// Write an array without including length information.
pub fn serial_vector_no_length<B: Buffer, T: Serial>(xs: &[T], out: &mut B) {
    serial_iter(xs.iter(), out)
}

/// Serialize an ordered map. Serialization is by increasing order of keys.
pub fn serial_map_no_length<B: Buffer, K: Serial, V: Serial>(map: &BTreeMap<K, V>, out: &mut B) {
    for (k, v) in map.iter() {
        // iterator over ordered pairs.
        out.put(k);
        out.put(v);
    }
}

/// Deserialize a map from a byte source. This ensures there are no duplicates,
/// as well as that all keys are in strictly increasing order.
pub fn deserial_map_no_length<R: ReadBytesExt, K: Deserial + Ord, V: Deserial>(
    source: &mut R,
    len: usize,
) -> ParseResult<BTreeMap<K, V>> {
    let mut out = BTreeMap::new();
    let mut x = None;
    for _ in 0..len {
        let k = source.get()?;
        let v = source.get()?;
        if let Some((old_k, old_v)) = x.take() {
            if k > old_k {
                out.insert(old_k, old_v);
            } else {
                bail!("Keys not in order.")
            }
        }
        x = Some((k, v));
    }
    if let Some((k, v)) = x {
        out.insert(k, v);
    }
    Ok(out)
}

/// Analogous to [serial_map_no_length], but for sets.
pub fn serial_set_no_length<B: Buffer, K: Serial>(map: &BTreeSet<K>, out: &mut B) {
    for k in map.iter() {
        out.put(k);
    }
}

/// Analogous to [deserial_map_no_length], but for sets.
/// NB: This ensures there are no duplicates, and that all the keys are in
/// strictly increasing order.
pub fn deserial_set_no_length<R: ReadBytesExt, K: Deserial + Ord>(
    source: &mut R,
    len: usize,
) -> ParseResult<BTreeSet<K>> {
    let mut out = BTreeSet::new();
    let mut x = None;
    for _ in 0..len {
        let k = source.get()?;
        if let Some(old_k) = x.take() {
            if k > old_k {
                out.insert(old_k);
            } else {
                bail!("Keys not in order.")
            }
        }
        x = Some(k);
    }
    if let Some(k) = x {
        out.insert(k);
    }
    Ok(out)
}

impl<T: Serial, S: Serial> Serial for (T, S) {
    #[inline]
    fn serial<B: Buffer>(&self, out: &mut B) {
        self.0.serial(out);
        self.1.serial(out);
    }
}

impl<T: Serial, S: Serial, U: Serial> Serial for (T, S, U) {
    #[inline]
    fn serial<B: Buffer>(&self, out: &mut B) {
        self.0.serial(out);
        self.1.serial(out);
        self.2.serial(out);
    }
}

impl<T> Serial for PhantomData<T> {
    #[inline]
    fn serial<B: Buffer>(&self, _out: &mut B) {}
}

impl<T: Serial> Serial for Box<T> {
    #[inline]
    fn serial<B: Buffer>(&self, out: &mut B) { self.as_ref().serial(out) }
}

impl Serial for [u8] {
    #[inline]
    fn serial<B: Buffer>(&self, out: &mut B) {
        out.write_all(self).expect("Writing to buffer is safe.");
    }
}

/// Analogue of [Deserial], but instead this has the type to serialize as a type
/// parameter, and is implemented once for a source. The reason for this trait
/// is that it is often more convenient to use since we can rely on the
/// typechecker to fill in more details. Contrast `A::deserial(source)` to
/// `source.get()`. In the latter case the return type is usually clear from
/// context.
pub trait Get<A> {
    fn get(&mut self) -> ParseResult<A>;
}

impl<R: ReadBytesExt, A: Deserial> Get<A> for R {
    #[inline]
    fn get(&mut self) -> ParseResult<A> { A::deserial(self) }
}

/// Dual to `Get`, and the analogue of `Serial`. It allows writing
/// `sink.put(value)` in contrast to `value.serial(sink)`. It is less important
/// than `Get`.
pub trait Put<A> {
    fn put(&mut self, _v: &A);
}

impl<R: Buffer, A: Serial> Put<A> for R {
    #[inline]
    fn put(&mut self, v: &A) { v.serial(self) }
}

/// A convenient way to refer to both [Serial] and [Deserial] together.
pub trait Serialize: Serial + Deserial {}

/// Generic instance deriving Deserialize for any type that implements
/// both put and get.
impl<A: Deserial + Serial> Serialize for A {}

/// Directly serialize to a vector of bytes.
#[inline]
pub fn to_bytes<A: Serial>(x: &A) -> Vec<u8> {
    let mut buf = Vec::new();
    buf.put(x);
    buf
}

#[inline]
/// A small wrapper that is sometimes more convenient than `A::deserial`.
/// It is here mostly for historical reasons, for backwards compatibility.
pub fn from_bytes<A: Deserial, R: ReadBytesExt>(source: &mut R) -> ParseResult<A> {
    A::deserial(source)
}

// Some more generic implementations

impl<T: Serial, const N: usize> Serial for [T; N] {
    fn serial<B: Buffer>(&self, out: &mut B) {
        for x in self.iter() {
            x.serial(out);
        }
    }
}

impl<T: Deserial, const N: usize> Deserial for [T; N] {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let mut out_vec = Vec::with_capacity(N);
        for _ in 0..N {
            out_vec.push(T::deserial(source)?);
        }
        let out_array: [T; N] = out_vec.try_into().map_err(|_| ()).unwrap();
        Ok(out_array)
    }
}

// Some more std implementations
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};

impl Serial for Ipv4Addr {
    fn serial<W: WriteBytesExt>(&self, target: &mut W) {
        target.write_u8(4).expect("Writing to buffer is safe.");
        target
            .write_all(&self.octets())
            .expect("Writing to buffer is safe.");
    }
}

impl Deserial for Ipv4Addr {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let mut octects = [0u8; 4];
        source.read_exact(&mut octects)?;
        Ok(Ipv4Addr::from(octects))
    }
}

impl Serial for Ipv6Addr {
    fn serial<W: WriteBytesExt>(&self, target: &mut W) {
        target.write_u8(6).expect("Writing to buffer is safe.");
        target
            .write_all(&self.octets())
            .expect("Writing to buffer is safe.");
    }
}

impl Deserial for Ipv6Addr {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let mut octets = [0u8; 16];
        source.read_exact(&mut octets)?;
        Ok(Ipv6Addr::from(octets))
    }
}

impl Serial for IpAddr {
    fn serial<W: Buffer + WriteBytesExt>(&self, target: &mut W) {
        match self {
            IpAddr::V4(ip4) => ip4.serial(target),
            IpAddr::V6(ip6) => ip6.serial(target),
        }
    }
}

impl Deserial for IpAddr {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        match source.read_u8()? {
            4 => Ok(IpAddr::V4(Ipv4Addr::deserial(source)?)),
            6 => Ok(IpAddr::V6(Ipv6Addr::deserial(source)?)),
            x => bail!("Can't deserialize an IpAddr (unknown type: {})", x),
        }
    }
}

impl Serial for SocketAddr {
    fn serial<W: Buffer + WriteBytesExt>(&self, target: &mut W) {
        self.ip().serial(target);
        self.port().serial(target);
    }
}

impl Deserial for SocketAddr {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        Ok(SocketAddr::new(
            IpAddr::deserial(source)?,
            u16::deserial(source)?,
        ))
    }
}

impl Serial for ExchangeRate {
    fn serial<W: Buffer + WriteBytesExt>(&self, target: &mut W) {
        self.numerator().serial(target);
        self.denominator().serial(target);
    }
}

impl Deserial for ExchangeRate {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let numerator = source.get()?;
        let denominator = source.get()?;
        Self::new(numerator, denominator).ok_or_else(|| anyhow::anyhow!("Invalid exchange rate."))
    }
}

use std::{
    collections::{BTreeSet, HashSet},
    hash::{BuildHasher, Hash},
};

impl<T: Serial + Eq + Hash, S: BuildHasher + Default> Serial for HashSet<T, S> {
    fn serial<W: Buffer + WriteBytesExt>(&self, target: &mut W) {
        (self.len() as u32).serial(target);
        self.iter().for_each(|ref item| item.serial(target));
    }
}

impl<T: Deserial + Eq + Hash, S: BuildHasher + Default> Deserial for HashSet<T, S> {
    fn deserial<R: ReadBytesExt>(source: &mut R) -> ParseResult<Self> {
        let len = u32::deserial(source)?;
        let mut out = HashSet::with_capacity_and_hasher(
            std::cmp::min(len as usize, MAX_PREALLOCATED_CAPACITY),
            Default::default(),
        );

        for _i in 0..len {
            out.insert(T::deserial(source)?);
        }
        Ok(out)
    }
}

impl<T: Serial> Serial for &T {
    fn serial<W: Buffer + WriteBytesExt>(&self, target: &mut W) { (*self).serial(target) }
}

// Helpers for json serialization

use hex::{decode, encode};
use serde::{de, de::Visitor, Deserializer, Serializer};
use std::{fmt, io::Cursor};

/// Encode the given value into a byte array using its [Serial] instance, and
/// then encode that byte array as a hex string into the provided serde
/// Serializer.
pub fn base16_encode<S: Serializer, T: Serial>(v: &T, ser: S) -> Result<S::Ok, S::Error> {
    let b16_str = encode(to_bytes(v));
    ser.serialize_str(&b16_str)
}

/// Dual to [base16_encode].
pub fn base16_decode<'de, D: Deserializer<'de>, T: Deserial>(des: D) -> Result<T, D::Error> {
    struct Base16Visitor<D>(std::marker::PhantomData<D>);

    impl<'de, D: Deserial> Visitor<'de> for Base16Visitor<D> {
        type Value = D;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            write!(formatter, "A base 16 string.")
        }

        fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
            let bytes = decode(v).map_err(de::Error::custom)?;
            D::deserial(&mut Cursor::new(&bytes)).map_err(de::Error::custom)
        }
    }

    des.deserialize_str(Base16Visitor(Default::default()))
}

/// Analogous to [base16_encode], but encodes into a string rather than a serde
/// Serializer.
pub fn base16_encode_string<S: Serial>(x: &S) -> String { encode(to_bytes(x)) }

/// Dual to [base16_encode_string].
pub fn base16_decode_string<S: Deserial>(x: &str) -> ParseResult<S> {
    let d = decode(x)?;
    from_bytes(&mut Cursor::new(&d))
}

/// Analogous to [base16_encode] but after serializing to a byte array it only
/// encodes the `&[4..]` into the serde Serializer. This is intended to use in
/// cases where we are encoding a collection such as a vector into JSON. Since
/// JSON is self-describing we do not need to explicitly record the length,
/// which we do in binary.
pub fn base16_ignore_length_encode<S: Serializer, T: Serial>(
    v: &T,
    ser: S,
) -> Result<S::Ok, S::Error> {
    let b16_str = encode(&to_bytes(v)[4..]);
    ser.serialize_str(&b16_str)
}

/// Dual to [base16_ignore_length_encode]
pub fn base16_ignore_length_decode<'de, D: Deserializer<'de>, T: Deserial>(
    des: D,
) -> Result<T, D::Error> {
    // Deserialization in base 16 for values which explicitly record the length.
    // In JSON serialization this explicit length is not needed because JSON is
    // self-describing and we always know the length of input.
    struct Base16IgnoreLengthVisitor<D>(std::marker::PhantomData<D>);

    impl<'de, D: Deserial> Visitor<'de> for Base16IgnoreLengthVisitor<D> {
        type Value = D;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            write!(formatter, "A base 16 string.")
        }

        fn visit_str<E: de::Error>(self, v: &str) -> Result<Self::Value, E> {
            let bytes = decode(v).map_err(de::Error::custom)?;
            let mut all_bytes = Vec::with_capacity(bytes.len() + 4);
            all_bytes.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
            all_bytes.extend_from_slice(&bytes);
            D::deserial(&mut Cursor::new(&all_bytes)).map_err(de::Error::custom)
        }
    }
    des.deserialize_str(Base16IgnoreLengthVisitor(Default::default()))
}

#[test]
fn test_map_serialization() {
    use rand::Rng;
    for n in 0..1000 {
        let mut map = BTreeMap::<u64, u32>::new();
        for (k, v) in rand::thread_rng()
            .sample_iter(rand::distributions::Standard)
            .take(n)
        {
            map.insert(k, v);
        }
        let deserialized = super::serialize_deserialize(&map).expect("Deserialization succeeds.");
        assert_eq!(map, deserialized);
    }
}

#[test]
fn test_set_serialization() {
    use rand::Rng;
    for n in 0..1000 {
        let mut set = BTreeSet::<u64>::new();
        for k in rand::thread_rng()
            .sample_iter(rand::distributions::Standard)
            .take(n)
        {
            set.insert(k);
        }
        let deserialized = super::serialize_deserialize(&set).expect("Deserialization succeeds.");
        assert_eq!(set, deserialized);
    }
}