codec_impl/
lib.rs

1// Copyright 2015-2018 Susy Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Susy Codec serialization support for uint and fixed hash.
10
11#![cfg_attr(not(feature = "std"), no_std)]
12
13#[doc(hidden)]
14pub extern crate susy_codec as codec;
15
16/// Add Susy Codec serialization support to an integer created by `construct_uint!`.
17#[macro_export]
18macro_rules! impl_uint_codec {
19	($name: ident, $len: expr) => {
20		impl $crate::codec::Encode for $name {
21			fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
22				let mut bytes = [0u8; $len * 8];
23				self.to_little_endian(&mut bytes);
24				bytes.using_encoded(f)
25			}
26		}
27
28		impl $crate::codec::Decode for $name {
29			fn decode<I: $crate::codec::Input>(input: &mut I) -> Option<Self> {
30				<[u8; $len * 8] as $crate::codec::Decode>::decode(input)
31					.map(|b| $name::from_little_endian(&b))
32			}
33		}
34	}
35}
36
37/// Add Susy Codec serialization support to a fixed-sized hash type created by `construct_fixed_hash!`.
38#[macro_export]
39macro_rules! impl_fixed_hash_codec {
40	($name: ident, $len: expr) => {
41		impl $crate::codec::Encode for $name {
42			fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
43				self.0.using_encoded(f)
44			}
45		}
46		impl $crate::codec::Decode for $name {
47			fn decode<I: $crate::codec::Input>(input: &mut I) -> Option<Self> {
48				<[u8; $len] as $crate::codec::Decode>::decode(input).map($name)
49			}
50		}
51	}
52}