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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
use crate::{constants::*, schema, traits::*, types::*};

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, collections, string::String, vec::Vec};
use collections::{BTreeMap, BTreeSet};
use convert::TryFrom;
#[cfg(not(feature = "std"))]
use core::{convert, hash, marker, mem::MaybeUninit, slice};
use hash::Hash;
#[cfg(feature = "std")]
use std::{collections, convert, hash, marker, mem::MaybeUninit, slice};
// Implementations of Serialize

impl<X: Serial, Y: Serial> Serial for (X, Y) {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.0.serial(out)?;
        self.1.serial(out)
    }
}

impl<X: Deserial, Y: Deserial> Deserial for (X, Y) {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let x = X::deserial(source)?;
        let y = Y::deserial(source)?;
        Ok((x, y))
    }
}

impl Serial for () {
    #[inline(always)]
    fn serial<W: Write>(&self, _out: &mut W) -> Result<(), W::Err> { Ok(()) }
}

impl Deserial for () {
    #[inline(always)]
    fn deserial<R: Read>(_source: &mut R) -> ParseResult<Self> { Ok(()) }
}

impl Serial for u8 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_u8(*self) }
}

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

impl Serial for u16 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_u16(*self) }
}

impl Deserial for u16 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> { source.read_u16() }
}

impl Serial for u32 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_u32(*self) }
}

impl Deserial for u32 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> { source.read_u32() }
}

impl Serial for u64 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_u64(*self) }
}

impl Serial for u128 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        out.write_all(&self.to_le_bytes())
    }
}

impl Deserial for u64 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> { source.read_u64() }
}

impl Deserial for u128 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let bytes = read_n_bytes!(16, source);
        Ok(u128::from_le_bytes(bytes))
    }
}

impl Serial for i8 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_i8(*self) }
}

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

impl Serial for i16 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_i16(*self) }
}

impl Deserial for i16 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> { source.read_i16() }
}

impl Serial for i32 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_i32(*self) }
}

impl Deserial for i32 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> { source.read_i32() }
}

impl Serial for i64 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_i64(*self) }
}

impl Deserial for i64 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> { source.read_i64() }
}

impl Serial for i128 {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        out.write_all(&self.to_le_bytes())
    }
}

impl Deserial for i128 {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let bytes = read_n_bytes!(16, source);
        Ok(i128::from_le_bytes(bytes))
    }
}

/// Serialization of `bool` encodes it as a single byte, `false` is represented
/// by `0u8` and `true` is _only_ represented by `1u8`.
impl Serial for bool {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        if *self {
            out.write_u8(1)?;
        } else {
            out.write_u8(0)?;
        }
        Ok(())
    }
}

/// Deserializing a `bool` reads one byte, and returns the value `false` if the
/// byte is `0u8` and `true` if the byte is `1u8`, every other value results in
/// an error.
impl Deserial for bool {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let b = source.read_u8()?;
        match b {
            0 => Ok(false),
            1 => Ok(true),
            _ => Err(ParseError::default()),
        }
    }
}

impl Serial for Amount {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_u64(self.micro_ccd) }
}

impl Deserial for Amount {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        source.read_u64().map(Amount::from_micro_ccd)
    }
}

impl Serial for Timestamp {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.timestamp_millis().serial(out)
    }
}

impl Deserial for Timestamp {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        u64::deserial(source).map(Timestamp::from_timestamp_millis)
    }
}

impl Serial for Duration {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { self.millis().serial(out) }
}

impl Deserial for Duration {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        u64::deserial(source).map(Duration::from_millis)
    }
}

/// Serialized by writing an `u32` representing the number of bytes for a
/// utf8-encoding of the string, then writing the bytes. Similar to `Vec<_>`.
impl Serial for &str {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let bytes = self.as_bytes();
        let len = bytes.len() as u32;
        len.serial(out)?;
        out.write_all(bytes)
    }
}

/// Serialized by writing an `u32` representing the number of bytes for a
/// utf8-encoding of the string, then writing the bytes. Similar to `&str`.
impl Serial for String {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { self.as_str().serial(out) }
}

/// Deserial by reading an `u32` representing the number of bytes, then takes
/// that number of bytes and tries to decode using utf8.
impl Deserial for String {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let bytes = Vec::deserial(source)?;
        let res = String::from_utf8(bytes).map_err(|_| ParseError::default())?;
        Ok(res)
    }
}

impl<T: Serial> Serial for Box<T> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { self.as_ref().serial(out) }
}

impl<T: Deserial> Deserial for Box<T> {
    #[inline]
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let t = T::deserial(source)?;
        Ok(Box::new(t))
    }
}

