Skip to content
Snippets Groups Projects
Commit 6477b2ad authored by Marek Kotewicz's avatar Marek Kotewicz
Browse files

implements "bytes" for all fixed size arrays

parent 006fe202
No related merge requests found
...@@ -22,9 +22,26 @@ impl BytesConvertable for Vec<u8> { ...@@ -22,9 +22,26 @@ impl BytesConvertable for Vec<u8> {
fn bytes(&self) -> &[u8] { self } 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] #[test]
fn bytes_convertable() { fn bytes_convertable() {
assert_eq!(vec![0x12u8, 0x34].bytes(), &[0x12u8, 0x34]); assert_eq!(vec![0x12u8, 0x34].bytes(), &[0x12u8, 0x34]);
assert_eq!([0u8; 0].bytes(), &[]);
} }
/// TODO: optimise some conversations /// TODO: optimise some conversations
......
...@@ -20,10 +20,11 @@ impl<T> Hashable for T where T: BytesConvertable { ...@@ -20,10 +20,11 @@ impl<T> Hashable for T where T: BytesConvertable {
#[test] #[test]
fn sha3_empty() { fn sha3_empty() {
use std::str::FromStr; 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] #[test]
fn sha3_as() { fn sha3_as() {
use std::str::FromStr; use std::str::FromStr;
assert_eq!((&[0x41u8; 32][..]).sha3(), H256::from_str("59cad5948673622c1d64e2322488bf01619f7ff45789741b15a9f782ce9290a8").unwrap()); assert_eq!([0x41u8; 32].sha3(), H256::from_str("59cad5948673622c1d64e2322488bf01619f7ff45789741b15a9f782ce9290a8").unwrap());
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment