Merge branch 'master' of github.com:gavofyork/ethcore-util into io
This commit is contained in:
commit
f72f7d05e9
26
src/bytes.rs
26
src/bytes.rs
@ -39,6 +39,7 @@ use std::fmt;
|
||||
use std::slice;
|
||||
use std::cmp::Ordering;
|
||||
use std::error::Error as StdError;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use uint::{U128, U256};
|
||||
use hash::FixedHash;
|
||||
|
||||
@ -89,6 +90,31 @@ impl ToPretty for Bytes {
|
||||
}
|
||||
}
|
||||
|
||||
pub enum BytesRef<'a> {
|
||||
Flexible(&'a mut Bytes),
|
||||
Fixed(&'a mut [u8])
|
||||
}
|
||||
|
||||
impl<'a> Deref for BytesRef<'a> {
|
||||
type Target = [u8];
|
||||
|
||||
fn deref(&self) -> &[u8] {
|
||||
match self {
|
||||
&BytesRef::Flexible(ref bytes) => bytes,
|
||||
&BytesRef::Fixed(ref bytes) => bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a> DerefMut for BytesRef<'a> {
|
||||
fn deref_mut(&mut self) -> &mut [u8] {
|
||||
match self {
|
||||
&mut BytesRef::Flexible(ref mut bytes) => bytes,
|
||||
&mut BytesRef::Fixed(ref mut bytes) => bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Vector of bytes
|
||||
pub type Bytes = Vec<u8>;
|
||||
|
||||
|
48
src/hash.rs
48
src/hash.rs
@ -35,6 +35,14 @@ pub trait FixedHash: Sized + BytesConvertable + Populatable {
|
||||
fn is_zero(&self) -> bool;
|
||||
}
|
||||
|
||||
fn clean_0x(s: &str) -> &str {
|
||||
if s.len() >= 2 && &s[0..2] == "0x" {
|
||||
&s[2..]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_hash {
|
||||
($from: ident, $size: expr) => {
|
||||
#[derive(Eq)]
|
||||
@ -377,6 +385,30 @@ macro_rules! impl_hash {
|
||||
|
||||
pub fn from_bloomed<T>(b: &T) -> Self where T: FixedHash { b.bloom_part($size) }
|
||||
}
|
||||
|
||||
impl From<u64> for $from {
|
||||
fn from(mut value: u64) -> $from {
|
||||
let mut ret = $from::new();
|
||||
for i in 0..8 {
|
||||
if i < $size {
|
||||
ret.0[$size - i - 1] = (value & 0xff) as u8;
|
||||
value >>= 8;
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
||||
|
||||
impl<'_> From<&'_ str> for $from {
|
||||
fn from(s: &'_ str) -> $from {
|
||||
use std::str::FromStr;
|
||||
if s.len() % 2 == 1 {
|
||||
$from::from_str(&("0".to_string() + &(clean_0x(s).to_string()))[..]).unwrap_or($from::new())
|
||||
} else {
|
||||
$from::from_str(clean_0x(s)).unwrap_or($from::new())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -505,5 +537,21 @@ mod tests {
|
||||
let a = Address::from(h);
|
||||
assert_eq!(address, a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_u64() {
|
||||
assert_eq!(H128::from(0x1234567890abcdef), H128::from_str("00000000000000001234567890abcdef").unwrap());
|
||||
assert_eq!(H64::from(0x1234567890abcdef), H64::from_str("1234567890abcdef").unwrap());
|
||||
assert_eq!(H32::from(0x1234567890abcdef), H32::from_str("90abcdef").unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_str() {
|
||||
assert_eq!(H64::from(0x1234567890abcdef), H64::from("0x1234567890abcdef"));
|
||||
assert_eq!(H64::from(0x1234567890abcdef), H64::from("1234567890abcdef"));
|
||||
assert_eq!(H64::from(0x234567890abcdef), H64::from("0x234567890abcdef"));
|
||||
// too short.
|
||||
assert_eq!(H64::from(0), H64::from("0x34567890abcdef"));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user