impl<C: ?Sized> Serial for marker::PhantomData<C> {
    #[inline(always)]
    fn serial<W: Write>(&self, _out: &mut W) -> Result<(), W::Err> { Ok(()) }
}

impl<C: ?Sized> Deserial for marker::PhantomData<C> {
    #[inline(always)]
    fn deserial<R: Read>(_source: &mut R) -> ParseResult<Self> {
        Ok(marker::PhantomData::default())
    }
}

/// Serialized if the `Option` is a `None` we write `0u8`. If `Some`, we write
/// `1u8` followed by the serialization of the contained `T`.
impl<T: Serial> Serial for Option<T> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        match self {
            None => out.write_u8(0),
            Some(t) => {
                out.write_u8(1)?;
                t.serial(out)
            }
        }
    }
}

/// Deserial by reading one byte, where `0u8` represents `None` and `1u8`
/// represents `Some`, every other value results in an error.
/// In the case of `Some` we deserialize using the contained `T`.
impl<T: Deserial> Deserial for Option<T> {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let idx: u8 = source.get()?;
        match idx {
            0 => Ok(None),
            1 => {
                let t = T::deserial(source)?;
                Ok(Some(t))
            }
            _ => Err(ParseError::default()),
        }
    }
}

impl Serial for AccountAddress {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { out.write_all(&self.0) }
}

impl Deserial for AccountAddress {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let bytes = {
            // This deliberately does not initialize the array up-front.
            // Initialization is not needed, and costs quite a bit of code space in the Wasm
            // generated code. Since account addresses
            let mut bytes: MaybeUninit<[u8; 32]> = MaybeUninit::uninit();
            let write_bytes =
                unsafe { slice::from_raw_parts_mut(bytes.as_mut_ptr() as *mut u8, 32) };
            source.read_exact(write_bytes)?;
            unsafe { bytes.assume_init() }
        };
        Ok(AccountAddress(bytes))
    }
}

impl Serial for ContractAddress {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        out.write_u64(self.index)?;
        out.write_u64(self.subindex)
    }
}

impl Deserial for ContractAddress {
    #[inline]
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let index = source.get()?;
        let subindex = source.get()?;
        Ok(ContractAddress {
            index,
            subindex,
        })
    }
}

impl Serial for Address {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        match self {
            Address::Account(ref acc) => {
                out.write_u8(0)?;
                acc.serial(out)
            }
            Address::Contract(ref cnt) => {
                out.write_u8(1)?;
                cnt.serial(out)
            }
        }
    }
}

impl Deserial for Address {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let tag = u8::deserial(source)?;
        match tag {
            0 => Ok(Address::Account(source.get()?)),
            1 => Ok(Address::Contract(source.get()?)),
            _ => Err(ParseError::default()),
        }
    }
}

impl<'a> Serial for ContractName<'a> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let name = self.get_chain_name();
        let len = name.len() as u16;
        len.serial(out)?;
        serial_vector_no_length(name.as_bytes(), out)
    }
}

impl Serial for OwnedContractName {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.as_contract_name().serial(out)
    }
}

impl Deserial for OwnedContractName {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u16 = source.get()?;
        let bytes = deserial_vector_no_length(source, len as usize)?;
        let name = String::from_utf8(bytes).map_err(|_| ParseError::default())?;
        let owned_contract_name =
            OwnedContractName::new(name).map_err(|_| ParseError::default())?;
        Ok(owned_contract_name)
    }
}

impl<'a> Serial for ReceiveName<'a> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let name = self.get_chain_name();
        let len = name.len() as u16;
        len.serial(out)?;
        serial_vector_no_length(name.as_bytes(), out)
    }
}

impl Serial for OwnedReceiveName {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.as_receive_name().serial(out)
    }
}

impl Deserial for OwnedReceiveName {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u16 = source.get()?;
        let bytes = deserial_vector_no_length(source, len as usize)?;
        let name = String::from_utf8(bytes).map_err(|_| ParseError::default())?;
        let owned_receive_name = OwnedReceiveName::new(name).map_err(|_| ParseError::default())?;
        Ok(owned_receive_name)
    }
}

impl<'a> Serial for EntrypointName<'a> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let len = self.0.len() as u16;
        len.serial(out)?;
        serial_vector_no_length(self.0.as_bytes(), out)
    }
}

impl Serial for OwnedEntrypointName {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.as_entrypoint_name().serial(out)
    }
}

impl Deserial for OwnedEntrypointName {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u16 = source.get()?;
        let bytes = deserial_vector_no_length(source, len as usize)?;
        let name = String::from_utf8(bytes).map_err(|_| ParseError::default())?;
        let owned_entrypoint_name = Self::new(name).map_err(|_| ParseError::default())?;
        Ok(owned_entrypoint_name)
    }
}

