From 9ccaec5b917978f6b7b9ee4ca2e1f0e9a879374f Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 13 May 2019 12:33:59 +1000 Subject: [PATCH] Move SSZ macros into own file --- eth2/utils/ssz/src/lib.rs | 88 +----------------------------------- eth2/utils/ssz/src/macros.rs | 86 +++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 87 deletions(-) create mode 100644 eth2/utils/ssz/src/macros.rs diff --git a/eth2/utils/ssz/src/lib.rs b/eth2/utils/ssz/src/lib.rs index e6e061e513..a1ecb8deee 100644 --- a/eth2/utils/ssz/src/lib.rs +++ b/eth2/utils/ssz/src/lib.rs @@ -1,5 +1,6 @@ mod decode; mod encode; +mod macros; pub use decode::{ impls::decode_list_of_variable_length_items, Decodable, DecodeError, SszDecoderBuilder, @@ -18,90 +19,3 @@ where { val.as_ssz_bytes() } - -#[macro_export] -macro_rules! impl_encode_via_from { - ($impl_type: ty, $from_type: ty) => { - impl Encodable for $impl_type { - fn is_ssz_fixed_len() -> bool { - <$from_type as Encodable>::is_ssz_fixed_len() - } - - fn ssz_fixed_len() -> usize { - <$from_type as Encodable>::ssz_fixed_len() - } - - fn ssz_append(&self, buf: &mut Vec) { - let conv: $from_type = self.clone().into(); - - conv.ssz_append(buf) - } - } - }; -} - -#[macro_export] -macro_rules! impl_decode_via_from { - ($impl_type: ty, $from_type: tt) => { - impl Decodable for $impl_type { - fn is_ssz_fixed_len() -> bool { - <$from_type as Decodable>::is_ssz_fixed_len() - } - - fn ssz_fixed_len() -> usize { - <$from_type as Decodable>::ssz_fixed_len() - } - - fn from_ssz_bytes(bytes: &[u8]) -> Result { - $from_type::from_ssz_bytes(bytes).and_then(|dec| Ok(dec.into())) - } - } - }; -} - -#[cfg(test)] -mod tests { - use super::*; - use crate as ssz; - - #[derive(PartialEq, Debug, Clone, Copy)] - struct Wrapper(u64); - - impl From for Wrapper { - fn from(x: u64) -> Wrapper { - Wrapper(x) - } - } - - impl From for u64 { - fn from(x: Wrapper) -> u64 { - x.0 - } - } - - impl_encode_via_from!(Wrapper, u64); - impl_decode_via_from!(Wrapper, u64); - - #[test] - fn impl_encode_via_from() { - let check_encode = |a: u64, b: Wrapper| assert_eq!(a.as_ssz_bytes(), b.as_ssz_bytes()); - - check_encode(0, Wrapper(0)); - check_encode(1, Wrapper(1)); - check_encode(42, Wrapper(42)); - } - - #[test] - fn impl_decode_via_from() { - let check_decode = |bytes: Vec| { - let a = u64::from_ssz_bytes(&bytes).unwrap(); - let b = Wrapper::from_ssz_bytes(&bytes).unwrap(); - - assert_eq!(a, b.into()) - }; - - check_decode(vec![0, 0, 0, 0, 0, 0, 0, 0]); - check_decode(vec![1, 0, 0, 0, 0, 0, 0, 0]); - check_decode(vec![1, 0, 0, 0, 2, 0, 0, 0]); - } -} diff --git a/eth2/utils/ssz/src/macros.rs b/eth2/utils/ssz/src/macros.rs new file mode 100644 index 0000000000..dfe4258d27 --- /dev/null +++ b/eth2/utils/ssz/src/macros.rs @@ -0,0 +1,86 @@ +#[macro_export] +macro_rules! impl_encode_via_from { + ($impl_type: ty, $from_type: ty) => { + impl ssz::Encodable for $impl_type { + fn is_ssz_fixed_len() -> bool { + <$from_type as ssz::Encodable>::is_ssz_fixed_len() + } + + fn ssz_fixed_len() -> usize { + <$from_type as ssz::Encodable>::ssz_fixed_len() + } + + fn ssz_append(&self, buf: &mut Vec) { + let conv: $from_type = self.clone().into(); + + conv.ssz_append(buf) + } + } + }; +} + +#[macro_export] +macro_rules! impl_decode_via_from { + ($impl_type: ty, $from_type: tt) => { + impl ssz::Decodable for $impl_type { + fn is_ssz_fixed_len() -> bool { + <$from_type as ssz::Decodable>::is_ssz_fixed_len() + } + + fn ssz_fixed_len() -> usize { + <$from_type as ssz::Decodable>::ssz_fixed_len() + } + + fn from_ssz_bytes(bytes: &[u8]) -> Result { + $from_type::from_ssz_bytes(bytes).and_then(|dec| Ok(dec.into())) + } + } + }; +} + +#[cfg(test)] +mod tests { + use crate as ssz; + use ssz::{Decodable, Encodable}; + + #[derive(PartialEq, Debug, Clone, Copy)] + struct Wrapper(u64); + + impl From for Wrapper { + fn from(x: u64) -> Wrapper { + Wrapper(x) + } + } + + impl From for u64 { + fn from(x: Wrapper) -> u64 { + x.0 + } + } + + impl_encode_via_from!(Wrapper, u64); + impl_decode_via_from!(Wrapper, u64); + + #[test] + fn impl_encode_via_from() { + let check_encode = |a: u64, b: Wrapper| assert_eq!(a.as_ssz_bytes(), b.as_ssz_bytes()); + + check_encode(0, Wrapper(0)); + check_encode(1, Wrapper(1)); + check_encode(42, Wrapper(42)); + } + + #[test] + fn impl_decode_via_from() { + let check_decode = |bytes: Vec| { + let a = u64::from_ssz_bytes(&bytes).unwrap(); + let b = Wrapper::from_ssz_bytes(&bytes).unwrap(); + + assert_eq!(a, b.into()) + }; + + check_decode(vec![0, 0, 0, 0, 0, 0, 0, 0]); + check_decode(vec![1, 0, 0, 0, 0, 0, 0, 0]); + check_decode(vec![1, 0, 0, 0, 2, 0, 0, 0]); + } +}