Assert hash-to-uint conversions operate on same-sized types

This commit is contained in:
David Palm 2018-08-24 13:54:56 +02:00
parent 3e0b5aa600
commit 2fb834e13a
2 changed files with 12 additions and 5 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "fixed-hash"
version = "0.2.2"
version = "0.2.3"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
homepage = "https://github.com/paritytech/parity-common"

View File

@ -344,8 +344,7 @@ macro_rules! impl_hash_conversions {
($a: ident, $a_size: expr, $b: ident, $b_size: expr) => {
impl From<$b> for $a {
fn from(value: $b) -> $a {
// REVIEW: better way of ensuring the macro params are ok?
assert!($a_size > $b_size && $a_size % 2 == 0 && $b_size %2 == 0);
debug_assert!($a_size > $b_size && $a_size % 2 == 0 && $b_size %2 == 0);
let mut ret = $a::new();
ret.0[($a_size - $b_size)..$a_size].copy_from_slice(&value);
ret
@ -354,8 +353,7 @@ macro_rules! impl_hash_conversions {
impl From<$a> for $b {
fn from(value: $a) -> $b {
// REVIEW: better way of ensuring the macro params are ok?
assert!($a_size > $b_size && $a_size % 2 == 0 && $b_size %2 == 0);
debug_assert!($a_size > $b_size && $a_size % 2 == 0 && $b_size %2 == 0);
let mut ret = $b::new();
ret.0.copy_from_slice(&value[($a_size - $b_size)..$a_size]);
ret
@ -378,6 +376,7 @@ macro_rules! impl_hash_conversions {
#[macro_export]
macro_rules! impl_hash_uint_conversions {
($hash: ident, $uint: ident) => {
debug_assert_eq!(::core::mem::size_of::<$hash>(), ::core::mem::size_of::<$uint>());
impl From<$uint> for $hash {
fn from(value: $uint) -> $hash {
let mut ret = $hash::new();
@ -677,4 +676,12 @@ mod tests {
let r: U256 = From::from(h);
assert!(r == u)
}
#[test]
#[should_panic]
fn converting_differently_sized_types_panics() {
use uint::U512;
impl_hash_uint_conversions!(H256, U512);
}
}