impl<'a> Serial for Parameter<'a> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let len = self.0.len() as u16;
        len.serial(out)?;
        out.write_all(self.0)
    }
}

impl Serial for OwnedParameter {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.as_parameter().serial(out)
    }
}

impl Deserial for OwnedParameter {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u16 = source.get()?;
        let bytes = deserial_vector_no_length(source, len as usize)?;
        Ok(Self(bytes))
    }
}

impl Serial for ChainMetadata {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { self.slot_time.serial(out) }
}

impl Deserial for ChainMetadata {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let slot_time = source.get()?;
        Ok(Self {
            slot_time,
        })
    }
}

impl<K: Serial + Ord> SerialCtx for BTreeSet<K> {
    fn serial_ctx<W: Write>(
        &self,
        size_len: schema::SizeLength,
        out: &mut W,
    ) -> Result<(), W::Err> {
        schema::serial_length(self.len(), size_len, out)?;
        serial_set_no_length(self, out)
    }
}

impl<K: Deserial + Ord + Copy> DeserialCtx for BTreeSet<K> {
    fn deserial_ctx<R: Read>(
        size_len: schema::SizeLength,
        ensure_ordered: bool,
        source: &mut R,
    ) -> ParseResult<Self> {
        let len = schema::deserial_length(source, size_len)?;
        if ensure_ordered {
            deserial_set_no_length(source, len)
        } else {
            deserial_set_no_length_no_order_check(source, len)
        }
    }
}

impl<K: Serial + Ord, V: Serial> SerialCtx for BTreeMap<K, V> {
    fn serial_ctx<W: Write>(
        &self,
        size_len: schema::SizeLength,
        out: &mut W,
    ) -> Result<(), W::Err> {
        schema::serial_length(self.len(), size_len, out)?;
        serial_map_no_length(self, out)
    }
}

impl<K: Deserial + Ord + Copy, V: Deserial> DeserialCtx for BTreeMap<K, V> {
    fn deserial_ctx<R: Read>(
        size_len: schema::SizeLength,
        ensure_ordered: bool,
        source: &mut R,
    ) -> ParseResult<Self> {
        let len = schema::deserial_length(source, size_len)?;
        if ensure_ordered {
            deserial_map_no_length(source, len)
        } else {
            deserial_map_no_length_no_order_check(source, len)
        }
    }
}

/// Serialization for HashSet given a size_len.
/// Values are not serialized in any particular order.
impl<K: Serial> SerialCtx for HashSet<K> {
    fn serial_ctx<W: Write>(
        &self,
        size_len: schema::SizeLength,
        out: &mut W,
    ) -> Result<(), W::Err> {
        schema::serial_length(self.len(), size_len, out)?;
        serial_hashset_no_length(self, out)
    }
}

/// Deserialization for HashSet given a size_len.
/// Values are not verified to be in any particular order and setting
/// ensure_ordering have no effect.
impl<K: Deserial + Eq + Hash> DeserialCtx for HashSet<K> {
    fn deserial_ctx<R: Read>(
        size_len: schema::SizeLength,
        _ensure_ordered: bool,
        source: &mut R,
    ) -> ParseResult<Self> {
        let len = schema::deserial_length(source, size_len)?;
        deserial_hashset_no_length(source, len)
    }
}

/// Serialization for HashMap given a size_len.
/// Keys are not serialized in any particular order.
impl<K: Serial, V: Serial> SerialCtx for HashMap<K, V> {
    fn serial_ctx<W: Write>(
        &self,
        size_len: schema::SizeLength,
        out: &mut W,
    ) -> Result<(), W::Err> {
        schema::serial_length(self.len(), size_len, out)?;
        serial_hashmap_no_length(self, out)
    }
}

/// Deserialization for HashMap given a size_len.
/// Keys are not verified to be in any particular order and setting
/// ensure_ordering have no effect.
impl<K: Deserial + Eq + Hash, V: Deserial> DeserialCtx for HashMap<K, V> {
    fn deserial_ctx<R: Read>(
        size_len: schema::SizeLength,
        _ensure_ordered: bool,
        source: &mut R,
    ) -> ParseResult<Self> {
        let len = schema::deserial_length(source, size_len)?;
        deserial_hashmap_no_length(source, len)
    }
}

impl<T: Serial> SerialCtx for &[T] {
    fn serial_ctx<W: Write>(
        &self,
        size_len: schema::SizeLength,
        out: &mut W,
    ) -> Result<(), W::Err> {
        schema::serial_length(self.len(), size_len, out)?;
        serial_vector_no_length(self, out)
    }
}

impl<T: Serial> SerialCtx for Vec<T> {
    fn serial_ctx<W: Write>(
        &self,
        size_len: schema::SizeLength,
        out: &mut W,
    ) -> Result<(), W::Err> {
        self.as_slice().serial_ctx(size_len, out)
    }
}

impl<T: Deserial> DeserialCtx for Vec<T> {
    fn deserial_ctx<R: Read>(
        size_len: schema::SizeLength,
        _ensure_ordered: bool,
        source: &mut R,
    ) -> ParseResult<Self> {
        let len = schema::deserial_length(source, size_len)?;
        deserial_vector_no_length(source, len)
    }
}

impl SerialCtx for &str {
    fn serial_ctx<W: Write>(
        &self,
        size_len: schema::SizeLength,
        out: &mut W,
    ) -> Result<(), W::Err> {
        schema::serial_length(self.len(), size_len, out)?;
        serial_vector_no_length(&self.as_bytes().to_vec(), out)
    }
}

impl SerialCtx for String {
    fn serial_ctx<W: Write>(
        &self,
        size_len: schema::SizeLength,
        out: &mut W,
    ) -> Result<(), W::Err> {
        self.as_str().serial_ctx(size_len, out)
    }
}

impl DeserialCtx for String {
    fn deserial_ctx<R: Read>(
        size_len: schema::SizeLength,
        _ensure_ordered: bool,
        source: &mut R,
    ) -> ParseResult<Self> {
        let len = schema::deserial_length(source, size_len)?;
        let bytes = deserial_vector_no_length(source, len)?;
        let res = String::from_utf8(bytes).map_err(|_| ParseError::default())?;
        Ok(res)
    }
}

/// Write a slice of elements, without including length information.
/// This is intended to be used either when the length is statically known,
/// or when the length is serialized independently as part of a bigger
/// structure.
pub fn serial_vector_no_length<W: Write, T: Serial>(xs: &[T], out: &mut W) -> Result<(), W::Err> {
    for x in xs {
        x.serial(out)?;
    }
    Ok(())
}

/// Read a vector given a length.
pub fn deserial_vector_no_length<R: Read, T: Deserial>(
    reader: &mut R,
    len: usize,
) -> ParseResult<Vec<T>> {
    let mut vec = Vec::with_capacity(core::cmp::min(len, MAX_PREALLOCATED_CAPACITY));
    for _ in 0..len {
        vec.push(T::deserial(reader)?);
    }
    Ok(vec)
}

/// Write a Map as a list of key-value pairs ordered by the key, without the
/// length information.
pub fn serial_map_no_length<W: Write, K: Serial, V: Serial>(
    map: &BTreeMap<K, V>,
    out: &mut W,
) -> Result<(), W::Err> {
    for (k, v) in map.iter() {
        k.serial(out)?;
        v.serial(out)?;
    }
    Ok(())
}

/// Read a [BTreeMap](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html) as a list of key-value pairs given some length.
/// NB: This ensures there are no duplicates, hence the specialized type.
/// Moreover this will only succeed if keys are listed in order.
pub fn deserial_map_no_length<R: Read, K: Deserial + Ord + Copy, 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()?;
        match x {
            None => {
                out.insert(k, v);
            }
            Some(kk) => {
                if k > kk {
                    out.insert(k, v);
                } else {
                    return Err(ParseError::default());
                }
            }
        }
        x = Some(k);
    }
    Ok(out)
}

/// Read a [BTreeMap](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html) as a list of key-value pairs given some length.
/// Slightly faster version of `deserial_map_no_length` as it is skipping the
/// order checking
pub fn deserial_map_no_length_no_order_check<R: Read, K: Deserial + Ord, V: Deserial>(
    source: &mut R,
    len: usize,
) -> ParseResult<BTreeMap<K, V>> {
    let mut out = BTreeMap::new();
    for _ in 0..len {
        let k = source.get()?;
        let v = source.get()?;
        if out.insert(k, v).is_some() {
            return Err(ParseError::default());
        }
    }
    Ok(out)
}

/// Write a HashMap as a list of key-value pairs in to particular order, without
/// the length information.
pub fn serial_hashmap_no_length<W: Write, K: Serial, V: Serial>(
    map: &HashMap<K, V>,
    out: &mut W,
) -> Result<(), W::Err> {
    for (k, v) in map.iter() {
        k.serial(out)?;
        v.serial(out)?;
    }
    Ok(())
}

/// Read a [HashMap](https://doc.rust-lang.org/std/collections/struct.HashMap.html) as a list of key-value pairs given some length.
pub fn deserial_hashmap_no_length<R: Read, K: Deserial + Eq + Hash, V: Deserial>(
    source: &mut R,
    len: usize,
) -> ParseResult<HashMap<K, V>> {
    let mut out = HashMap::default();
    for _ in 0..len {
        let k = source.get()?;
        let v = source.get()?;
        if out.insert(k, v).is_some() {
            return Err(ParseError::default());
        }
    }
    Ok(out)
}

/// Write a [BTreeSet](https://doc.rust-lang.org/std/collections/struct.BTreeSet.html) as an ascending list of keys, without the length information.
pub fn serial_set_no_length<W: Write, K: Serial>(
    map: &BTreeSet<K>,
    out: &mut W,
) -> Result<(), W::Err> {
    for k in map.iter() {
        k.serial(out)?;
    }
    Ok(())
}

/// Read a [BTreeSet](https://doc.rust-lang.org/std/collections/struct.BTreeSet.html) as a list of keys, given some length.
/// NB: This ensures there are no duplicates, hence the specialized type.
/// Moreover this will only succeed if keys are listed in order.
pub fn deserial_set_no_length<R: Read, K: Deserial + Ord + Copy>(
    source: &mut R,
    len: usize,
) -> ParseResult<BTreeSet<K>> {
    let mut out = BTreeSet::new();
    let mut prev = None;
    for _ in 0..len {
        let key = source.get()?;
        let next = Some(key);
        if next <= prev {
            return Err(ParseError::default());
        }
        out.insert(key);
        prev = next;
    }
    Ok(out)
}

/// Write a [HashSet](https://doc.rust-lang.org/std/collections/struct.HashSet.html) as a list of keys in no particular order, without the length information.
pub fn serial_hashset_no_length<W: Write, K: Serial>(
    map: &HashSet<K>,
    out: &mut W,
) -> Result<(), W::Err> {
    for k in map.iter() {
        k.serial(out)?;
    }
    Ok(())
}

/// Read a [HashSet](https://doc.rust-lang.org/std/collections/struct.HashSet.html) as a list of keys, given some length.
/// NB: This ensures there are no duplicates.
pub fn deserial_hashset_no_length<R: Read, K: Deserial + Eq + Hash>(
    source: &mut R,
    len: usize,
) -> ParseResult<HashSet<K>> {
    let mut out = HashSet::default();
    for _ in 0..len {
        let key = source.get()?;
        if !out.insert(key) {
            return Err(ParseError::default());
        }
    }
    Ok(out)
}

/// Read a [BTreeSet](https://doc.rust-lang.org/std/collections/struct.BTreeSet.html) as an list of key-value pairs given some length.
/// Slightly faster version of `deserial_set_no_length` as it is skipping the
/// order checking. The only check that is made to the set is that there are no
/// duplicates.
pub fn deserial_set_no_length_no_order_check<R: Read, K: Deserial + Ord>(
    source: &mut R,
    len: usize,
) -> ParseResult<BTreeSet<K>> {
    let mut out = BTreeSet::new();
    for _ in 0..len {
        let key = source.get()?;
        if !out.insert(key) {
            return Err(ParseError::default());
        }
    }
    Ok(out)
}

/// Serialized by writing an `u32` representing the number of elements, followed
/// by the elements serialized according to their type `T`.
impl<T: Serial> Serial for Vec<T> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let len = self.len() as u32;
        len.serial(out)?;
        serial_vector_no_length(self, out)
    }
}

/// Deserialized by reading an `u32` representing the number of elements, then
/// deserializing that many elements of type `T`.
impl<T: Deserial> Deserial for Vec<T> {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u32 = source.get()?;
        deserial_vector_no_length(source, len as usize)
    }
}

/// The serialization of maps encodes their size as a u32. This should be
/// sufficient for all realistic use cases in smart contracts.
/// They are serialized in ascending order.
impl<K: Serial + Ord, V: Serial> Serial for BTreeMap<K, V> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let len = self.len() as u32;
        len.serial(out)?;
        serial_map_no_length(self, out)
    }
}

/// The deserialization of maps assumes their size is a u32.
///
/// <b style="color: darkred">WARNING</b>: Deserialization **does not** ensure
/// the ordering of the keys, it only ensures that there are no duplicates.
/// Serializing a `BTreeMap` via its `Serial` instance will lay out elements
/// by the increasing order of keys. As a consequence deserializing, and
/// serializing back is in general not the identity. This could have
/// consequences if the data is hashed, or the byte representation
/// is used in some other way directly. In those cases the a canonical
/// order should be ensured to avoid subtle, difficult to diagnose,
/// bugs.
impl<K: Deserial + Ord, V: Deserial> Deserial for BTreeMap<K, V> {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u32 = source.get()?;
        deserial_map_no_length_no_order_check(source, len as usize)
    }
}

/// The serialization of maps encodes their size as a u32. This should be
/// sufficient for all realistic use cases in smart contracts.
/// They are serialized in no particular order.
impl<K: Serial, V: Serial> Serial for HashMap<K, V> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let len = self.len() as u32;
        len.serial(out)?;
        serial_hashmap_no_length(self, out)
    }
}

