diff --git a/src/bytes.rs b/src/bytes.rs index faab23be3..21d28e87c 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -34,6 +34,7 @@ //! } //! ``` +use std::mem; use std::fmt; use std::slice; use std::cmp::Ordering; @@ -260,19 +261,23 @@ impl FromBytes for String { } } -impl FromBytes for u64 { - fn from_bytes(bytes: &[u8]) -> FromBytesResult { - match bytes.len() { - 0 => Ok(0), - l @ 1...8 => { - let mut res = 0u64; - for i in 0..l { - let shift = (l - 1 - i) * 8; - res = res + ((bytes[i] as u64) << shift); +macro_rules! impl_uint_from_bytes { + ($to: ident) => { + impl FromBytes for $to { + fn from_bytes(bytes: &[u8]) -> FromBytesResult<$to> { + match bytes.len() { + 0 => Ok(0), + l if l <= mem::size_of::<$to>() => { + let mut res = 0 as $to; + for i in 0..l { + let shift = (l - 1 - i) * 8; + res = res + ((bytes[i] as $to) << shift); + } + Ok(res) + } + _ => Err(FromBytesError::DataIsTooLong) } - Ok(res) } - _ => Err(FromBytesError::DataIsTooLong) } } } @@ -287,20 +292,11 @@ impl FromBytes for bool { } } - -macro_rules! impl_map_from_bytes { - ($from: ident, $to: ident) => { - impl FromBytes for $from { - fn from_bytes(bytes: &[u8]) -> FromBytesResult<$from> { - $to::from_bytes(bytes).map(| x | { x as $from }) - } - } - } -} - -impl_map_from_bytes!(usize, u64); -impl_map_from_bytes!(u16, u64); -impl_map_from_bytes!(u32, u64); +//impl_uint_from_bytes!(u8); +impl_uint_from_bytes!(u16); +impl_uint_from_bytes!(u32); +impl_uint_from_bytes!(u64); +impl_uint_from_bytes!(usize); macro_rules! impl_uint_from_bytes { ($name: ident) => { diff --git a/src/crypto.rs b/src/crypto.rs index a80045e55..d13a53f53 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -37,7 +37,7 @@ impl From<::secp256k1::Error> for CryptoError { ::secp256k1::Error::InvalidPublicKey => CryptoError::InvalidPublic, ::secp256k1::Error::InvalidSignature => CryptoError::InvalidSignature, ::secp256k1::Error::InvalidSecretKey => CryptoError::InvalidSecret, - _ => panic!("Crypto error: {:?}", e), + _ => CryptoError::InvalidSignature, } } } diff --git a/src/rlp/rlperrors.rs b/src/rlp/rlperrors.rs index 4ee41a2ce..27b3082b5 100644 --- a/src/rlp/rlperrors.rs +++ b/src/rlp/rlperrors.rs @@ -8,7 +8,7 @@ pub enum DecoderError { RlpIsTooShort, RlpExpectedToBeList, RlpExpectedToBeData, - RlpIncorrectListLen + RlpIncorrectListLen, } impl StdError for DecoderError {