From 6477b2ad69fde88002921dbf51128c53590ced78 Mon Sep 17 00:00:00 2001 From: debris Date: Fri, 27 Nov 2015 18:15:44 +0100 Subject: [PATCH] implements "bytes" for all fixed size arrays --- src/bytes.rs | 17 +++++++++++++++++ src/sha3.rs | 5 +++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index 73592643d..870c9eef3 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -22,9 +22,26 @@ impl BytesConvertable for Vec { fn bytes(&self) -> &[u8] { self } } +macro_rules! impl_bytes_convertable_for_array { + ($zero: expr) => (); + ($len: expr, $($idx: expr),*) => { + impl BytesConvertable for [u8; $len] { + fn bytes(&self) -> &[u8] { self } + } + impl_bytes_convertable_for_array! { $($idx),* } + } +} + +// two -1 at the end is not expanded +impl_bytes_convertable_for_array! { + 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, + 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1 +} + #[test] fn bytes_convertable() { assert_eq!(vec![0x12u8, 0x34].bytes(), &[0x12u8, 0x34]); + assert_eq!([0u8; 0].bytes(), &[]); } /// TODO: optimise some conversations diff --git a/src/sha3.rs b/src/sha3.rs index 61c5c26f8..e2866d288 100644 --- a/src/sha3.rs +++ b/src/sha3.rs @@ -20,10 +20,11 @@ impl Hashable for T where T: BytesConvertable { #[test] fn sha3_empty() { use std::str::FromStr; - assert_eq!((&[0u8; 0][..]).sha3(), H256::from_str("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap()); + assert_eq!([0u8; 0].sha3(), H256::from_str("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").unwrap()); } #[test] fn sha3_as() { use std::str::FromStr; - assert_eq!((&[0x41u8; 32][..]).sha3(), H256::from_str("59cad5948673622c1d64e2322488bf01619f7ff45789741b15a9f782ce9290a8").unwrap()); + assert_eq!([0x41u8; 32].sha3(), H256::from_str("59cad5948673622c1d64e2322488bf01619f7ff45789741b15a9f782ce9290a8").unwrap()); } +