removed unused code from util and unnecessary dependency of FixedHash
This commit is contained in:
parent
21c65a99ea
commit
17bfc113c1
@ -35,47 +35,6 @@
|
||||
use std::fmt;
|
||||
use std::slice;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use hash::FixedHash;
|
||||
use elastic_array::*;
|
||||
use std::mem;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
/// Vector like object
|
||||
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]);
|
||||
@ -230,213 +189,6 @@ impl<T> Populatable for [T] where T: Sized {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Bytes array deserialization error
|
||||
pub enum FromBytesError {
|
||||
/// Not enough bytes for the requested type
|
||||
NotLongEnough,
|
||||
/// Too many bytes for the requested type
|
||||
TooLong,
|
||||
/// Invalid marker for (enums)
|
||||
UnknownMarker,
|
||||
}
|
||||
|
||||
/// Value that can be serialized from bytes array
|
||||
pub trait FromRawBytes: Sized {
|
||||
/// function that will instantiate and initialize object from slice
|
||||
fn from_bytes(d: &[u8]) -> Result<Self, FromBytesError>;
|
||||
}
|
||||
|
||||
impl<T> FromRawBytes for T where T: FixedHash {
|
||||
fn from_bytes(bytes: &[u8]) -> Result<Self, FromBytesError> {
|
||||
match bytes.len().cmp(&mem::size_of::<T>()) {
|
||||
Ordering::Less => return Err(FromBytesError::NotLongEnough),
|
||||
Ordering::Greater => return Err(FromBytesError::TooLong),
|
||||
Ordering::Equal => ()
|
||||
};
|
||||
|
||||
let mut res = T::zero();
|
||||
res.copy_raw(bytes);
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! sized_binary_map {
|
||||
($target_ty: ident) => {
|
||||
impl FromRawBytes for $target_ty {
|
||||
fn from_bytes(bytes: &[u8]) -> Result<Self, FromBytesError> {
|
||||
match bytes.len().cmp(&::std::mem::size_of::<$target_ty>()) {
|
||||
::std::cmp::Ordering::Less => return Err(FromBytesError::NotLongEnough),
|
||||
::std::cmp::Ordering::Greater => return Err(FromBytesError::TooLong),
|
||||
::std::cmp::Ordering::Equal => ()
|
||||
};
|
||||
let mut res: Self = 0;
|
||||
res.copy_raw(bytes);
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
impl ToBytesWithMap for $target_ty {
|
||||
fn to_bytes_map(&self) -> Vec<u8> {
|
||||
let sz = ::std::mem::size_of::<$target_ty>();
|
||||
let mut res = Vec::<u8>::with_capacity(sz);
|
||||
|
||||
let ip: *const $target_ty = self;
|
||||
let ptr: *const u8 = ip as *const _;
|
||||
unsafe {
|
||||
res.set_len(sz);
|
||||
::std::ptr::copy(ptr, res.as_mut_ptr(), sz);
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sized_binary_map!(u16);
|
||||
sized_binary_map!(u32);
|
||||
sized_binary_map!(u64);
|
||||
|
||||
/// Value that can be serialized from variable-length byte array
|
||||
pub trait FromRawBytesVariable: Sized {
|
||||
/// Create value from slice
|
||||
fn from_bytes_variable(bytes: &[u8]) -> Result<Self, FromBytesError>;
|
||||
}
|
||||
|
||||
impl<T> FromRawBytesVariable for T where T: FromRawBytes {
|
||||
fn from_bytes_variable(bytes: &[u8]) -> Result<Self, FromBytesError> {
|
||||
match bytes.len().cmp(&mem::size_of::<T>()) {
|
||||
Ordering::Less => return Err(FromBytesError::NotLongEnough),
|
||||
Ordering::Greater => return Err(FromBytesError::TooLong),
|
||||
Ordering::Equal => ()
|
||||
};
|
||||
|
||||
T::from_bytes(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRawBytesVariable for String {
|
||||
fn from_bytes_variable(bytes: &[u8]) -> Result<String, FromBytesError> {
|
||||
Ok(::std::str::from_utf8(bytes).unwrap().to_owned())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> FromRawBytesVariable for Vec<T> where T: FromRawBytes {
|
||||
fn from_bytes_variable(bytes: &[u8]) -> Result<Self, FromBytesError> {
|
||||
let size_of_t = mem::size_of::<T>();
|
||||
let length_in_chunks = bytes.len() / size_of_t;
|
||||
|
||||
let mut result = Vec::with_capacity(length_in_chunks);
|
||||
unsafe { result.set_len(length_in_chunks) };
|
||||
for i in 0..length_in_chunks {
|
||||
*result.get_mut(i).unwrap() = try!(T::from_bytes(
|
||||
&bytes[size_of_t * i..size_of_t * (i+1)]))
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl<V1, T2> FromRawBytes for (V1, T2) where V1: FromRawBytesVariable, T2: FromRawBytes {
|
||||
fn from_bytes(bytes: &[u8]) -> Result<Self, FromBytesError> {
|
||||
let header = 8usize;
|
||||
let mut map: (u64, ) = (0,);
|
||||
|
||||
if bytes.len() < header { return Err(FromBytesError::NotLongEnough); }
|
||||
map.copy_raw(&bytes[0..header]);
|
||||
|
||||
Ok((
|
||||
try!(V1::from_bytes_variable(&bytes[header..header + (map.0 as usize)])),
|
||||
try!(T2::from_bytes(&bytes[header + (map.0 as usize)..bytes.len()])),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<V1, V2, T3> FromRawBytes for (V1, V2, T3)
|
||||
where V1: FromRawBytesVariable,
|
||||
V2: FromRawBytesVariable,
|
||||
T3: FromRawBytes
|
||||
{
|
||||
fn from_bytes(bytes: &[u8]) -> Result<Self, FromBytesError> {
|
||||
let header = 16usize;
|
||||
let mut map: (u64, u64, ) = (0, 0,);
|
||||
|
||||
if bytes.len() < header { return Err(FromBytesError::NotLongEnough); }
|
||||
map.copy_raw(&bytes[0..header]);
|
||||
|
||||
let map_1 = (header, header + map.0 as usize);
|
||||
let map_2 = (map_1.1 as usize, map_1.1 as usize + map.1 as usize);
|
||||
Ok((
|
||||
try!(V1::from_bytes_variable(&bytes[map_1.0..map_1.1])),
|
||||
try!(V2::from_bytes_variable(&bytes[map_2.0..map_2.1])),
|
||||
try!(T3::from_bytes(&bytes[map_2.1..bytes.len()])),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, V1, X1, T2> ToBytesWithMap for (X1, &'a T2) where V1: ToBytesWithMap, X1: Deref<Target=[V1]>, T2: ToBytesWithMap {
|
||||
fn to_bytes_map(&self) -> Vec<u8> {
|
||||
let header = 8usize;
|
||||
let v1_size = mem::size_of::<V1>();
|
||||
let mut result = Vec::with_capacity(header + self.0.len() * v1_size + mem::size_of::<T2>());
|
||||
result.extend(((self.0.len() * v1_size) as u64).to_bytes_map());
|
||||
|
||||
for i in 0..self.0.len() {
|
||||
result.extend(self.0[i].to_bytes_map());
|
||||
}
|
||||
result.extend(self.1.to_bytes_map());
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl<'a, V1, X1, V2, X2, T3> ToBytesWithMap for (X1, X2, &'a T3)
|
||||
where V1: ToBytesWithMap, X1: Deref<Target=[V1]>,
|
||||
V2: ToBytesWithMap, X2: Deref<Target=[V2]>,
|
||||
T3: ToBytesWithMap
|
||||
{
|
||||
fn to_bytes_map(&self) -> Vec<u8> {
|
||||
let header = 16usize;
|
||||
let v1_size = mem::size_of::<V1>();
|
||||
let v2_size = mem::size_of::<V2>();
|
||||
let mut result = Vec::with_capacity(
|
||||
header +
|
||||
self.0.len() * v1_size +
|
||||
self.1.len() * v2_size +
|
||||
mem::size_of::<T3>()
|
||||
);
|
||||
result.extend(((self.0.len() * v1_size) as u64).to_bytes_map());
|
||||
result.extend(((self.1.len() * v2_size) as u64).to_bytes_map());
|
||||
for i in 0..self.0.len() {
|
||||
result.extend(self.0[i].to_bytes_map());
|
||||
}
|
||||
for i in 0..self.1.len() {
|
||||
result.extend(self.1[i].to_bytes_map());
|
||||
}
|
||||
result.extend(self.2.to_bytes_map());
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRawBytesVariable for Vec<u8> {
|
||||
fn from_bytes_variable(bytes: &[u8]) -> Result<Vec<u8>, FromBytesError> {
|
||||
Ok(bytes.to_vec())
|
||||
}
|
||||
}
|
||||
|
||||
/// Value that serializes directly to variable-sized byte array and stores map
|
||||
pub trait ToBytesWithMap {
|
||||
/// serialize to variable-sized byte array and store map
|
||||
fn to_bytes_map(&self) -> Vec<u8>;
|
||||
}
|
||||
|
||||
impl<T> ToBytesWithMap for T where T: FixedHash {
|
||||
fn to_bytes_map(&self) -> Vec<u8> {
|
||||
self.as_slice().to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fax_raw() {
|
||||
let mut x = [255u8; 4];
|
||||
@ -488,44 +240,3 @@ fn populate_big_types() {
|
||||
h.copy_raw_from(&a);
|
||||
assert_eq!(h, h256_from_hex("ffffffffffffffffffffffffffffffffffffffff000000000000000000000069"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_bytes_from_tuple() {
|
||||
type Tup = (Vec<u16>, u16);
|
||||
|
||||
let tup: (&[u16], u16) = (&[1; 4], 10);
|
||||
let bytes = vec![
|
||||
// map
|
||||
8u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
|
||||
// four 1u16
|
||||
1u8, 0u8,
|
||||
1u8, 0u8,
|
||||
1u8, 0u8,
|
||||
1u8, 0u8,
|
||||
// 10u16
|
||||
10u8, 0u8];
|
||||
|
||||
let (v, x) = Tup::from_bytes(&bytes).unwrap();
|
||||
assert_eq!(tup, (&v[..], x));
|
||||
let tup_from = (v, x);
|
||||
|
||||
let tup_to = (tup_from.0, &tup_from.1);
|
||||
let bytes_to = tup_to.to_bytes_map();
|
||||
assert_eq!(bytes_to, bytes);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytes_map_from_triple() {
|
||||
let data: (&[u16], &[u32], u64) = (&[2; 6], &[6; 3], 12u64);
|
||||
let bytes_map = (data.0, data.1, &data.2).to_bytes_map();
|
||||
assert_eq!(bytes_map, vec![
|
||||
// data map 2 x u64
|
||||
12, 0, 0, 0, 0, 0, 0, 0,
|
||||
12, 0, 0, 0, 0, 0, 0, 0,
|
||||
// vec![2u16; 6]
|
||||
2, 0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0,
|
||||
// vec![6u32; 3]
|
||||
6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0,
|
||||
// 12u64
|
||||
12, 0, 0, 0, 0, 0, 0, 0]);
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ use std::fmt;
|
||||
use std::cmp::Ordering;
|
||||
use std::error::Error as StdError;
|
||||
use bigint::uint::{Uint, U128, U256};
|
||||
use hash::FixedHash;
|
||||
use hash::{H64, H128, Address, H256, H512, H520, H2048};
|
||||
use elastic_array::*;
|
||||
|
||||
/// Vector like object
|
||||
@ -146,13 +146,25 @@ macro_rules! impl_uint_to_bytes {
|
||||
impl_uint_to_bytes!(U256);
|
||||
impl_uint_to_bytes!(U128);
|
||||
|
||||
impl <T>ToBytes for T where T: FixedHash {
|
||||
macro_rules! impl_hash_to_bytes {
|
||||
($name: ident) => {
|
||||
impl ToBytes for $name {
|
||||
fn to_bytes<V: VecLike<u8>>(&self, out: &mut V) {
|
||||
out.vec_extend(self.as_slice());
|
||||
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
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum FromBytesError {
|
||||
@ -250,15 +262,29 @@ macro_rules! impl_uint_from_bytes {
|
||||
impl_uint_from_bytes!(U256, 32);
|
||||
impl_uint_from_bytes!(U128, 16);
|
||||
|
||||
impl <T>FromBytes for T where T: FixedHash {
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<T> {
|
||||
match bytes.len().cmp(&T::len()) {
|
||||
Ordering::Less => return Err(FromBytesError::DataIsTooShort),
|
||||
Ordering::Greater => return Err(FromBytesError::DataIsTooLong),
|
||||
Ordering::Equal => ()
|
||||
};
|
||||
|
||||
Ok(T::from_slice(bytes))
|
||||
macro_rules! impl_hash_from_bytes {
|
||||
($name: ident, $size: expr) => {
|
||||
impl FromBytes for $name {
|
||||
fn from_bytes(bytes: &[u8]) -> FromBytesResult<$name> {
|
||||
match bytes.len().cmp(&$size) {
|
||||
Ordering::Less => Err(FromBytesError::DataIsTooShort),
|
||||
Ordering::Greater => Err(FromBytesError::DataIsTooLong),
|
||||
Ordering::Equal => {
|
||||
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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user