implements "bytes" for all fixed size arrays
This commit is contained in:
parent
006fe20287
commit
6477b2ad69
17
src/bytes.rs
17
src/bytes.rs
@ -22,9 +22,26 @@ impl BytesConvertable for Vec<u8> {
|
||||
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
|
||||
|
@ -20,10 +20,11 @@ impl<T> 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());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user