1use crate::alloc::vec::Vec;
18use crate::alloc::boxed::Box;
19use crate::alloc::collections::btree_map::BTreeMap;
20
21#[cfg(any(feature = "std", feature = "full"))]
22use crate::alloc::{
23 string::String,
24 borrow::{Cow, ToOwned},
25};
26
27use core::{mem, slice};
28use arrayvec::ArrayVec;
29use core::marker::PhantomData;
30
31pub trait Input {
33 fn read(&mut self, into: &mut [u8]) -> usize;
35
36 fn read_byte(&mut self) -> Option<u8> {
38 let mut buf = [0u8];
39 match self.read(&mut buf[..]) {
40 0 => None,
41 1 => Some(buf[0]),
42 _ => unreachable!(),
43 }
44 }
45}
46
47#[cfg(not(feature = "std"))]
48impl<'a> Input for &'a [u8] {
49 fn read(&mut self, into: &mut [u8]) -> usize {
50 let len = ::core::cmp::min(into.len(), self.len());
51 into[..len].copy_from_slice(&self[..len]);
52 *self = &self[len..];
53 len
54 }
55}
56
57#[cfg(feature = "std")]
58impl<R: std::io::Read> Input for R {
59 fn read(&mut self, into: &mut [u8]) -> usize {
60 match (self as &mut dyn std::io::Read).read_exact(into) {
61 Ok(()) => into.len(),
62 Err(_) => 0,
63 }
64 }
65}
66
67struct PrefixInput<'a, T> {
69 prefix: Option<u8>,
70 input: &'a mut T,
71}
72
73impl<'a, T: 'a + Input> Input for PrefixInput<'a, T> {
74 fn read(&mut self, buffer: &mut [u8]) -> usize {
75 match self.prefix.take() {
76 Some(v) if !buffer.is_empty() => {
77 buffer[0] = v;
78 1 + self.input.read(&mut buffer[1..])
79 }
80 _ => self.input.read(buffer)
81 }
82 }
83}
84
85pub trait Output: Sized {
87 fn write(&mut self, bytes: &[u8]);
89
90 fn push_byte(&mut self, byte: u8) {
91 self.write(&[byte]);
92 }
93
94 fn push<V: Encode + ?Sized>(&mut self, value: &V) {
95 value.encode_to(self);
96 }
97}
98
99#[cfg(not(feature = "std"))]
100impl Output for Vec<u8> {
101 fn write(&mut self, bytes: &[u8]) {
102 self.extend(bytes);
103 }
104}
105
106#[cfg(feature = "std")]
107impl<W: std::io::Write> Output for W {
108 fn write(&mut self, bytes: &[u8]) {
109 (self as &mut dyn std::io::Write).write_all(bytes).expect("Codec outputs are infallible");
110 }
111}
112
113struct ArrayVecWrapper<T: arrayvec::Array>(ArrayVec<T>);
114
115impl<T: arrayvec::Array<Item=u8>> Output for ArrayVecWrapper<T> {
116 fn write(&mut self, bytes: &[u8]) {
117 for byte in bytes {
118 self.push_byte(*byte);
119 }
120 }
121
122 fn push_byte(&mut self, byte: u8) {
123 self.0.push(byte);
124 }
125}
126
127pub trait Encode {
130 fn encode_to<T: Output>(&self, dest: &mut T) {
132 self.using_encoded(|buf| dest.write(buf));
133 }
134
135 fn encode(&self) -> Vec<u8> {
137 let mut r = Vec::new();
138 self.encode_to(&mut r);
139 r
140 }
141
142 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
144 f(&self.encode())
145 }
146}
147
148pub trait EncodeAppend {
151 type Item: Encode;
153
154 fn append(self_encoded: Vec<u8>, to_append: &[Self::Item]) -> Option<Vec<u8>>;
156}
157
158pub trait Decode: Sized {
160 fn decode<I: Input>(value: &mut I) -> Option<Self>;
162}
163
164pub trait Codec: Decode + Encode {}
166impl<S: Decode + Encode> Codec for S {}
167
168pub trait CompactLen<T> {
170 fn compact_len(val: &T) -> usize;
172}
173
174#[derive(Eq, PartialEq, Clone, Copy, Ord, PartialOrd)]
176pub struct Compact<T>(pub T);
177
178impl<T> From<T> for Compact<T> {
179 fn from(x: T) -> Compact<T> { Compact(x) }
180}
181
182impl<'a, T: Copy> From<&'a T> for Compact<T> {
183 fn from(x: &'a T) -> Compact<T> { Compact(*x) }
184}
185
186pub trait CompactAs: From<Compact<Self>> {
188 type As;
189 fn encode_as(&self) -> &Self::As;
190 fn decode_from(_: Self::As) -> Self;
191}
192
193impl<T> Encode for Compact<T>
194where
195 T: CompactAs,
196 for<'a> CompactRef<'a, <T as CompactAs>::As>: Encode,
197{
198 fn encode_to<W: Output>(&self, dest: &mut W) {
199 CompactRef(self.0.encode_as()).encode_to(dest)
200 }
201}
202
203impl<'a, T> Encode for CompactRef<'a, T>
204where
205 T: CompactAs,
206 for<'b> CompactRef<'b, <T as CompactAs>::As>: Encode,
207{
208 fn encode_to<Out: Output>(&self, dest: &mut Out) {
209 CompactRef(self.0.encode_as()).encode_to(dest)
210 }
211}
212
213impl<T> Decode for Compact<T>
214where
215 T: CompactAs,
216 Compact<<T as CompactAs>::As>: Decode,
217{
218 fn decode<I: Input>(input: &mut I) -> Option<Self> {
219 Compact::<T::As>::decode(input)
220 .map(|x| Compact(<T as CompactAs>::decode_from(x.0)))
221 }
222}
223
224macro_rules! impl_from_compact {
225 ( $( $ty:ty ),* ) => {
226 $(
227 impl From<Compact<$ty>> for $ty {
228 fn from(x: Compact<$ty>) -> $ty { x.0 }
229 }
230 )*
231 }
232}
233
234impl_from_compact! { (), u8, u16, u32, u64, u128 }
235
236#[derive(Eq, PartialEq, Clone, Copy)]
238pub struct CompactRef<'a, T>(pub &'a T);
239
240impl<'a, T> From<&'a T> for CompactRef<'a, T> {
241 fn from(x: &'a T) -> Self { CompactRef(x) }
242}
243
244impl<T> ::core::fmt::Debug for Compact<T> where T: ::core::fmt::Debug {
245 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
246 self.0.fmt(f)
247 }
248}
249
250#[cfg(feature = "std")]
251impl<T> serde::Serialize for Compact<T> where T: serde::Serialize {
252 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
253 T::serialize(&self.0, serializer)
254 }
255}
256
257#[cfg(feature = "std")]
258impl<'de, T> serde::Deserialize<'de> for Compact<T> where T: serde::Deserialize<'de> {
259 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
260 T::deserialize(deserializer).map(Compact)
261 }
262}
263
264#[cfg(feature = "std")]
265pub trait MaybeDebugSerde: core::fmt::Debug + serde::Serialize + for<'a> serde::Deserialize<'a> {}
266#[cfg(feature = "std")]
267impl<T> MaybeDebugSerde for T where T: core::fmt::Debug + serde::Serialize + for<'a> serde::Deserialize<'a> {}
268
269#[cfg(not(feature = "std"))]
270pub trait MaybeDebugSerde {}
271#[cfg(not(feature = "std"))]
272impl<T> MaybeDebugSerde for T {}
273
274pub trait HasCompact: Sized {
276 type Type: for<'a> EncodeAsRef<'a, Self> + Decode + From<Self> + Into<Self> + Clone +
278 PartialEq + Eq + MaybeDebugSerde;
279}
280
281pub trait EncodeAsRef<'a, T: 'a> {
283 type RefType: Encode + From<&'a T>;
285}
286
287impl<'a, T: 'a> EncodeAsRef<'a, T> for Compact<T> where CompactRef<'a, T>: Encode + From<&'a T> {
288 type RefType = CompactRef<'a, T>;
289}
290
291impl<T: 'static> HasCompact for T where
292 Compact<T>: for<'a> EncodeAsRef<'a, T> + Decode + From<Self> + Into<Self> + Clone +
293 PartialEq + Eq + MaybeDebugSerde,
294{
295 type Type = Compact<T>;
296}
297
298impl<'a> Encode for CompactRef<'a, ()> {
308 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
309 f(&[])
310 }
311}
312
313impl Encode for Compact<()> {
314 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
315 f(&[])
316 }
317}
318
319impl<'a> Encode for CompactRef<'a, u8> {
320 fn encode_to<W: Output>(&self, dest: &mut W) {
321 match self.0 {
322 0..=0b0011_1111 => dest.push_byte(self.0 << 2),
323 _ => ((u16::from(*self.0) << 2) | 0b01).encode_to(dest),
324 }
325 }
326
327 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
328 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 2]>::new());
329 self.encode_to(&mut r);
330 f(&r.0)
331 }
332}
333
334impl Encode for Compact<u8> {
335 fn encode_to<W: Output>(&self, dest: &mut W) {
336 CompactRef(&self.0).encode_to(dest)
337 }
338
339 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
340 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 2]>::new());
341 self.encode_to(&mut r);
342 f(&r.0)
343 }
344}
345
346impl CompactLen<u8> for Compact<u8> {
347 fn compact_len(val: &u8) -> usize {
348 match val {
349 0..=0b0011_1111 => 1,
350 _ => 2,
351 }
352 }
353}
354
355impl<'a> Encode for CompactRef<'a, u16> {
356 fn encode_to<W: Output>(&self, dest: &mut W) {
357 match self.0 {
358 0..=0b0011_1111 => dest.push_byte((*self.0 as u8) << 2),
359 0..=0b0011_1111_1111_1111 => ((*self.0 << 2) | 0b01).encode_to(dest),
360 _ => ((u32::from(*self.0) << 2) | 0b10).encode_to(dest),
361 }
362 }
363
364 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
365 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 4]>::new());
366 self.encode_to(&mut r);
367 f(&r.0)
368 }
369}
370
371impl Encode for Compact<u16> {
372 fn encode_to<W: Output>(&self, dest: &mut W) {
373 CompactRef(&self.0).encode_to(dest)
374 }
375
376 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
377 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 4]>::new());
378 self.encode_to(&mut r);
379 f(&r.0)
380 }
381}
382
383impl CompactLen<u16> for Compact<u16> {
384 fn compact_len(val: &u16) -> usize {
385 match val {
386 0..=0b0011_1111 => 1,
387 0..=0b0011_1111_1111_1111 => 2,
388 _ => 4,
389 }
390 }
391}
392
393impl<'a> Encode for CompactRef<'a, u32> {
394 fn encode_to<W: Output>(&self, dest: &mut W) {
395 match self.0 {
396 0..=0b0011_1111 => dest.push_byte((*self.0 as u8) << 2),
397 0..=0b0011_1111_1111_1111 => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
398 0..=0b0011_1111_1111_1111_1111_1111_1111_1111 => ((*self.0 << 2) | 0b10).encode_to(dest),
399 _ => {
400 dest.push_byte(0b11);
401 self.0.encode_to(dest);
402 }
403 }
404 }
405
406 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
407 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 5]>::new());
408 self.encode_to(&mut r);
409 f(&r.0)
410 }
411}
412
413impl Encode for Compact<u32> {
414 fn encode_to<W: Output>(&self, dest: &mut W) {
415 CompactRef(&self.0).encode_to(dest)
416 }
417
418 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
419 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 5]>::new());
420 self.encode_to(&mut r);
421 f(&r.0)
422 }
423}
424
425impl CompactLen<u32> for Compact<u32> {
426 fn compact_len(val: &u32) -> usize {
427 match val {
428 0..=0b0011_1111 => 1,
429 0..=0b0011_1111_1111_1111 => 2,
430 0..=0b0011_1111_1111_1111_1111_1111_1111_1111 => 4,
431 _ => 5,
432 }
433 }
434}
435
436impl<'a> Encode for CompactRef<'a, u64> {
437 fn encode_to<W: Output>(&self, dest: &mut W) {
438 match self.0 {
439 0..=0b0011_1111 => dest.push_byte((*self.0 as u8) << 2),
440 0..=0b0011_1111_1111_1111 => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
441 0..=0b0011_1111_1111_1111_1111_1111_1111_1111 => (((*self.0 as u32) << 2) | 0b10).encode_to(dest),
442 _ => {
443 let bytes_needed = 8 - self.0.leading_zeros() / 8;
444 assert!(bytes_needed >= 4, "Previous match arm matches anyting less than 2^30; qed");
445 dest.push_byte(0b11 + ((bytes_needed - 4) << 2) as u8);
446 let mut v = *self.0;
447 for _ in 0..bytes_needed {
448 dest.push_byte(v as u8);
449 v >>= 8;
450 }
451 assert_eq!(v, 0, "shifted sufficient bits right to lead only leading zeros; qed")
452 }
453 }
454 }
455
456 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
457 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 9]>::new());
458 self.encode_to(&mut r);
459 f(&r.0)
460 }
461}
462
463impl Encode for Compact<u64> {
464 fn encode_to<W: Output>(&self, dest: &mut W) {
465 CompactRef(&self.0).encode_to(dest)
466 }
467
468 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
469 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 9]>::new());
470 self.encode_to(&mut r);
471 f(&r.0)
472 }
473}
474
475impl CompactLen<u64> for Compact<u64> {
476 fn compact_len(val: &u64) -> usize {
477 match val {
478 0..=0b0011_1111 => 1,
479 0..=0b0011_1111_1111_1111 => 2,
480 0..=0b0011_1111_1111_1111_1111_1111_1111_1111 => 4,
481 _ => {
482 (8 - val.leading_zeros() / 8) as usize + 1
483 },
484 }
485 }
486}
487
488impl<'a> Encode for CompactRef<'a, u128> {
489 fn encode_to<W: Output>(&self, dest: &mut W) {
490 match self.0 {
491 0..=0b0011_1111 => dest.push_byte((*self.0 as u8) << 2),
492 0..=0b0011_1111_1111_1111 => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
493 0..=0b0011_1111_1111_1111_11111_111_1111_1111 => (((*self.0 as u32) << 2) | 0b10).encode_to(dest),
494 _ => {
495 let bytes_needed = 16 - self.0.leading_zeros() / 8;
496 assert!(bytes_needed >= 4, "Previous match arm matches anyting less than 2^30; qed");
497 dest.push_byte(0b11 + ((bytes_needed - 4) << 2) as u8);
498 let mut v = *self.0;
499 for _ in 0..bytes_needed {
500 dest.push_byte(v as u8);
501 v >>= 8;
502 }
503 assert_eq!(v, 0, "shifted sufficient bits right to lead only leading zeros; qed")
504 }
505 }
506 }
507
508 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
509 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 17]>::new());
510 self.encode_to(&mut r);
511 f(&r.0)
512 }
513}
514
515impl Encode for Compact<u128> {
516 fn encode_to<W: Output>(&self, dest: &mut W) {
517 CompactRef(&self.0).encode_to(dest)
518 }
519
520 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
521 let mut r = ArrayVecWrapper(ArrayVec::<[u8; 17]>::new());
522 self.encode_to(&mut r);
523 f(&r.0)
524 }
525}
526
527impl CompactLen<u128> for Compact<u128> {
528 fn compact_len(val: &u128) -> usize {
529 match val {
530 0..=0b0011_1111 => 1,
531 0..=0b0011_1111_1111_1111 => 2,
532 0..=0b0011_1111_1111_1111_1111_1111_1111_1111 => 4,
533 _ => {
534 (16 - val.leading_zeros() / 8) as usize + 1
535 },
536 }
537 }
538}
539
540impl Decode for Compact<()> {
541 fn decode<I: Input>(_input: &mut I) -> Option<Self> {
542 Some(Compact(()))
543 }
544}
545
546impl Decode for Compact<u8> {
547 fn decode<I: Input>(input: &mut I) -> Option<Self> {
548 let prefix = input.read_byte()?;
549 Some(Compact(match prefix % 4 {
550 0 => prefix as u8 >> 2,
551 1 => {
552 let x = u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2;
553 if x < 256 {
554 x as u8
555 } else {
556 return None
557 }
558 }
559 _ => return None,
560 }))
561 }
562}
563
564impl Decode for Compact<u16> {
565 fn decode<I: Input>(input: &mut I) -> Option<Self> {
566 let prefix = input.read_byte()?;
567 Some(Compact(match prefix % 4 {
568 0 => u16::from(prefix) >> 2,
569 1 => u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u16 >> 2,
570 2 => {
571 let x = u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2;
572 if x < 65536 {
573 x as u16
574 } else {
575 return None
576 }
577 }
578 _ => return None,
579 }))
580 }
581}
582
583impl Decode for Compact<u32> {
584 fn decode<I: Input>(input: &mut I) -> Option<Self> {
585 let prefix = input.read_byte()?;
586 Some(Compact(match prefix % 4 {
587 0 => u32::from(prefix) >> 2,
588 1 => u32::from(u16::decode(&mut PrefixInput{prefix: Some(prefix), input})?) >> 2,
589 2 => u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2,
590 3|_ => { if prefix >> 2 == 0 {
592 u32::decode(input)?
594 } else {
595 return None
597 }
598 }
599 }))
600 }
601}
602
603impl Decode for Compact<u64> {
604 fn decode<I: Input>(input: &mut I) -> Option<Self> {
605 let prefix = input.read_byte()?;
606 Some(Compact(match prefix % 4 {
607 0 => u64::from(prefix) >> 2,
608 1 => u64::from(u16::decode(&mut PrefixInput{prefix: Some(prefix), input})?) >> 2,
609 2 => u64::from(u32::decode(&mut PrefixInput{prefix: Some(prefix), input})?) >> 2,
610 3|_ => match (prefix >> 2) + 4 {
611 4 => u64::from(u32::decode(input)?),
612 8 => u64::decode(input)?,
613 x if x > 8 => return None,
614 bytes_needed => {
615 let mut res = 0;
616 for i in 0..bytes_needed {
617 res |= u64::from(input.read_byte()?) << (i * 8);
618 }
619 res
620 }
621 }
622 }))
623 }
624}
625
626impl Decode for Compact<u128> {
627 fn decode<I: Input>(input: &mut I) -> Option<Self> {
628 let prefix = input.read_byte()?;
629 Some(Compact(match prefix % 4 {
630 0 => u128::from(prefix) >> 2,
631 1 => u128::from(u16::decode(&mut PrefixInput{prefix: Some(prefix), input})?) >> 2,
632 2 => u128::from(u32::decode(&mut PrefixInput{prefix: Some(prefix), input})?) >> 2,
633 3|_ => match (prefix >> 2) + 4 {
634 4 => u128::from(u32::decode(input)?),
635 8 => u128::from(u64::decode(input)?),
636 16 => u128::decode(input)?,
637 x if x > 16 => return None,
638 bytes_needed => {
639 let mut res = 0;
640 for i in 0..bytes_needed {
641 res |= u128::from(input.read_byte()?) << (i * 8);
642 }
643 res
644 }
645 }
646 }))
647 }
648}
649
650impl<T: Encode, E: Encode> Encode for Result<T, E> {
651 fn encode_to<W: Output>(&self, dest: &mut W) {
652 match *self {
653 Ok(ref t) => {
654 dest.push_byte(0);
655 t.encode_to(dest);
656 }
657 Err(ref e) => {
658 dest.push_byte(1);
659 e.encode_to(dest);
660 }
661 }
662 }
663}
664
665impl<T: Decode, E: Decode> Decode for Result<T, E> {
666 fn decode<I: Input>(input: &mut I) -> Option<Self> {
667 match input.read_byte()? {
668 0 => Some(Ok(T::decode(input)?)),
669 1 => Some(Err(E::decode(input)?)),
670 _ => None,
671 }
672 }
673}
674
675#[derive(Eq, PartialEq, Clone, Copy)]
677pub struct OptionBool(pub Option<bool>);
678
679impl core::fmt::Debug for OptionBool {
680 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
681 self.0.fmt(f)
682 }
683}
684
685impl Encode for OptionBool {
686 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
687 f(&[match *self {
688 OptionBool(None) => 0u8,
689 OptionBool(Some(true)) => 1u8,
690 OptionBool(Some(false)) => 2u8,
691 }])
692 }
693}
694
695impl Decode for OptionBool {
696 fn decode<I: Input>(input: &mut I) -> Option<Self> {
697 match input.read_byte()? {
698 0 => Some(OptionBool(None)),
699 1 => Some(OptionBool(Some(true))),
700 2 => Some(OptionBool(Some(false))),
701 _ => None,
702 }
703 }
704}
705
706impl<T: Encode> Encode for Option<T> {
707 fn encode_to<W: Output>(&self, dest: &mut W) {
708 match *self {
709 Some(ref t) => {
710 dest.push_byte(1);
711 t.encode_to(dest);
712 }
713 None => dest.push_byte(0),
714 }
715 }
716}
717
718impl<T: Decode> Decode for Option<T> {
719 fn decode<I: Input>(input: &mut I) -> Option<Self> {
720 match input.read_byte()? {
721 0 => Some(None),
722 1 => Some(Some(T::decode(input)?)),
723 _ => None,
724 }
725 }
726}
727
728macro_rules! impl_array {
729 ( $( $n:expr )* ) => { $(
730 impl<T: Encode> Encode for [T; $n] {
731 fn encode_to<W: Output>(&self, dest: &mut W) {
732 for item in self.iter() {
733 item.encode_to(dest);
734 }
735 }
736 }
737
738 impl<T: Decode> Decode for [T; $n] {
739 fn decode<I: Input>(input: &mut I) -> Option<Self> {
740 let mut r = ArrayVec::new();
741 for _ in 0..$n {
742 r.push(T::decode(input)?);
743 }
744 r.into_inner().ok()
745 }
746 }
747 )* }
748}
749
750impl_array!(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
751 40 48 56 64 72 96 128 160 192 224 256);
752
753impl<T: Encode> Encode for Box<T> {
754 fn encode_to<W: Output>(&self, dest: &mut W) {
755 self.as_ref().encode_to(dest)
756 }
757}
758
759impl<T: Decode> Decode for Box<T> {
760 fn decode<I: Input>(input: &mut I) -> Option<Self> {
761 Some(Box::new(T::decode(input)?))
762 }
763}
764
765impl Encode for [u8] {
766 fn encode_to<W: Output>(&self, dest: &mut W) {
767 let len = self.len();
768 assert!(len <= u32::max_value() as usize, "Attempted to serialize a collection with too many elements.");
769 Compact(len as u32).encode_to(dest);
770 dest.write(self)
771 }
772}
773
774impl Encode for Vec<u8> {
775 fn encode_to<W: Output>(&self, dest: &mut W) {
776 self.as_slice().encode_to(dest)
777 }
778}
779
780impl Decode for Vec<u8> {
781 fn decode<I: Input>(input: &mut I) -> Option<Self> {
782 <Compact<u32>>::decode(input).and_then(move |Compact(len)| {
783 let len = len as usize;
784 let mut vec = vec![0; len];
785 if input.read(&mut vec[..len]) != len {
786 None
787 } else {
788 Some(vec)
789 }
790 })
791 }
792}
793
794impl<'a> Encode for &'a str {
795 fn encode_to<W: Output>(&self, dest: &mut W) {
796 self.as_bytes().encode_to(dest)
797 }
798}
799
800#[cfg(any(feature = "std", feature = "full"))]
801impl<'a, T: ToOwned + ?Sized + 'a> Encode for Cow<'a, T> where
802 &'a T: Encode,
803 <T as ToOwned>::Owned: Encode
804{
805 fn encode_to<W: Output>(&self, dest: &mut W) {
806 match self {
807 Cow::Owned(ref x) => x.encode_to(dest),
808 Cow::Borrowed(x) => x.encode_to(dest),
809 }
810 }
811}
812
813#[cfg(any(feature = "std", feature = "full"))]
814impl<'a, T: ToOwned + ?Sized> Decode for Cow<'a, T>
815 where <T as ToOwned>::Owned: Decode,
816{
817 fn decode<I: Input>(input: &mut I) -> Option<Self> {
818 Some(Cow::Owned(Decode::decode(input)?))
819 }
820}
821
822impl<T> Encode for PhantomData<T> {
823 fn encode_to<W: Output>(&self, _dest: &mut W) {
824 }
825}
826
827impl<T> Decode for PhantomData<T> {
828 fn decode<I: Input>(_input: &mut I) -> Option<Self> {
829 Some(PhantomData)
830 }
831}
832
833#[cfg(any(feature = "std", feature = "full"))]
834impl Encode for String {
835 fn encode_to<W: Output>(&self, dest: &mut W) {
836 self.as_bytes().encode_to(dest)
837 }
838}
839
840#[cfg(any(feature = "std", feature = "full"))]
841impl Decode for String {
842 fn decode<I: Input>(input: &mut I) -> Option<Self> {
843 Some(Self::from_utf8_lossy(&Vec::decode(input)?).into())
844 }
845}
846
847impl<T: Encode> Encode for [T] {
848 fn encode_to<W: Output>(&self, dest: &mut W) {
849 let len = self.len();
850 assert!(len <= u32::max_value() as usize, "Attempted to serialize a collection with too many elements.");
851 Compact(len as u32).encode_to(dest);
852 for item in self {
853 item.encode_to(dest);
854 }
855 }
856}
857
858impl<T: Encode> Encode for Vec<T> {
859 fn encode_to<W: Output>(&self, dest: &mut W) {
860 self.as_slice().encode_to(dest)
861 }
862}
863
864impl<T: Decode> Decode for Vec<T> {
865 fn decode<I: Input>(input: &mut I) -> Option<Self> {
866 <Compact<u32>>::decode(input).and_then(move |Compact(len)| {
867 let mut r = Vec::with_capacity(len as usize);
868 for _ in 0..len {
869 r.push(T::decode(input)?);
870 }
871 Some(r)
872 })
873 }
874}
875
876impl<T: Encode + Decode> EncodeAppend for Vec<T> {
877 type Item = T;
878
879 fn append(mut self_encoded: Vec<u8>, to_append: &[Self::Item]) -> Option<Vec<u8>> {
880 if self_encoded.is_empty() {
881 return Some(to_append.encode())
882 }
883
884 let len = u32::from(Compact::<u32>::decode(&mut &self_encoded[..])?);
885 let new_len = len.checked_add(to_append.len() as u32)?;
886
887 let encoded_len = Compact::<u32>::compact_len(&len);
888 let encoded_new_len = Compact::<u32>::compact_len(&new_len);
889
890 let replace_len = |dest: &mut Vec<u8>| {
891 Compact(new_len).using_encoded(|e| {
892 dest[..encoded_new_len].copy_from_slice(e);
893 })
894 };
895
896 let append_new_elems = |dest: &mut Vec<u8>| to_append.iter().for_each(|a| a.encode_to(dest));
897
898 if encoded_len == encoded_new_len {
901 replace_len(&mut self_encoded);
902 append_new_elems(&mut self_encoded);
903
904 Some(self_encoded)
905 } else {
906 let prefix_size = encoded_new_len + self_encoded.len() - encoded_len;
907
908 let mut res = Vec::with_capacity(prefix_size);
909 unsafe { res.set_len(prefix_size); }
910
911 replace_len(&mut res);
914 res[encoded_new_len..prefix_size].copy_from_slice(&self_encoded[encoded_len..]);
915 append_new_elems(&mut res);
916
917 Some(res)
918 }
919 }
920}
921
922impl<K: Encode + Ord, V: Encode> Encode for BTreeMap<K, V> {
923 fn encode_to<W: Output>(&self, dest: &mut W) {
924 let len = self.len();
925 assert!(len <= u32::max_value() as usize, "Attempted to serialize a collection with too many elements.");
926 (len as u32).encode_to(dest);
927 for i in self.iter() {
928 i.encode_to(dest);
929 }
930 }
931}
932
933impl<K: Decode + Ord, V: Decode> Decode for BTreeMap<K, V> {
934 fn decode<I: Input>(input: &mut I) -> Option<Self> {
935 u32::decode(input).and_then(move |len| {
936 let mut r: BTreeMap<K, V> = BTreeMap::new();
937 for _ in 0..len {
938 let (key, v) = <(K, V)>::decode(input)?;
939 r.insert(key, v);
940 }
941 Some(r)
942 })
943 }
944}
945
946impl Encode for () {
947 fn encode_to<T: Output>(&self, _dest: &mut T) {
948 }
949
950 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
951 f(&[])
952 }
953
954 fn encode(&self) -> Vec<u8> {
955 Vec::new()
956 }
957}
958
959impl<'a, T: 'a + Encode + ?Sized> Encode for &'a T {
960 fn encode_to<D: Output>(&self, dest: &mut D) {
961 (&**self).encode_to(dest)
962 }
963
964 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
965 (&**self).using_encoded(f)
966 }
967
968 fn encode(&self) -> Vec<u8> {
969 (&**self).encode()
970 }
971}
972
973impl Decode for () {
974 fn decode<I: Input>(_: &mut I) -> Option<()> {
975 Some(())
976 }
977}
978
979macro_rules! tuple_impl {
980 ($one:ident,) => {
981 impl<$one: Encode> Encode for ($one,) {
982 fn encode_to<T: Output>(&self, dest: &mut T) {
983 self.0.encode_to(dest);
984 }
985 }
986
987 impl<$one: Decode> Decode for ($one,) {
988 fn decode<I: Input>(input: &mut I) -> Option<Self> {
989 match $one::decode(input) {
990 None => None,
991 Some($one) => Some(($one,)),
992 }
993 }
994 }
995 };
996 ($first:ident, $($rest:ident,)+) => {
997 impl<$first: Encode, $($rest: Encode),+>
998 Encode for
999 ($first, $($rest),+) {
1000 fn encode_to<T: Output>(&self, dest: &mut T) {
1001 let (
1002 ref $first,
1003 $(ref $rest),+
1004 ) = *self;
1005
1006 $first.encode_to(dest);
1007 $($rest.encode_to(dest);)+
1008 }
1009 }
1010
1011 impl<$first: Decode, $($rest: Decode),+>
1012 Decode for
1013 ($first, $($rest),+) {
1014 fn decode<INPUT: Input>(input: &mut INPUT) -> Option<Self> {
1015 Some((
1016 match $first::decode(input) {
1017 Some(x) => x,
1018 None => return None,
1019 },
1020 $(match $rest::decode(input) {
1021 Some(x) => x,
1022 None => return None,
1023 },)+
1024 ))
1025 }
1026 }
1027
1028 tuple_impl!($($rest,)+);
1029 }
1030}
1031
1032#[allow(non_snake_case)]
1033mod inner_tuple_impl {
1034 use super::{Input, Output, Decode, Encode};
1035 tuple_impl!(A, B, C, D, E, F, G, H, I, J, K,);
1036}
1037
1038trait EndianSensitive: Copy + 'static {
1043 fn to_le(self) -> Self { self }
1044 fn to_be(self) -> Self { self }
1045 fn from_le(self) -> Self { self }
1046 fn from_be(self) -> Self { self }
1047 fn as_be_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { f(&self) }
1048 fn as_le_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { f(&self) }
1049}
1050
1051macro_rules! impl_endians {
1052 ( $( $t:ty ),* ) => { $(
1053 impl EndianSensitive for $t {
1054 fn to_le(self) -> Self { <$t>::to_le(self) }
1055 fn to_be(self) -> Self { <$t>::to_be(self) }
1056 fn from_le(self) -> Self { <$t>::from_le(self) }
1057 fn from_be(self) -> Self { <$t>::from_be(self) }
1058 fn as_be_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { let d = self.to_be(); f(&d) }
1059 fn as_le_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { let d = self.to_le(); f(&d) }
1060 }
1061
1062 impl Encode for $t {
1063 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1064 self.as_le_then(|le| {
1065 let size = mem::size_of::<$t>();
1066 let value_slice = unsafe {
1067 let ptr = le as *const _ as *const u8;
1068 if size != 0 {
1069 slice::from_raw_parts(ptr, size)
1070 } else {
1071 &[]
1072 }
1073 };
1074
1075 f(value_slice)
1076 })
1077 }
1078 }
1079
1080 impl Decode for $t {
1081 fn decode<I: Input>(input: &mut I) -> Option<Self> {
1082 let size = mem::size_of::<$t>();
1083 assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type.");
1084 let mut val: $t = unsafe { mem::zeroed() };
1085
1086 unsafe {
1087 let raw: &mut [u8] = slice::from_raw_parts_mut(
1088 &mut val as *mut $t as *mut u8,
1089 size
1090 );
1091 if input.read(raw) != size { return None }
1092 }
1093 Some(val.from_le())
1094 }
1095 }
1096 )* }
1097}
1098macro_rules! impl_non_endians {
1099 ( $( $t:ty ),* ) => { $(
1100 impl EndianSensitive for $t {}
1101
1102 impl Encode for $t {
1103 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
1104 self.as_le_then(|le| {
1105 let size = mem::size_of::<$t>();
1106 let value_slice = unsafe {
1107 let ptr = le as *const _ as *const u8;
1108 if size != 0 {
1109 slice::from_raw_parts(ptr, size)
1110 } else {
1111 &[]
1112 }
1113 };
1114
1115 f(value_slice)
1116 })
1117 }
1118 }
1119
1120 impl Decode for $t {
1121 fn decode<I: Input>(input: &mut I) -> Option<Self> {
1122 let size = mem::size_of::<$t>();
1123 assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type.");
1124 let mut val: $t = unsafe { mem::zeroed() };
1125
1126 unsafe {
1127 let raw: &mut [u8] = slice::from_raw_parts_mut(
1128 &mut val as *mut $t as *mut u8,
1129 size
1130 );
1131 if input.read(raw) != size { return None }
1132 }
1133 Some(val.from_le())
1134 }
1135 }
1136 )* }
1137}
1138
1139impl_endians!(u16, u32, u64, u128, usize, i16, i32, i64, i128, isize);
1140impl_non_endians!(
1141 i8, [u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8],
1142 [u8; 10], [u8; 12], [u8; 14], [u8; 16], [u8; 20], [u8; 24], [u8; 28],
1143 [u8; 32], [u8; 40], [u8; 48], [u8; 56], [u8; 64], [u8; 80], [u8; 96],
1144 [u8; 112], [u8; 128], bool
1145);
1146
1147
1148#[cfg(test)]
1149mod tests {
1150 use super::*;
1151 use std::borrow::Cow;
1152
1153 #[test]
1154 fn vec_is_slicable() {
1155 let v = b"Hello world".to_vec();
1156 v.using_encoded(|ref slice|
1157 assert_eq!(slice, &b"\x2cHello world")
1158 );
1159 }
1160
1161 #[test]
1162 fn btree_map_works() {
1163 let mut m: BTreeMap<u32, Vec<u8>> = BTreeMap::new();
1164 m.insert(1, b"qwe".to_vec());
1165 m.insert(2, b"qweasd".to_vec());
1166 let encoded = m.encode();
1167
1168 assert_eq!(m, Decode::decode(&mut &encoded[..]).unwrap());
1169
1170 let mut m: BTreeMap<Vec<u8>, Vec<u8>> = BTreeMap::new();
1171 m.insert(b"123".to_vec(), b"qwe".to_vec());
1172 m.insert(b"1234".to_vec(), b"qweasd".to_vec());
1173 let encoded = m.encode();
1174
1175 assert_eq!(m, Decode::decode(&mut &encoded[..]).unwrap());
1176
1177 let mut m: BTreeMap<Vec<u32>, Vec<u8>> = BTreeMap::new();
1178 m.insert(vec![1, 2, 3], b"qwe".to_vec());
1179 m.insert(vec![1, 2], b"qweasd".to_vec());
1180 let encoded = m.encode();
1181
1182 assert_eq!(m, Decode::decode(&mut &encoded[..]).unwrap());
1183 }
1184
1185 #[test]
1186 fn encode_borrowed_tuple() {
1187 let x = vec![1u8, 2, 3, 4];
1188 let y = 128i64;
1189
1190 let encoded = (&x, &y).encode();
1191
1192 assert_eq!((x, y), Decode::decode(&mut &encoded[..]).unwrap());
1193 }
1194
1195 #[test]
1196 fn cow_works() {
1197 let x = &[1u32, 2, 3, 4, 5, 6][..];
1198 let y = Cow::Borrowed(&x);
1199 assert_eq!(x.encode(), y.encode());
1200
1201 let z: Cow<'_, [u32]> = Cow::decode(&mut &x.encode()[..]).unwrap();
1202 assert_eq!(*z, *x);
1203 }
1204
1205 #[test]
1206 fn cow_string_works() {
1207 let x = "Hello world!";
1208 let y = Cow::Borrowed(&x);
1209 assert_eq!(x.encode(), y.encode());
1210
1211 let z: Cow<'_, str> = Cow::decode(&mut &x.encode()[..]).unwrap();
1212 assert_eq!(*z, *x);
1213 }
1214
1215 #[test]
1216 fn compact_128_encoding_works() {
1217 let tests = [
1218 (0u128, 1usize), (63, 1), (64, 2), (16383, 2),
1219 (16384, 4), (1073741823, 4),
1220 (1073741824, 5), ((1 << 32) - 1, 5),
1221 (1 << 32, 6), (1 << 40, 7), (1 << 48, 8), ((1 << 56) - 1, 8), (1 << 56, 9), ((1 << 64) - 1, 9),
1222 (1 << 64, 10), (1 << 72, 11), (1 << 80, 12), (1 << 88, 13), (1 << 96, 14), (1 << 104, 15),
1223 (1 << 112, 16), ((1 << 120) - 1, 16), (1 << 120, 17), (u128::max_value(), 17)
1224 ];
1225 for &(n, l) in &tests {
1226 let encoded = Compact(n as u128).encode();
1227 assert_eq!(encoded.len(), l);
1228 assert_eq!(Compact::compact_len(&n), l);
1229 assert_eq!(<Compact<u128>>::decode(&mut &encoded[..]).unwrap().0, n);
1230 }
1231 }
1232
1233 #[test]
1234 fn compact_64_encoding_works() {
1235 let tests = [
1236 (0u64, 1usize), (63, 1), (64, 2), (16383, 2),
1237 (16384, 4), (1073741823, 4),
1238 (1073741824, 5), ((1 << 32) - 1, 5),
1239 (1 << 32, 6), (1 << 40, 7), (1 << 48, 8), ((1 << 56) - 1, 8), (1 << 56, 9), (u64::max_value(), 9)
1240 ];
1241 for &(n, l) in &tests {
1242 let encoded = Compact(n as u64).encode();
1243 assert_eq!(encoded.len(), l);
1244 assert_eq!(Compact::compact_len(&n), l);
1245 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n);
1246 }
1247 }
1248
1249 #[test]
1250 fn compact_32_encoding_works() {
1251 let tests = [(0u32, 1usize), (63, 1), (64, 2), (16383, 2), (16384, 4), (1073741823, 4), (1073741824, 5), (u32::max_value(), 5)];
1252 for &(n, l) in &tests {
1253 let encoded = Compact(n as u32).encode();
1254 assert_eq!(encoded.len(), l);
1255 assert_eq!(Compact::compact_len(&n), l);
1256 assert_eq!(<Compact<u32>>::decode(&mut &encoded[..]).unwrap().0, n);
1257 }
1258 }
1259
1260 #[test]
1261 fn compact_16_encoding_works() {
1262 let tests = [(0u16, 1usize), (63, 1), (64, 2), (16383, 2), (16384, 4), (65535, 4)];
1263 for &(n, l) in &tests {
1264 let encoded = Compact(n as u16).encode();
1265 assert_eq!(encoded.len(), l);
1266 assert_eq!(Compact::compact_len(&n), l);
1267 assert_eq!(<Compact<u16>>::decode(&mut &encoded[..]).unwrap().0, n);
1268 }
1269 assert!(<Compact<u16>>::decode(&mut &Compact(65536u32).encode()[..]).is_none());
1270 }
1271
1272 #[test]
1273 fn compact_8_encoding_works() {
1274 let tests = [(0u8, 1usize), (63, 1), (64, 2), (255, 2)];
1275 for &(n, l) in &tests {
1276 let encoded = Compact(n as u8).encode();
1277 assert_eq!(encoded.len(), l);
1278 assert_eq!(Compact::compact_len(&n), l);
1279 assert_eq!(<Compact<u8>>::decode(&mut &encoded[..]).unwrap().0, n);
1280 }
1281 assert!(<Compact<u8>>::decode(&mut &Compact(256u32).encode()[..]).is_none());
1282 }
1283
1284 fn hexify(bytes: &[u8]) -> String {
1285 bytes.iter().map(|ref b| format!("{:02x}", b)).collect::<Vec<String>>().join(" ")
1286 }
1287
1288 #[test]
1289 fn string_encoded_as_expected() {
1290 let value = String::from("Hello, World!");
1291 let encoded = value.encode();
1292 assert_eq!(hexify(&encoded), "34 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21");
1293 assert_eq!(<String>::decode(&mut &encoded[..]).unwrap(), value);
1294 }
1295
1296 #[test]
1297 fn vec_of_u8_encoded_as_expected() {
1298 let value = vec![0u8, 1, 1, 2, 3, 5, 8, 13, 21, 34];
1299 let encoded = value.encode();
1300 assert_eq!(hexify(&encoded), "28 00 01 01 02 03 05 08 0d 15 22");
1301 assert_eq!(<Vec<u8>>::decode(&mut &encoded[..]).unwrap(), value);
1302 }
1303
1304 #[test]
1305 fn vec_of_i16_encoded_as_expected() {
1306 let value = vec![0i16, 1, -1, 2, -2, 3, -3];
1307 let encoded = value.encode();
1308 assert_eq!(hexify(&encoded), "1c 00 00 01 00 ff ff 02 00 fe ff 03 00 fd ff");
1309 assert_eq!(<Vec<i16>>::decode(&mut &encoded[..]).unwrap(), value);
1310 }
1311
1312 #[test]
1313 fn vec_of_option_int_encoded_as_expected() {
1314 let value = vec![Some(1i8), Some(-1), None];
1315 let encoded = value.encode();
1316 assert_eq!(hexify(&encoded), "0c 01 01 01 ff 00");
1317 assert_eq!(<Vec<Option<i8>>>::decode(&mut &encoded[..]).unwrap(), value);
1318 }
1319
1320 #[test]
1321 fn vec_of_option_bool_encoded_as_expected() {
1322 let value = vec![OptionBool(Some(true)), OptionBool(Some(false)), OptionBool(None)];
1323 let encoded = value.encode();
1324 assert_eq!(hexify(&encoded), "0c 01 02 00");
1325 assert_eq!(<Vec<OptionBool>>::decode(&mut &encoded[..]).unwrap(), value);
1326 }
1327
1328 #[test]
1329 fn vec_encode_append_works() {
1330 let max_value = 1_000_000;
1331
1332 let encoded = (0..max_value).fold(Vec::new(), |encoded, v| {
1333 <Vec<u32> as EncodeAppend>::append(encoded, &[v]).unwrap()
1334 });
1335
1336 let decoded = Vec::<u32>::decode(&mut &encoded[..]).unwrap();
1337 assert_eq!(decoded, (0..max_value).collect::<Vec<_>>());
1338 }
1339
1340 #[test]
1341 fn vec_encode_append_multiple_items_works() {
1342 let max_value = 1_000_000;
1343
1344 let encoded = (0..max_value).fold(Vec::new(), |encoded, v| {
1345 <Vec<u32> as EncodeAppend>::append(encoded, &[v, v, v, v]).unwrap()
1346 });
1347
1348 let decoded = Vec::<u32>::decode(&mut &encoded[..]).unwrap();
1349 let expected = (0..max_value).fold(Vec::new(), |mut vec, i| {
1350 vec.append(&mut vec![i, i, i, i]);
1351 vec
1352 });
1353 assert_eq!(decoded, expected);
1354 }
1355
1356 #[test]
1357 fn vec_of_string_encoded_as_expected() {
1358 let value = vec![
1359 "Hamlet".to_owned(),
1360 "Война и мир".to_owned(),
1361 "三国演义".to_owned(),
1362 "أَلْف لَيْلَة وَلَيْلَة".to_owned()
1363 ];
1364 let encoded = value.encode();
1365 assert_eq!(hexify(&encoded), "10 18 48 61 6d 6c 65 74 50 d0 92 d0 be d0 b9 d0 bd d0 b0 20 d0 \
1366 b8 20 d0 bc d0 b8 d1 80 30 e4 b8 89 e5 9b bd e6 bc 94 e4 b9 89 bc d8 a3 d9 8e d9 84 d9 92 \
1367 d9 81 20 d9 84 d9 8e d9 8a d9 92 d9 84 d9 8e d8 a9 20 d9 88 d9 8e d9 84 d9 8e d9 8a d9 92 \
1368 d9 84 d9 8e d8 a9 e2 80 8e");
1369 assert_eq!(<Vec<String>>::decode(&mut &encoded[..]).unwrap(), value);
1370 }
1371
1372 #[test]
1373 fn compact_integers_encoded_as_expected() {
1374 let tests = [
1375 (0u64, "00"),
1376 (63, "fc"),
1377 (64, "01 01"),
1378 (16383, "fd ff"),
1379 (16384, "02 00 01 00"),
1380 (1073741823, "fe ff ff ff"),
1381 (1073741824, "03 00 00 00 40"),
1382 ((1 << 32) - 1, "03 ff ff ff ff"),
1383 (1 << 32, "07 00 00 00 00 01"),
1384 (1 << 40, "0b 00 00 00 00 00 01"),
1385 (1 << 48, "0f 00 00 00 00 00 00 01"),
1386 ((1 << 56) - 1, "0f ff ff ff ff ff ff ff"),
1387 (1 << 56, "13 00 00 00 00 00 00 00 01"),
1388 (u64::max_value(), "13 ff ff ff ff ff ff ff ff")
1389 ];
1390 for &(n, s) in &tests {
1391 let encoded = Compact(n as u64).encode();
1393 assert_eq!(hexify(&encoded), s);
1394 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n);
1395
1396 if n <= u32::max_value() as u64 {
1398 assert_eq!(<Compact<u32>>::decode(&mut &encoded[..]).unwrap().0, n as u32);
1399 let encoded = Compact(n as u32).encode();
1400 assert_eq!(hexify(&encoded), s);
1401 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n as u64);
1402 }
1403 if n <= u16::max_value() as u64 {
1404 assert_eq!(<Compact<u16>>::decode(&mut &encoded[..]).unwrap().0, n as u16);
1405 let encoded = Compact(n as u16).encode();
1406 assert_eq!(hexify(&encoded), s);
1407 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n as u64);
1408 }
1409 if n <= u8::max_value() as u64 {
1410 assert_eq!(<Compact<u8>>::decode(&mut &encoded[..]).unwrap().0, n as u8);
1411 let encoded = Compact(n as u8).encode();
1412 assert_eq!(hexify(&encoded), s);
1413 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n as u64);
1414 }
1415 }
1416 }
1417
1418 #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
1419 #[derive(PartialEq, Eq, Clone)]
1420 struct Wrapper(u8);
1421
1422 impl CompactAs for Wrapper {
1423 type As = u8;
1424 fn encode_as(&self) -> &u8 {
1425 &self.0
1426 }
1427 fn decode_from(x: u8) -> Wrapper {
1428 Wrapper(x)
1429 }
1430 }
1431
1432 impl From<Compact<Wrapper>> for Wrapper {
1433 fn from(x: Compact<Wrapper>) -> Wrapper {
1434 x.0
1435 }
1436 }
1437
1438 #[test]
1439 fn compact_as_8_encoding_works() {
1440 let tests = [(0u8, 1usize), (63, 1), (64, 2), (255, 2)];
1441 for &(n, l) in &tests {
1442 let compact: Compact<Wrapper> = Wrapper(n).into();
1443 let encoded = compact.encode();
1444 assert_eq!(encoded.len(), l);
1445 assert_eq!(Compact::compact_len(&n), l);
1446 let decoded = <Compact<Wrapper>>::decode(&mut & encoded[..]).unwrap();
1447 let wrapper: Wrapper = decoded.into();
1448 assert_eq!(wrapper, Wrapper(n));
1449 }
1450 }
1451
1452 struct WithCompact<T: HasCompact> {
1453 _data: T,
1454 }
1455
1456 #[test]
1457 fn compact_as_has_compact() {
1458 let _data = WithCompact { _data: Wrapper(1) };
1459 }
1460
1461 #[test]
1462 fn compact_using_encoded_arrayvec_size() {
1463 Compact(std::u8::MAX).using_encoded(|_| {});
1464 Compact(std::u16::MAX).using_encoded(|_| {});
1465 Compact(std::u32::MAX).using_encoded(|_| {});
1466 Compact(std::u64::MAX).using_encoded(|_| {});
1467 Compact(std::u128::MAX).using_encoded(|_| {});
1468
1469 CompactRef(&std::u8::MAX).using_encoded(|_| {});
1470 CompactRef(&std::u16::MAX).using_encoded(|_| {});
1471 CompactRef(&std::u32::MAX).using_encoded(|_| {});
1472 CompactRef(&std::u64::MAX).using_encoded(|_| {});
1473 CompactRef(&std::u128::MAX).using_encoded(|_| {});
1474 }
1475}