/// The deserialization of maps assumes their size is a u32.
///
/// <b style="color: darkred">WARNING</b>: Deserialization only ensures that
/// there are no duplicates. Serializing a `HashMap` via its `Serial` instance
/// will not lay out elements in a particular order. As a consequence
/// deserializing, and serializing back is in general not the identity. This
/// could have consequences if the data is hashed, or the byte representation
/// is used in some other way directly. In those cases use a `BTreeMap` instead.
impl<K: Deserial + Hash + Eq, V: Deserial> Deserial for HashMap<K, V> {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u32 = source.get()?;
        deserial_hashmap_no_length(source, len as usize)
    }
}

/// The serialization of sets encodes their size as a u32. This should be
/// sufficient for all realistic use cases in smart contracts.
/// They are serialized in canonical order (increasing)
impl<K: Serial + Ord> Serial for BTreeSet<K> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let len = self.len() as u32;
        len.serial(out)?;
        serial_set_no_length(self, out)
    }
}

/// The deserialization of sets assumes their size is a u32.
///
/// <b style="color: darkred">WARNING</b>: Deserialization **does not** ensure
/// the ordering of the keys, it only ensures that there are no duplicates.
/// Serializing a `BTreeSet` via its `Serial` instance will lay out elements
/// by the increasing order. As a consequence deserializing, and
/// serializing back is in general not the identity. This could have
/// consequences if the data is hashed, or the byte representation
/// is used in some other way directly. In those cases a canonical
/// order should be ensured to avoid subtle, difficult to diagnose,
/// bugs.
impl<K: Deserial + Ord> Deserial for BTreeSet<K> {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u32 = source.get()?;
        deserial_set_no_length_no_order_check(source, len as usize)
    }
}

// The serialization of sets encodes their size as a u32. This should be
/// sufficient for all realistic use cases in smart contracts.
/// They are serialized in no particular order.
impl<K: Serial> Serial for HashSet<K> {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        let len = self.len() as u32;
        len.serial(out)?;
        serial_hashset_no_length(self, out)
    }
}

/// The deserialization of sets assumes their size is a u32.
///
/// <b style="color: darkred">WARNING</b>: Deserialization only ensures that
/// there are no duplicates. Serializing a `HashSet` via its `Serial` instance
/// will not lay out elements in any particular order. As a consequence
/// deserializing, and serializing back is in general not the identity. This
/// could have consequences if the data is hashed, or the byte representation
/// is used in some other way directly. In those cases use a `BTreeSet` instead.
impl<K: Deserial + Hash + Eq> Deserial for HashSet<K> {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let len: u32 = source.get()?;
        deserial_hashset_no_length(source, len as usize)
    }
}

/// Serialize the array by writing elements consecutively starting at 0.
/// Since the length of the array is known statically it is not written out
/// explicitly. Thus serialization of the array A and the slice &A[..] differ.
impl<T: Serial, const N: usize> Serial for [T; N] {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        for elem in self.iter() {
            elem.serial(out)?;
        }
        Ok(())
    }
}

impl<T: Deserial, const N: usize> Deserial for [T; N] {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let mut data: [MaybeUninit<T>; N] = unsafe { MaybeUninit::uninit().assume_init() };
        for item in data.iter_mut() {
            *item = MaybeUninit::new(T::deserial(source)?);
        }
        Ok(unsafe { data.as_ptr().cast::<[T; N]>().read() })
    }
}

impl Address {
    pub fn matches_account(&self, acc: &AccountAddress) -> bool {
        if let Address::Account(ref my_acc) = self {
            my_acc == acc
        } else {
            false
        }
    }

    pub fn matches_contract(&self, cnt: &ContractAddress) -> bool {
        if let Address::Contract(ref my_cnt) = self {
            my_cnt == cnt
        } else {
            false
        }
    }
}

impl Serial for AttributeTag {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> { self.0.serial(out) }
}

impl Deserial for AttributeTag {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> { Ok(AttributeTag(source.get()?)) }
}

impl Serial for AttributeValue {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        out.write_all(&self.inner[..=self.len()]) // Writes the length (u8) +
                                                  // all the values.
    }
}

impl Deserial for AttributeValue {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let mut buf = [0u8; 32];
        let len: u8 = source.get()?;
        buf[0] = len;
        if len > 31 {
            return Err(ParseError::default());
        }
        source.read_exact(&mut buf[1..=len as usize])?;
        Ok(unsafe { AttributeValue::new_unchecked(buf) })
    }
}

impl Serial for OwnedPolicy {
    fn serial<W: Write>(&self, out: &mut W) -> Result<(), W::Err> {
        self.identity_provider.serial(out)?;
        self.created_at.serial(out)?;
        self.valid_to.serial(out)?;
        (self.items.len() as u16).serial(out)?;
        for item in self.items.iter() {
            item.0.serial(out)?;
            item.1.serial(out)?;
        }
        Ok(())
    }
}

