removed unused code from util and unnecessary dependency of FixedHash (#1824)

This commit is contained in:
Marek Kotewicz 2016-08-03 18:36:45 +02:00 committed by Gav Wood
parent 11b65ce53d
commit 531bc79edc
2 changed files with 41 additions and 53 deletions

View File

@ -35,46 +35,8 @@
use std::fmt; use std::fmt;
use std::slice; use std::slice;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use elastic_array::*;
/// Vector like object /// Slice pretty print helper
pub trait VecLike<T> {
/// Add an element to the collection
fn vec_push(&mut self, value: T);
/// Add a slice to the collection
fn vec_extend(&mut self, slice: &[T]);
}
impl<T> VecLike<T> for Vec<T> where T: Copy {
fn vec_push(&mut self, value: T) {
Vec::<T>::push(self, value)
}
fn vec_extend(&mut self, slice: &[T]) {
Vec::<T>::extend_from_slice(self, slice)
}
}
macro_rules! impl_veclike_for_elastic_array {
($from: ident) => {
impl<T> VecLike<T> for $from<T> where T: Copy {
fn vec_push(&mut self, value: T) {
$from::<T>::push(self, value)
}
fn vec_extend(&mut self, slice: &[T]) {
$from::<T>::append_slice(self, slice)
}
}
}
}
impl_veclike_for_elastic_array!(ElasticArray16);
impl_veclike_for_elastic_array!(ElasticArray32);
impl_veclike_for_elastic_array!(ElasticArray1024);
/// Slie pretty print helper
pub struct PrettySlice<'a> (&'a [u8]); pub struct PrettySlice<'a> (&'a [u8]);
impl<'a> fmt::Debug for PrettySlice<'a> { impl<'a> fmt::Debug for PrettySlice<'a> {

View File

@ -22,7 +22,7 @@ use std::fmt;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::error::Error as StdError; use std::error::Error as StdError;
use bigint::uint::{Uint, U128, U256}; use bigint::uint::{Uint, U128, U256};
use hash::FixedHash; use hash::{H64, H128, Address, H256, H512, H520, H2048};
use elastic_array::*; use elastic_array::*;
/// Vector like object /// Vector like object
@ -146,13 +146,25 @@ macro_rules! impl_uint_to_bytes {
impl_uint_to_bytes!(U256); impl_uint_to_bytes!(U256);
impl_uint_to_bytes!(U128); impl_uint_to_bytes!(U128);
impl <T>ToBytes for T where T: FixedHash { macro_rules! impl_hash_to_bytes {
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) { ($name: ident) => {
out.vec_extend(self.as_slice()); impl ToBytes for $name {
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
out.vec_extend(&self);
}
fn to_bytes_len(&self) -> usize { self.len() }
}
} }
fn to_bytes_len(&self) -> usize { self.as_slice().len() }
} }
impl_hash_to_bytes!(H64);
impl_hash_to_bytes!(H128);
impl_hash_to_bytes!(Address);
impl_hash_to_bytes!(H256);
impl_hash_to_bytes!(H512);
impl_hash_to_bytes!(H520);
impl_hash_to_bytes!(H2048);
/// Error returned when `FromBytes` conversation goes wrong /// Error returned when `FromBytes` conversation goes wrong
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum FromBytesError { pub enum FromBytesError {
@ -250,15 +262,29 @@ macro_rules! impl_uint_from_bytes {
impl_uint_from_bytes!(U256, 32); impl_uint_from_bytes!(U256, 32);
impl_uint_from_bytes!(U128, 16); impl_uint_from_bytes!(U128, 16);
impl <T>FromBytes for T where T: FixedHash { macro_rules! impl_hash_from_bytes {
fn from_bytes(bytes: &[u8]) -> FromBytesResult<T> { ($name: ident, $size: expr) => {
match bytes.len().cmp(&T::len()) { impl FromBytes for $name {
Ordering::Less => return Err(FromBytesError::DataIsTooShort), fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> {
Ordering::Greater => return Err(FromBytesError::DataIsTooLong), match bytes.len().cmp(&$size) {
Ordering::Equal => () Ordering::Less => Err(FromBytesError::DataIsTooShort),
}; Ordering::Greater => Err(FromBytesError::DataIsTooLong),
Ordering::Equal => {
Ok(T::from_slice(bytes)) let mut t = [0u8; $size];
t.copy_from_slice(bytes);
Ok($name(t))
}
}
}
}
} }
} }
impl_hash_from_bytes!(H64, 8);
impl_hash_from_bytes!(H128, 16);
impl_hash_from_bytes!(Address, 20);
impl_hash_from_bytes!(H256, 32);
impl_hash_from_bytes!(H512, 64);
impl_hash_from_bytes!(H520, 65);
impl_hash_from_bytes!(H2048, 256);