1use alloc::vec::Vec;
18use alloc::boxed::Box;
19
20#[cfg(any(feature = "std", feature = "full"))]
21use alloc::{
22 string::String,
23 borrow::{Cow, ToOwned},
24};
25
26use core::{mem, slice};
27use arrayvec::ArrayVec;
28use core::marker::PhantomData;
29
30pub trait Input {
32 fn read(&mut self, into: &mut [u8]) -> usize;
34
35 fn read_byte(&mut self) -> Option<u8> {
37 let mut buf = [0u8];
38 match self.read(&mut buf[..]) {
39 0 => None,
40 1 => Some(buf[0]),
41 _ => unreachable!(),
42 }
43 }
44}
45
46#[cfg(not(feature = "std"))]
47impl<'a> Input for &'a [u8] {
48 fn read(&mut self, into: &mut [u8]) -> usize {
49 let len = ::core::cmp::min(into.len(), self.len());
50 into[..len].copy_from_slice(&self[..len]);
51 *self = &self[len..];
52 len
53 }
54}
55
56#[cfg(feature = "std")]
57impl<R: ::std::io::Read> Input for R {
58 fn read(&mut self, into: &mut [u8]) -> usize {
59 match (self as &mut ::std::io::Read).read_exact(into) {
60 Ok(()) => into.len(),
61 Err(_) => 0,
62 }
63 }
64}
65
66struct PrefixInput<'a, T: 'a> {
68 prefix: Option<u8>,
69 input: &'a mut T,
70}
71
72impl<'a, T: 'a + Input> Input for PrefixInput<'a, T> {
73 fn read(&mut self, buffer: &mut [u8]) -> usize {
74 match self.prefix.take() {
75 Some(v) if buffer.len() > 0 => {
76 buffer[0] = v;
77 1 + self.input.read(&mut buffer[1..])
78 }
79 _ => self.input.read(buffer)
80 }
81 }
82}
83
84pub trait Output: Sized {
86 fn write(&mut self, bytes: &[u8]);
88
89 fn push_byte(&mut self, byte: u8) {
90 self.write(&[byte]);
91 }
92
93 fn push<V: Encode + ?Sized>(&mut self, value: &V) {
94 value.encode_to(self);
95 }
96}
97
98#[cfg(not(feature = "std"))]
99impl Output for Vec<u8> {
100 fn write(&mut self, bytes: &[u8]) {
101 self.extend(bytes);
102 }
103}
104
105#[cfg(feature = "std")]
106impl<W: ::std::io::Write> Output for W {
107 fn write(&mut self, bytes: &[u8]) {
108 (self as &mut ::std::io::Write).write_all(bytes).expect("Codec outputs are infallible");
109 }
110}
111
112pub trait Encode {
115 fn encode_to<T: Output>(&self, dest: &mut T) {
117 self.using_encoded(|buf| dest.write(buf));
118 }
119
120 fn encode(&self) -> Vec<u8> {
122 let mut r = Vec::new();
123 self.encode_to(&mut r);
124 r
125 }
126
127 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
129 f(&self.encode())
130 }
131}
132
133pub trait Decode: Sized {
135 fn decode<I: Input>(value: &mut I) -> Option<Self>;
137}
138
139pub trait Codec: Decode + Encode {}
141
142#[derive(Eq, PartialEq, Clone, Copy, Ord, PartialOrd)]
144pub struct Compact<T>(pub T);
145
146impl<T> From<T> for Compact<T> {
147 fn from(x: T) -> Compact<T> { Compact(x) }
148}
149
150impl<'a, T: Copy> From<&'a T> for Compact<T> {
151 fn from(x: &'a T) -> Compact<T> { Compact(*x) }
152}
153
154pub trait CompactAs: From<Compact<Self>> {
156 type As;
157 fn encode_as(&self) -> &Self::As;
158 fn decode_from(Self::As) -> Self;
159}
160
161impl<T> Encode for Compact<T>
162where
163 T: CompactAs,
164 for<'a> CompactRef<'a, <T as CompactAs>::As>: Encode,
165{
166 fn encode_to<W: Output>(&self, dest: &mut W) {
167 CompactRef(self.0.encode_as()).encode_to(dest)
168 }
169}
170
171impl<'a, T> Encode for CompactRef<'a, T>
172where
173 T: CompactAs,
174 for<'b> CompactRef<'b, <T as CompactAs>::As>: Encode,
175{
176 fn encode_to<Out: Output>(&self, dest: &mut Out) {
177 CompactRef(self.0.encode_as()).encode_to(dest)
178 }
179}
180
181impl<T> Decode for Compact<T>
182where
183 T: CompactAs,
184 Compact<<T as CompactAs>::As>: Decode,
185{
186 fn decode<I: Input>(input: &mut I) -> Option<Self> {
187 Compact::<T::As>::decode(input)
188 .map(|x| Compact(<T as CompactAs>::decode_from(x.0)))
189 }
190}
191
192macro_rules! impl_from_compact {
193 ( $( $ty:ty ),* ) => {
194 $(
195 impl From<Compact<$ty>> for $ty {
196 fn from(x: Compact<$ty>) -> $ty { x.0 }
197 }
198 )*
199 }
200}
201
202impl_from_compact! { u8, u16, u32, u64, u128 }
203
204#[derive(Eq, PartialEq, Clone, Copy)]
206pub struct CompactRef<'a, T: 'a>(pub &'a T);
207
208impl<'a, T> From<&'a T> for CompactRef<'a, T> {
209 fn from(x: &'a T) -> Self { CompactRef(x) }
210}
211
212impl<T> ::core::fmt::Debug for Compact<T> where T: ::core::fmt::Debug {
213 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
214 self.0.fmt(f)
215 }
216}
217
218#[cfg(feature = "std")]
219impl<T> ::serde::Serialize for Compact<T> where T: ::serde::Serialize {
220 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: ::serde::Serializer {
221 T::serialize(&self.0, serializer)
222 }
223}
224
225#[cfg(feature = "std")]
226impl<'de, T> ::serde::Deserialize<'de> for Compact<T> where T: ::serde::Deserialize<'de> {
227 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: ::serde::Deserializer<'de> {
228 T::deserialize(deserializer).map(Compact)
229 }
230}
231
232#[cfg(feature = "std")]
233pub trait MaybeDebugSerde: ::core::fmt::Debug + ::serde::Serialize + for<'a> ::serde::Deserialize<'a> {}
234#[cfg(feature = "std")]
235impl<T> MaybeDebugSerde for T where T: ::core::fmt::Debug + ::serde::Serialize + for<'a> ::serde::Deserialize<'a> {}
236
237#[cfg(not(feature = "std"))]
238pub trait MaybeDebugSerde {}
239#[cfg(not(feature = "std"))]
240impl<T> MaybeDebugSerde for T {}
241
242pub trait HasCompact: Sized {
244 type Type: for<'a> EncodeAsRef<'a, Self> + Decode + From<Self> + Into<Self> + Clone +
246 PartialEq + Eq + MaybeDebugSerde;
247}
248
249pub trait EncodeAsRef<'a, T: 'a> {
251 type RefType: Encode + From<&'a T>;
253}
254
255impl<'a, T: 'a> EncodeAsRef<'a, T> for Compact<T> where CompactRef<'a, T>: Encode + From<&'a T> {
256 type RefType = CompactRef<'a, T>;
257}
258
259impl<T: 'static> HasCompact for T where
260 Compact<T>: for<'a> EncodeAsRef<'a, T> + Decode + From<Self> + Into<Self> + Clone +
261 PartialEq + Eq + MaybeDebugSerde,
262{
263 type Type = Compact<T>;
264}
265
266impl<'a> Encode for CompactRef<'a, u8> {
276 fn encode_to<W: Output>(&self, dest: &mut W) {
277 match self.0 {
278 0...0b00111111 => dest.push_byte(self.0 << 2),
279 _ => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
280 }
281 }
282}
283
284impl Encode for Compact<u8> {
285 fn encode_to<W: Output>(&self, dest: &mut W) {
286 CompactRef(&self.0).encode_to(dest)
287 }
288}
289
290impl<'a> Encode for CompactRef<'a, u16> {
291 fn encode_to<W: Output>(&self, dest: &mut W) {
292 match self.0 {
293 0...0b00111111 => dest.push_byte((*self.0 as u8) << 2),
294 0...0b00111111_11111111 => ((*self.0 << 2) | 0b01).encode_to(dest),
295 _ => (((*self.0 as u32) << 2) | 0b10).encode_to(dest),
296 }
297 }
298}
299
300impl Encode for Compact<u16> {
301 fn encode_to<W: Output>(&self, dest: &mut W) {
302 CompactRef(&self.0).encode_to(dest)
303 }
304}
305
306impl<'a> Encode for CompactRef<'a, u32> {
307 fn encode_to<W: Output>(&self, dest: &mut W) {
308 match self.0 {
309 0...0b00111111 => dest.push_byte((*self.0 as u8) << 2),
310 0...0b00111111_11111111 => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
311 0...0b00111111_11111111_11111111_11111111 => ((*self.0 << 2) | 0b10).encode_to(dest),
312 _ => {
313 dest.push_byte(0b11);
314 self.0.encode_to(dest);
315 }
316 }
317 }
318}
319
320impl Encode for Compact<u32> {
321 fn encode_to<W: Output>(&self, dest: &mut W) {
322 CompactRef(&self.0).encode_to(dest)
323 }
324}
325
326impl<'a> Encode for CompactRef<'a, u64> {
327 fn encode_to<W: Output>(&self, dest: &mut W) {
328 match self.0 {
329 0...0b00111111 => dest.push_byte((*self.0 as u8) << 2),
330 0...0b00111111_11111111 => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
331 0...0b00111111_11111111_11111111_11111111 => (((*self.0 as u32) << 2) | 0b10).encode_to(dest),
332 _ => {
333 let bytes_needed = 8 - self.0.leading_zeros() / 8;
334 assert!(bytes_needed >= 4, "Previous match arm matches anyting less than 2^30; qed");
335 dest.push_byte(0b11 + ((bytes_needed - 4) << 2) as u8);
336 let mut v = *self.0;
337 for _ in 0..bytes_needed {
338 dest.push_byte(v as u8);
339 v >>= 8;
340 }
341 assert_eq!(v, 0, "shifted sufficient bits right to lead only leading zeros; qed")
342 }
343 }
344 }
345}
346
347impl Encode for Compact<u64> {
348 fn encode_to<W: Output>(&self, dest: &mut W) {
349 CompactRef(&self.0).encode_to(dest)
350 }
351}
352
353impl<'a> Encode for CompactRef<'a, u128> {
354 fn encode_to<W: Output>(&self, dest: &mut W) {
355 match self.0 {
356 0...0b00111111 => dest.push_byte((*self.0 as u8) << 2),
357 0...0b00111111_11111111 => (((*self.0 as u16) << 2) | 0b01).encode_to(dest),
358 0...0b00111111_11111111_11111111_11111111 => (((*self.0 as u32) << 2) | 0b10).encode_to(dest),
359 _ => {
360 let bytes_needed = 16 - self.0.leading_zeros() / 8;
361 assert!(bytes_needed >= 4, "Previous match arm matches anyting less than 2^30; qed");
362 dest.push_byte(0b11 + ((bytes_needed - 4) << 2) as u8);
363 let mut v = *self.0;
364 for _ in 0..bytes_needed {
365 dest.push_byte(v as u8);
366 v >>= 8;
367 }
368 assert_eq!(v, 0, "shifted sufficient bits right to lead only leading zeros; qed")
369 }
370 }
371 }
372}
373
374impl Encode for Compact<u128> {
375 fn encode_to<W: Output>(&self, dest: &mut W) {
376 CompactRef(&self.0).encode_to(dest)
377 }
378}
379
380impl Decode for Compact<u8> {
381 fn decode<I: Input>(input: &mut I) -> Option<Self> {
382 let prefix = input.read_byte()?;
383 Some(Compact(match prefix % 4 {
384 0 => prefix as u8 >> 2,
385 1 => {
386 let x = u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2;
387 if x < 256 {
388 x as u8
389 } else {
390 return None
391 }
392 }
393 _ => return None,
394 }))
395 }
396}
397
398impl Decode for Compact<u16> {
399 fn decode<I: Input>(input: &mut I) -> Option<Self> {
400 let prefix = input.read_byte()?;
401 Some(Compact(match prefix % 4 {
402 0 => prefix as u16 >> 2,
403 1 => u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u16 >> 2,
404 2 => {
405 let x = u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? >> 2;
406 if x < 65536 {
407 x as u16
408 } else {
409 return None
410 }
411 }
412 _ => return None,
413 }))
414 }
415}
416
417impl Decode for Compact<u32> {
418 fn decode<I: Input>(input: &mut I) -> Option<Self> {
419 let prefix = input.read_byte()?;
420 Some(Compact(match prefix % 4 {
421 0 => prefix as u32 >> 2,
422 1 => u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u32 >> 2,
423 2 => u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u32 >> 2,
424 3|_ => { if prefix >> 2 == 0 {
426 u32::decode(input)?
428 } else {
429 return None
431 }
432 }
433 }))
434 }
435}
436
437impl Decode for Compact<u64> {
438 fn decode<I: Input>(input: &mut I) -> Option<Self> {
439 let prefix = input.read_byte()?;
440 Some(Compact(match prefix % 4 {
441 0 => prefix as u64 >> 2,
442 1 => u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u64 >> 2,
443 2 => u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u64 >> 2,
444 3|_ => match (prefix >> 2) + 4 {
445 4 => u32::decode(input)? as u64,
446 8 => u64::decode(input)?,
447 x if x > 8 => return None,
448 bytes_needed => {
449 let mut res = 0;
450 for i in 0..bytes_needed {
451 res |= (input.read_byte()? as u64) << (i * 8);
452 }
453 res
454 }
455 }
456 }))
457 }
458}
459
460impl Decode for Compact<u128> {
461 fn decode<I: Input>(input: &mut I) -> Option<Self> {
462 let prefix = input.read_byte()?;
463 Some(Compact(match prefix % 4 {
464 0 => prefix as u128 >> 2,
465 1 => u16::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u128 >> 2,
466 2 => u32::decode(&mut PrefixInput{prefix: Some(prefix), input})? as u128 >> 2,
467 3|_ => match (prefix >> 2) + 4 {
468 4 => u32::decode(input)? as u128,
469 8 => u64::decode(input)? as u128,
470 16 => u128::decode(input)?,
471 x if x > 16 => return None,
472 bytes_needed => {
473 let mut res = 0;
474 for i in 0..bytes_needed {
475 res |= (input.read_byte()? as u128) << (i * 8);
476 }
477 res
478 }
479 }
480 }))
481 }
482}
483
484impl<S: Decode + Encode> Codec for S {}
485
486impl<T: Encode, E: Encode> Encode for Result<T, E> {
487 fn encode_to<W: Output>(&self, dest: &mut W) {
488 match *self {
489 Ok(ref t) => {
490 dest.push_byte(0);
491 t.encode_to(dest);
492 }
493 Err(ref e) => {
494 dest.push_byte(1);
495 e.encode_to(dest);
496 }
497 }
498 }
499}
500
501impl<T: Decode, E: Decode> Decode for Result<T, E> {
502 fn decode<I: Input>(input: &mut I) -> Option<Self> {
503 match input.read_byte()? {
504 0 => Some(Ok(T::decode(input)?)),
505 1 => Some(Err(E::decode(input)?)),
506 _ => None,
507 }
508 }
509}
510
511#[derive(Eq, PartialEq, Clone, Copy)]
513pub struct OptionBool(pub Option<bool>);
514
515impl ::core::fmt::Debug for OptionBool {
516 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
517 self.0.fmt(f)
518 }
519}
520
521impl Encode for OptionBool {
522 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
523 f(&[match *self {
524 OptionBool(None) => 0u8,
525 OptionBool(Some(true)) => 1u8,
526 OptionBool(Some(false)) => 2u8,
527 }])
528 }
529}
530
531impl Decode for OptionBool {
532 fn decode<I: Input>(input: &mut I) -> Option<Self> {
533 match input.read_byte()? {
534 0 => Some(OptionBool(None)),
535 1 => Some(OptionBool(Some(true))),
536 2 => Some(OptionBool(Some(false))),
537 _ => None,
538 }
539 }
540}
541
542impl<T: Encode> Encode for Option<T> {
543 fn encode_to<W: Output>(&self, dest: &mut W) {
544 match *self {
545 Some(ref t) => {
546 dest.push_byte(1);
547 t.encode_to(dest);
548 }
549 None => dest.push_byte(0),
550 }
551 }
552}
553
554impl<T: Decode> Decode for Option<T> {
555 fn decode<I: Input>(input: &mut I) -> Option<Self> {
556 match input.read_byte()? {
557 0 => Some(None),
558 1 => Some(Some(T::decode(input)?)),
559 _ => None,
560 }
561 }
562}
563
564macro_rules! impl_array {
565 ( $( $n:expr )* ) => { $(
566 impl<T: Encode> Encode for [T; $n] {
567 fn encode_to<W: Output>(&self, dest: &mut W) {
568 for item in self.iter() {
569 item.encode_to(dest);
570 }
571 }
572 }
573
574 impl<T: Decode> Decode for [T; $n] {
575 fn decode<I: Input>(input: &mut I) -> Option<Self> {
576 let mut r = ArrayVec::new();
577 for _ in 0..$n {
578 r.push(T::decode(input)?);
579 }
580 r.into_inner().ok()
581 }
582 }
583 )* }
584}
585
586impl_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
587 40 48 56 64 72 96 128 160 192 224 256);
588
589impl<T: Encode> Encode for Box<T> {
590 fn encode_to<W: Output>(&self, dest: &mut W) {
591 self.as_ref().encode_to(dest)
592 }
593}
594
595impl<T: Decode> Decode for Box<T> {
596 fn decode<I: Input>(input: &mut I) -> Option<Self> {
597 Some(Box::new(T::decode(input)?))
598 }
599}
600
601impl Encode for [u8] {
602 fn encode_to<W: Output>(&self, dest: &mut W) {
603 let len = self.len();
604 assert!(len <= u32::max_value() as usize, "Attempted to serialize a collection with too many elements.");
605 Compact(len as u32).encode_to(dest);
606 dest.write(self)
607 }
608}
609
610impl Encode for Vec<u8> {
611 fn encode_to<W: Output>(&self, dest: &mut W) {
612 self.as_slice().encode_to(dest)
613 }
614}
615
616impl Decode for Vec<u8> {
617 fn decode<I: Input>(input: &mut I) -> Option<Self> {
618 <Compact<u32>>::decode(input).and_then(move |Compact(len)| {
619 let len = len as usize;
620 let mut vec = vec![0; len];
621 if input.read(&mut vec[..len]) != len {
622 None
623 } else {
624 Some(vec)
625 }
626 })
627 }
628}
629
630impl<'a> Encode for &'a str {
631 fn encode_to<W: Output>(&self, dest: &mut W) {
632 self.as_bytes().encode_to(dest)
633 }
634}
635
636#[cfg(any(feature = "std", feature = "full"))]
637impl<'a, T: ToOwned + ?Sized + 'a> Encode for Cow<'a, T> where
638 &'a T: Encode,
639 <T as ToOwned>::Owned: Encode
640{
641 fn encode_to<W: Output>(&self, dest: &mut W) {
642 match self {
643 Cow::Owned(ref x) => x.encode_to(dest),
644 Cow::Borrowed(x) => x.encode_to(dest),
645 }
646 }
647}
648
649#[cfg(any(feature = "std", feature = "full"))]
650impl<'a, T: ToOwned + ?Sized> Decode for Cow<'a, T> where
651 <T as ToOwned>::Owned: Decode
652{
653 fn decode<I: Input>(input: &mut I) -> Option<Self> {
654 Some(Cow::Owned(Decode::decode(input)?))
655 }
656}
657
658#[cfg(any(feature = "std", feature = "full"))]
659impl<T> Encode for PhantomData<T> {
660 fn encode_to<W: Output>(&self, _dest: &mut W) {
661 }
662}
663
664#[cfg(any(feature = "std", feature = "full"))]
665impl<T> Decode for PhantomData<T> {
666 fn decode<I: Input>(_input: &mut I) -> Option<Self> {
667 Some(PhantomData)
668 }
669}
670
671#[cfg(any(feature = "std", feature = "full"))]
672impl Encode for String {
673 fn encode_to<W: Output>(&self, dest: &mut W) {
674 self.as_bytes().encode_to(dest)
675 }
676}
677
678#[cfg(any(feature = "std", feature = "full"))]
679impl Decode for String {
680 fn decode<I: Input>(input: &mut I) -> Option<Self> {
681 Some(Self::from_utf8_lossy(&Vec::decode(input)?).into())
682 }
683}
684
685impl<T: Encode> Encode for [T] {
686 fn encode_to<W: Output>(&self, dest: &mut W) {
687 let len = self.len();
688 assert!(len <= u32::max_value() as usize, "Attempted to serialize a collection with too many elements.");
689 Compact(len as u32).encode_to(dest);
690 for item in self {
691 item.encode_to(dest);
692 }
693 }
694}
695
696impl<T: Encode> Encode for Vec<T> {
697 fn encode_to<W: Output>(&self, dest: &mut W) {
698 self.as_slice().encode_to(dest)
699 }
700}
701
702impl<T: Decode> Decode for Vec<T> {
703 fn decode<I: Input>(input: &mut I) -> Option<Self> {
704 <Compact<u32>>::decode(input).and_then(move |Compact(len)| {
705 let mut r = Vec::with_capacity(len as usize);
706 for _ in 0..len {
707 r.push(T::decode(input)?);
708 }
709 Some(r)
710 })
711 }
712}
713
714impl Encode for () {
715 fn encode_to<T: Output>(&self, _dest: &mut T) {
716 }
717
718 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
719 f(&[])
720 }
721
722 fn encode(&self) -> Vec<u8> {
723 Vec::new()
724 }
725}
726
727impl<'a, T: 'a + Encode + ?Sized> Encode for &'a T {
728 fn encode_to<D: Output>(&self, dest: &mut D) {
729 (&**self).encode_to(dest)
730 }
731
732 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
733 (&**self).using_encoded(f)
734 }
735
736 fn encode(&self) -> Vec<u8> {
737 (&**self).encode()
738 }
739}
740
741impl Decode for () {
742 fn decode<I: Input>(_: &mut I) -> Option<()> {
743 Some(())
744 }
745}
746
747macro_rules! tuple_impl {
748 ($one:ident,) => {
749 impl<$one: Encode> Encode for ($one,) {
750 fn encode_to<T: Output>(&self, dest: &mut T) {
751 self.0.encode_to(dest);
752 }
753 }
754
755 impl<$one: Decode> Decode for ($one,) {
756 fn decode<I: Input>(input: &mut I) -> Option<Self> {
757 match $one::decode(input) {
758 None => None,
759 Some($one) => Some(($one,)),
760 }
761 }
762 }
763 };
764 ($first:ident, $($rest:ident,)+) => {
765 impl<$first: Encode, $($rest: Encode),+>
766 Encode for
767 ($first, $($rest),+) {
768 fn encode_to<T: Output>(&self, dest: &mut T) {
769 let (
770 ref $first,
771 $(ref $rest),+
772 ) = *self;
773
774 $first.encode_to(dest);
775 $($rest.encode_to(dest);)+
776 }
777 }
778
779 impl<$first: Decode, $($rest: Decode),+>
780 Decode for
781 ($first, $($rest),+) {
782 fn decode<INPUT: Input>(input: &mut INPUT) -> Option<Self> {
783 Some((
784 match $first::decode(input) {
785 Some(x) => x,
786 None => return None,
787 },
788 $(match $rest::decode(input) {
789 Some(x) => x,
790 None => return None,
791 },)+
792 ))
793 }
794 }
795
796 tuple_impl!($($rest,)+);
797 }
798}
799
800#[allow(non_snake_case)]
801mod inner_tuple_impl {
802 use super::{Input, Output, Decode, Encode};
803 tuple_impl!(A, B, C, D, E, F, G, H, I, J, K,);
804}
805
806trait EndianSensitive: Copy + 'static {
811 fn to_le(self) -> Self { self }
812 fn to_be(self) -> Self { self }
813 fn from_le(self) -> Self { self }
814 fn from_be(self) -> Self { self }
815 fn as_be_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { f(&self) }
816 fn as_le_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { f(&self) }
817}
818
819macro_rules! impl_endians {
820 ( $( $t:ty ),* ) => { $(
821 impl EndianSensitive for $t {
822 fn to_le(self) -> Self { <$t>::to_le(self) }
823 fn to_be(self) -> Self { <$t>::to_be(self) }
824 fn from_le(self) -> Self { <$t>::from_le(self) }
825 fn from_be(self) -> Self { <$t>::from_be(self) }
826 fn as_be_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { let d = self.to_be(); f(&d) }
827 fn as_le_then<T, F: FnOnce(&Self) -> T>(&self, f: F) -> T { let d = self.to_le(); f(&d) }
828 }
829
830 impl Encode for $t {
831 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
832 self.as_le_then(|le| {
833 let size = mem::size_of::<$t>();
834 let value_slice = unsafe {
835 let ptr = le as *const _ as *const u8;
836 if size != 0 {
837 slice::from_raw_parts(ptr, size)
838 } else {
839 &[]
840 }
841 };
842
843 f(value_slice)
844 })
845 }
846 }
847
848 impl Decode for $t {
849 fn decode<I: Input>(input: &mut I) -> Option<Self> {
850 let size = mem::size_of::<$t>();
851 assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type.");
852 let mut val: $t = unsafe { mem::zeroed() };
853
854 unsafe {
855 let raw: &mut [u8] = slice::from_raw_parts_mut(
856 &mut val as *mut $t as *mut u8,
857 size
858 );
859 if input.read(raw) != size { return None }
860 }
861 Some(val.from_le())
862 }
863 }
864 )* }
865}
866macro_rules! impl_non_endians {
867 ( $( $t:ty ),* ) => { $(
868 impl EndianSensitive for $t {}
869
870 impl Encode for $t {
871 fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
872 self.as_le_then(|le| {
873 let size = mem::size_of::<$t>();
874 let value_slice = unsafe {
875 let ptr = le as *const _ as *const u8;
876 if size != 0 {
877 slice::from_raw_parts(ptr, size)
878 } else {
879 &[]
880 }
881 };
882
883 f(value_slice)
884 })
885 }
886 }
887
888 impl Decode for $t {
889 fn decode<I: Input>(input: &mut I) -> Option<Self> {
890 let size = mem::size_of::<$t>();
891 assert!(size > 0, "EndianSensitive can never be implemented for a zero-sized type.");
892 let mut val: $t = unsafe { mem::zeroed() };
893
894 unsafe {
895 let raw: &mut [u8] = slice::from_raw_parts_mut(
896 &mut val as *mut $t as *mut u8,
897 size
898 );
899 if input.read(raw) != size { return None }
900 }
901 Some(val.from_le())
902 }
903 }
904 )* }
905}
906
907impl_endians!(u16, u32, u64, u128, usize, i16, i32, i64, i128, isize);
908impl_non_endians!(i8, [u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8],
909 [u8; 10], [u8; 12], [u8; 14], [u8; 16], [u8; 20], [u8; 24], [u8; 28], [u8; 32], [u8; 40],
910 [u8; 48], [u8; 56], [u8; 64], [u8; 80], [u8; 96], [u8; 112], [u8; 128], bool);
911
912
913#[cfg(test)]
914mod tests {
915 use super::*;
916 use std::borrow::Cow;
917
918 #[test]
919 fn vec_is_slicable() {
920 let v = b"Hello world".to_vec();
921 v.using_encoded(|ref slice|
922 assert_eq!(slice, &b"\x2cHello world")
923 );
924 }
925
926 #[test]
927 fn encode_borrowed_tuple() {
928 let x = vec![1u8, 2, 3, 4];
929 let y = 128i64;
930
931 let encoded = (&x, &y).encode();
932
933 assert_eq!((x, y), Decode::decode(&mut &encoded[..]).unwrap());
934 }
935
936 #[test]
937 fn cow_works() {
938 let x = &[1u32, 2, 3, 4, 5, 6][..];
939 let y = Cow::Borrowed(&x);
940 assert_eq!(x.encode(), y.encode());
941
942 let z: Cow<[u32]> = Cow::decode(&mut &x.encode()[..]).unwrap();
943 assert_eq!(*z, *x);
944 }
945
946 #[test]
947 fn cow_string_works() {
948 let x = "Hello world!";
949 let y = Cow::Borrowed(&x);
950 assert_eq!(x.encode(), y.encode());
951
952 let z: Cow<str> = Cow::decode(&mut &x.encode()[..]).unwrap();
953 assert_eq!(*z, *x);
954 }
955
956 #[test]
957 fn compact_128_encoding_works() {
958 let tests = [
959 (0u128, 1usize), (63, 1), (64, 2), (16383, 2),
960 (16384, 4), (1073741823, 4),
961 (1073741824, 5), ((1 << 32) - 1, 5),
962 (1 << 32, 6), (1 << 40, 7), (1 << 48, 8), ((1 << 56) - 1, 8), (1 << 56, 9), ((1 << 64) - 1, 9),
963 (1 << 64, 10), (1 << 72, 11), (1 << 80, 12), (1 << 88, 13), (1 << 96, 14), (1 << 104, 15),
964 (1 << 112, 16), ((1 << 120) - 1, 16), (1 << 120, 17), (u128::max_value(), 17)
965 ];
966 for &(n, l) in &tests {
967 let encoded = Compact(n as u128).encode();
968 assert_eq!(encoded.len(), l);
969 assert_eq!(<Compact<u128>>::decode(&mut &encoded[..]).unwrap().0, n);
970 }
971 }
972
973 #[test]
974 fn compact_64_encoding_works() {
975 let tests = [
976 (0u64, 1usize), (63, 1), (64, 2), (16383, 2),
977 (16384, 4), (1073741823, 4),
978 (1073741824, 5), ((1 << 32) - 1, 5),
979 (1 << 32, 6), (1 << 40, 7), (1 << 48, 8), ((1 << 56) - 1, 8), (1 << 56, 9), (u64::max_value(), 9)
980 ];
981 for &(n, l) in &tests {
982 let encoded = Compact(n as u64).encode();
983 assert_eq!(encoded.len(), l);
984 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n);
985 }
986 }
987
988 #[test]
989 fn compact_32_encoding_works() {
990 let tests = [(0u32, 1usize), (63, 1), (64, 2), (16383, 2), (16384, 4), (1073741823, 4), (1073741824, 5), (u32::max_value(), 5)];
991 for &(n, l) in &tests {
992 let encoded = Compact(n as u32).encode();
993 assert_eq!(encoded.len(), l);
994 assert_eq!(<Compact<u32>>::decode(&mut &encoded[..]).unwrap().0, n);
995 }
996 }
997
998 #[test]
999 fn compact_16_encoding_works() {
1000 let tests = [(0u16, 1usize), (63, 1), (64, 2), (16383, 2), (16384, 4), (65535, 4)];
1001 for &(n, l) in &tests {
1002 let encoded = Compact(n as u16).encode();
1003 assert_eq!(encoded.len(), l);
1004 assert_eq!(<Compact<u16>>::decode(&mut &encoded[..]).unwrap().0, n);
1005 }
1006 assert!(<Compact<u16>>::decode(&mut &Compact(65536u32).encode()[..]).is_none());
1007 }
1008
1009 #[test]
1010 fn compact_8_encoding_works() {
1011 let tests = [(0u8, 1usize), (63, 1), (64, 2), (255, 2)];
1012 for &(n, l) in &tests {
1013 let encoded = Compact(n as u8).encode();
1014 assert_eq!(encoded.len(), l);
1015 assert_eq!(<Compact<u8>>::decode(&mut &encoded[..]).unwrap().0, n);
1016 }
1017 assert!(<Compact<u8>>::decode(&mut &Compact(256u32).encode()[..]).is_none());
1018 }
1019
1020 fn hexify(bytes: &Vec<u8>) -> String {
1021 bytes.iter().map(|ref b| format!("{:02x}", b)).collect::<Vec<String>>().join(" ")
1022 }
1023
1024 #[test]
1025 fn string_encoded_as_expected() {
1026 let value = String::from("Hello, World!");
1027 let encoded = value.encode();
1028 assert_eq!(hexify(&encoded), "34 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 21");
1029 assert_eq!(<String>::decode(&mut &encoded[..]).unwrap(), value);
1030 }
1031
1032 #[test]
1033 fn vec_of_u8_encoded_as_expected() {
1034 let value = vec![0u8, 1, 1, 2, 3, 5, 8, 13, 21, 34];
1035 let encoded = value.encode();
1036 assert_eq!(hexify(&encoded), "28 00 01 01 02 03 05 08 0d 15 22");
1037 assert_eq!(<Vec<u8>>::decode(&mut &encoded[..]).unwrap(), value);
1038 }
1039
1040 #[test]
1041 fn vec_of_i16_encoded_as_expected() {
1042 let value = vec![0i16, 1, -1, 2, -2, 3, -3];
1043 let encoded = value.encode();
1044 assert_eq!(hexify(&encoded), "1c 00 00 01 00 ff ff 02 00 fe ff 03 00 fd ff");
1045 assert_eq!(<Vec<i16>>::decode(&mut &encoded[..]).unwrap(), value);
1046 }
1047
1048 #[test]
1049 fn vec_of_option_int_encoded_as_expected() {
1050 let value = vec![Some(1i8), Some(-1), None];
1051 let encoded = value.encode();
1052 assert_eq!(hexify(&encoded), "0c 01 01 01 ff 00");
1053 assert_eq!(<Vec<Option<i8>>>::decode(&mut &encoded[..]).unwrap(), value);
1054 }
1055
1056 #[test]
1057 fn vec_of_option_bool_encoded_as_expected() {
1058 let value = vec![OptionBool(Some(true)), OptionBool(Some(false)), OptionBool(None)];
1059 let encoded = value.encode();
1060 assert_eq!(hexify(&encoded), "0c 01 02 00");
1061 assert_eq!(<Vec<OptionBool>>::decode(&mut &encoded[..]).unwrap(), value);
1062 }
1063
1064 #[test]
1065 fn vec_of_string_encoded_as_expected() {
1066 let value = vec![
1067 "Hamlet".to_owned(),
1068 "Война и мир".to_owned(),
1069 "三国演义".to_owned(),
1070 "أَلْف لَيْلَة وَلَيْلَة".to_owned()
1071 ];
1072 let encoded = value.encode();
1073 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 \
1074 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 \
1075 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 \
1076 d9 84 d9 8e d8 a9 e2 80 8e");
1077 assert_eq!(<Vec<String>>::decode(&mut &encoded[..]).unwrap(), value);
1078 }
1079
1080 #[test]
1081 fn compact_integers_encoded_as_expected() {
1082 let tests = [
1083 (0u64, "00"),
1084 (63, "fc"),
1085 (64, "01 01"),
1086 (16383, "fd ff"),
1087 (16384, "02 00 01 00"),
1088 (1073741823, "fe ff ff ff"),
1089 (1073741824, "03 00 00 00 40"),
1090 ((1 << 32) - 1, "03 ff ff ff ff"),
1091 (1 << 32, "07 00 00 00 00 01"),
1092 (1 << 40, "0b 00 00 00 00 00 01"),
1093 (1 << 48, "0f 00 00 00 00 00 00 01"),
1094 ((1 << 56) - 1, "0f ff ff ff ff ff ff ff"),
1095 (1 << 56, "13 00 00 00 00 00 00 00 01"),
1096 (u64::max_value(), "13 ff ff ff ff ff ff ff ff")
1097 ];
1098 for &(n, s) in &tests {
1099 let encoded = Compact(n as u64).encode();
1101 assert_eq!(hexify(&encoded), s);
1102 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n);
1103
1104 if n <= u32::max_value() as u64 {
1106 assert_eq!(<Compact<u32>>::decode(&mut &encoded[..]).unwrap().0, n as u32);
1107 let encoded = Compact(n as u32).encode();
1108 assert_eq!(hexify(&encoded), s);
1109 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n as u64);
1110 }
1111 if n <= u16::max_value() as u64 {
1112 assert_eq!(<Compact<u16>>::decode(&mut &encoded[..]).unwrap().0, n as u16);
1113 let encoded = Compact(n as u16).encode();
1114 assert_eq!(hexify(&encoded), s);
1115 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n as u64);
1116 }
1117 if n <= u8::max_value() as u64 {
1118 assert_eq!(<Compact<u8>>::decode(&mut &encoded[..]).unwrap().0, n as u8);
1119 let encoded = Compact(n as u8).encode();
1120 assert_eq!(hexify(&encoded), s);
1121 assert_eq!(<Compact<u64>>::decode(&mut &encoded[..]).unwrap().0, n as u64);
1122 }
1123 }
1124 }
1125
1126 #[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
1127 #[derive(PartialEq, Eq, Clone)]
1128 struct Wrapper(u8);
1129
1130 impl CompactAs for Wrapper {
1131 type As = u8;
1132 fn encode_as(&self) -> &u8 {
1133 &self.0
1134 }
1135 fn decode_from(x: u8) -> Wrapper {
1136 Wrapper(x)
1137 }
1138 }
1139
1140 impl From<Compact<Wrapper>> for Wrapper {
1141 fn from(x: Compact<Wrapper>) -> Wrapper {
1142 x.0
1143 }
1144 }
1145
1146 #[test]
1147 fn compact_as_8_encoding_works() {
1148 let tests = [(0u8, 1usize), (63, 1), (64, 2), (255, 2)];
1149 for &(n, l) in &tests {
1150 let compact: Compact<Wrapper> = Wrapper(n).into();
1151 let encoded = compact.encode();
1152 assert_eq!(encoded.len(), l);
1153 let decoded = <Compact<Wrapper>>::decode(&mut & encoded[..]).unwrap();
1154 let wrapper: Wrapper = decoded.into();
1155 assert_eq!(wrapper, Wrapper(n));
1156 }
1157 }
1158
1159 struct WithCompact<T: HasCompact> {
1160 _data: T,
1161 }
1162
1163 #[test]
1164 fn compact_as_has_compact() {
1165 let _data = WithCompact { _data: Wrapper(1) };
1166 }
1167}