impl Deserial for OwnedPolicy {
    fn deserial<R: Read>(source: &mut R) -> ParseResult<Self> {
        let identity_provider = source.get()?;
        let created_at = source.get()?;
        let valid_to = source.get()?;
        let len: u16 = source.get()?;
        let mut items = Vec::with_capacity(len as usize);
        for _ in 0..len {
            let tag = AttributeTag::deserial(source)?;
            let value = AttributeValue::deserial(source)?;
            items.push((tag, value))
        }
        Ok(Self {
            identity_provider,
            created_at,
            valid_to,
            items,
        })
    }
}

impl<T> Cursor<T> {
    pub fn new(data: T) -> Self {
        Cursor {
            offset: 0,
            data,
        }
    }
}

impl<T: AsRef<[u8]>> Read for Cursor<T> {
    fn read(&mut self, buf: &mut [u8]) -> ParseResult<usize> {
        let mut len = self.data.as_ref().len() - self.offset;
        if len > buf.len() {
            len = buf.len();
        }
        if len > 0 {
            buf[0..len].copy_from_slice(&self.data.as_ref()[self.offset..self.offset + len]);
            self.offset += len;
            Ok(len)
        } else {
            Ok(0)
        }
    }
}

impl<T: AsRef<[u8]>> HasSize for T {
    fn size(&self) -> u32 { self.as_ref().len() as u32 }
}

impl<T: AsRef<[u8]>> AsRef<[u8]> for Cursor<T> {
    fn as_ref(&self) -> &[u8] { self.data.as_ref() }
}

impl<T: HasSize> Seek for Cursor<T> {
    type Err = ();

    fn seek(&mut self, pos: SeekFrom) -> Result<u32, Self::Err> {
        use SeekFrom::*;
        let end = self.data.size();
        match pos {
            Start(offset) => {
                if offset <= end {
                    self.offset = offset as usize;
                    Ok(offset)
                } else {
                    Err(())
                }
            }
            End(delta) => {
                if delta > 0 {
                    Err(()) // cannot seek beyond the end
                } else {
                    // due to two's complement representation of values we do not have to
                    // distinguish on whether we go forward or backwards. Reinterpreting the bits
                    // and adding unsigned values is the same as subtracting the
                    // absolute value.
                    let new_offset = end.wrapping_add(delta as u32);
                    if new_offset <= end {
                        self.offset = new_offset as usize;
                        Ok(new_offset)
                    } else {
                        Err(())
                    }
                }
            }
            Current(delta) => {
                // due to two's complement representation of values we do not have to
                // distinguish on whether we go forward or backwards.
                let current_offset = u32::try_from(self.offset).map_err(|_| ())?;
                let new_offset: u32 = current_offset.wrapping_add(delta as u32);
                if new_offset <= end {
                    self.offset = new_offset as usize;
                    Ok(new_offset)
                } else {
                    Err(())
                }
            }
        }
    }

    #[inline(always)]
    fn cursor_position(&self) -> u32 { self.offset as u32 }
}

impl Write for Cursor<&mut Vec<u8>> {
    type Err = ();

    fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Err> {
        if buf.is_empty() {
            Ok(0)
        } else {
            // remaining capacity.
            let remaining_len = self.data.len() - self.offset;
            let (to_write, to_extend): (_, &[u8]) = {
                if remaining_len >= buf.len() {
                    (buf, &[])
                } else {
                    (&buf[..remaining_len], &buf[remaining_len..])
                }
            };
            self.data[self.offset..self.offset + to_write.len()].copy_from_slice(to_write);
            self.data.extend_from_slice(to_extend);
            self.offset += buf.len();
            Ok(buf.len())
        }
    }
}

/// Serialize the given value to a freshly allocated vector of bytes using
/// the provided `Serial` instance.
///
/// This should only be used as a helper function at the top-level, and not in
/// implementations of `Serial`.
pub fn to_bytes<S: Serial>(x: &S) -> Vec<u8> {
    let mut out = Vec::new();
    let mut cursor = Cursor::new(&mut out);
    x.serial(&mut cursor).expect("Writing to a vector should succeed.");
    out
}

/// Dual to `to_bytes`.
#[inline]
pub fn from_bytes<S: Deserial>(source: &[u8]) -> ParseResult<S> {
    let mut cursor = Cursor::new(source);
    cursor.get()
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_u64_array_deserial_serial_is_id() {
        let xs: [u64; 1] = [123];
        let bytes = to_bytes(&xs);
        let xs2: ParseResult<[u64; 1]> = from_bytes(&bytes);
        assert_eq!(
            xs,
            xs2.unwrap(),
            "Serializing and then deserializing should return original value."
        );
    }

    #[test]
    fn test_string_array_deserial_serial_is_id() {
        let xs: [String; 1] = ["hello".to_string()];
        let bytes = to_bytes(&xs);
        let xs2: ParseResult<[String; 1]> = from_bytes(&bytes);
        assert_eq!(
            xs,
            xs2.unwrap(),
            "Serializing and then deserializing should return original value."
        );
    }

    #[test]
    fn test_cursor_seek_start() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        let result = cursor.seek(SeekFrom::Start(8));
        let position = result.expect("Seek should succeed");

        assert_eq!(position, 8, "Seek moved to the wrong position");
    }

    #[test]
    fn test_cursor_seek_start_at_the_end() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        let result = cursor.seek(SeekFrom::Start(10));
        let position = result.expect("Seek should succeed");

        assert_eq!(position, 10, "Seek moved to the wrong position");
    }

    #[test]
    fn test_cursor_seek_start_fails_beyond_end() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        let result = cursor.seek(SeekFrom::Start(11));
        result.expect_err("Should have failed to seek beyond end of data");
    }

    #[test]
    fn test_cursor_seek_end() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        let result = cursor.seek(SeekFrom::End(-8));
        let position = result.expect("Seek should succeed");

        assert_eq!(position, 2, "Seek moved to the wrong position");
    }

    #[test]
    fn test_cursor_seek_end_at_the_start() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        let result = cursor.seek(SeekFrom::End(-10));
        let position = result.expect("Seek should succeed");

        assert_eq!(position, 0, "Seek moved to the wrong position");
    }

    #[test]
    fn test_cursor_seek_end_at_the_end() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        let result = cursor.seek(SeekFrom::End(0));
        let position = result.expect("Seek should succeed");

        assert_eq!(position, 10, "Seek moved to the wrong position");
    }

    #[test]
    fn test_cursor_seek_end_fails_before_start() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        let result = cursor.seek(SeekFrom::End(-11));
        result.expect_err("Should have failed to seek before start of data");
    }

    #[test]
    fn test_cursor_seek_end_fails_beyond_end() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        let result = cursor.seek(SeekFrom::End(1));
        result.expect_err("Should have failed to seek beyond end of data");
    }

    #[test]
    fn test_cursor_seek_current_forward_twice() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        let result = cursor.seek(SeekFrom::Current(4));
        let position = result.expect("Seek should succeed");
        assert_eq!(position, 4, "Seek moved to the wrong position");

        let result = cursor.seek(SeekFrom::Current(2));
        let position = result.expect("Seek should succeed");
        assert_eq!(position, 6, "Seek moved to the wrong position");
    }

    #[test]
    fn test_cursor_seek_current_forward_backward() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        cursor.seek(SeekFrom::Current(4)).expect("Seek should succeed");

        let result = cursor.seek(SeekFrom::Current(-2));
        let position = result.expect("Seek should succeed");
        assert_eq!(position, 2, "Seek moved to the wrong position");
    }

    #[test]
    fn test_cursor_seek_current_forward_backward_fail_before_start() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        cursor.seek(SeekFrom::Current(4)).expect("Seek should succeed");

        let result = cursor.seek(SeekFrom::Current(-5));
        result.expect_err("Should have failed to seek before start of data");
    }

    #[test]
    fn test_cursor_seek_current_forward_twice_fail_beyond_end() {
        let bytes = [0u8; 10];
        let mut cursor = Cursor::new(&bytes);

        cursor.seek(SeekFrom::Current(4)).expect("Seek should succeed");

        let result = cursor.seek(SeekFrom::Current(7));
        result.expect_err("Should have failed to seek beyond end of data");
    }

    #[test]
    fn test_owned_policy_serial_deserial_is_identity() {
        let op = OwnedPolicy {
            identity_provider: 1234,
            created_at:        Timestamp::from_timestamp_millis(11),
            valid_to:          Timestamp::from_timestamp_millis(999999),
            items:             vec![
                (attributes::COUNTRY_OF_RESIDENCE, b"DK".into()),
                (attributes::ID_DOC_TYPE, b"A document type with 31 chars..".into()),
            ],
        };
        let mut buf = Vec::new();
        op.serial(&mut buf).unwrap();
        let res = OwnedPolicy::deserial(&mut Cursor::new(buf)).unwrap();
        assert_eq!(op.identity_provider, res.identity_provider, "identity provider didn't match");
        assert_eq!(op.created_at, res.created_at, "created_at didn't match");
        assert_eq!(op.valid_to, res.valid_to, "valid_to didn't match");
        assert_eq!(op.items, res.items, "items didn't match");
    }
}