merge with master

This commit is contained in:
Robert Habermeier
2016-09-01 15:07:06 +02:00
64 changed files with 718 additions and 724 deletions

View File

@@ -14,26 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Unified interfaces for bytes operations on basic types
//! General bytes-related utilities.
//!
//! # Examples
//! ```rust
//! extern crate ethcore_util as util;
//!
//! fn bytes_convertable() {
//! use util::bytes::BytesConvertable;
//!
//! let arr = [0; 5];
//! let slice: &[u8] = arr.as_slice();
//! }
//!
//! fn main() {
//! bytes_convertable();
//! }
//! ```
//! Includes a pretty-printer for bytes, in the form of `ToPretty` and `PrettySlice`
//! as
use std::fmt;
use std::slice;
use std::ops::{Deref, DerefMut};
/// Slice pretty print helper
@@ -71,20 +57,9 @@ pub trait ToPretty {
}
}
impl<'a> ToPretty for &'a [u8] {
impl<T: AsRef<[u8]>> ToPretty for T {
fn pretty(&self) -> PrettySlice {
PrettySlice(self)
}
}
impl<'a> ToPretty for &'a Bytes {
fn pretty(&self) -> PrettySlice {
PrettySlice(self.as_slice())
}
}
impl ToPretty for Bytes {
fn pretty(&self) -> PrettySlice {
PrettySlice(self.as_slice())
PrettySlice(self.as_ref())
}
}
@@ -116,128 +91,5 @@ impl <'a> DerefMut for BytesRef<'a> {
}
}
/// Vector of bytes
pub type Bytes = Vec<u8>;
/// Slice of bytes to underlying memory
pub trait BytesConvertable {
/// Get the underlying byte-wise representation of the value.
fn as_slice(&self) -> &[u8];
/// Get a copy of the underlying byte-wise representation.
fn to_bytes(&self) -> Bytes { self.as_slice().to_vec() }
}
impl<T> BytesConvertable for T where T: AsRef<[u8]> {
fn as_slice(&self) -> &[u8] { self.as_ref() }
}
#[test]
fn bytes_convertable() {
assert_eq!(vec![0x12u8, 0x34].as_slice(), &[0x12u8, 0x34]);
assert!([0u8; 0].as_slice().is_empty());
}
/// Simple trait to allow for raw population of a Sized object from a byte slice.
pub trait Populatable {
/// Copies a bunch of bytes `d` to `self`, overwriting as necessary.
///
/// If `d` is smaller, zero-out the remaining bytes.
fn populate_raw(&mut self, d: &[u8]) {
let mut s = self.as_slice_mut();
for i in 0..s.len() {
s[i] = if i < d.len() {d[i]} else {0};
}
}
/// Copies a bunch of bytes `d` to `self`, overwriting as necessary.
///
/// If `d` is smaller, will leave some bytes untouched.
fn copy_raw(&mut self, d: &[u8]) {
use std::io::Write;
self.as_slice_mut().write(d).unwrap();
}
/// Copies the raw representation of an object `d` to `self`, overwriting as necessary.
///
/// If `d` is smaller, zero-out the remaining bytes.
fn populate_raw_from(&mut self, d: &BytesConvertable) { self.populate_raw(d.as_slice()); }
/// Copies the raw representation of an object `d` to `self`, overwriting as necessary.
///
/// If `d` is smaller, will leave some bytes untouched.
fn copy_raw_from(&mut self, d: &BytesConvertable) { self.copy_raw(d.as_slice()); }
/// Get the raw slice for this object.
fn as_slice_mut(&mut self) -> &mut [u8];
}
impl<T> Populatable for T where T: Sized {
fn as_slice_mut(&mut self) -> &mut [u8] {
use std::mem;
unsafe {
slice::from_raw_parts_mut(self as *mut T as *mut u8, mem::size_of::<T>())
}
}
}
impl<T> Populatable for [T] where T: Sized {
fn as_slice_mut(&mut self) -> &mut [u8] {
use std::mem;
unsafe {
slice::from_raw_parts_mut(self.as_mut_ptr() as *mut u8, mem::size_of::<T>() * self.len())
}
}
}
#[test]
fn fax_raw() {
let mut x = [255u8; 4];
x.copy_raw(&[1u8; 2][..]);
assert_eq!(x, [1u8, 1, 255, 255]);
let mut x = [255u8; 4];
x.copy_raw(&[1u8; 6][..]);
assert_eq!(x, [1u8, 1, 1, 1]);
}
#[test]
fn populate_raw() {
let mut x = [255u8; 4];
x.populate_raw(&[1u8; 2][..]);
assert_eq!(x, [1u8, 1, 0, 0]);
let mut x = [255u8; 4];
x.populate_raw(&[1u8; 6][..]);
assert_eq!(x, [1u8, 1, 1, 1]);
}
#[test]
fn populate_raw_dyn() {
let mut x = [255u8; 4];
x.populate_raw(&[1u8; 2][..]);
assert_eq!(&x[..], [1u8, 1, 0, 0]);
let mut x = [255u8; 4];
x.populate_raw(&[1u8; 6][..]);
assert_eq!(&x[..], [1u8, 1, 1, 1]);
}
#[test]
fn fax_raw_dyn() {
let mut x = [255u8; 4];
x.copy_raw(&[1u8; 2][..]);
assert_eq!(&x[..], [1u8, 1, 255, 255]);
let mut x = [255u8; 4];
x.copy_raw(&[1u8; 6][..]);
assert_eq!(&x[..], [1u8, 1, 1, 1]);
}
#[test]
fn populate_big_types() {
use hash::*;
let a: H160 = "ffffffffffffffffffffffffffffffffffffffff".into();
let mut h: H256 = 0x69.into();
h.populate_raw_from(&a);
assert_eq!(h, "ffffffffffffffffffffffffffffffffffffffff000000000000000000000000".into());
let mut h: H256 = 0x69.into();
h.copy_raw_from(&a);
assert_eq!(h, "ffffffffffffffffffffffffffffffffffffffff000000000000000000000069".into());
}
/// Vector of bytes.
pub type Bytes = Vec<u8>;

View File

@@ -74,7 +74,7 @@ impl fmt::Display for UtilError {
}
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
/// Error indicating an expected value was not found.
pub struct Mismatch<T: fmt::Debug> {
/// Value expected.
@@ -89,7 +89,7 @@ impl<T: fmt::Debug + fmt::Display> fmt::Display for Mismatch<T> {
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
/// Error indicating value found is outside of a valid range.
pub struct OutOfBounds<T: fmt::Debug> {
/// Minimum allowed value.

View File

@@ -163,7 +163,6 @@ impl JournalDB for ArchiveDB {
for i in self.overlay.drain().into_iter() {
let (key, (value, rc)) = i;
if rc > 0 {
assert!(rc == 1);
batch.put(self.column, &key, &value);
inserts += 1;
}
@@ -192,7 +191,6 @@ impl JournalDB for ArchiveDB {
for i in self.overlay.drain().into_iter() {
let (key, (value, rc)) = i;
if rc > 0 {
assert!(rc == 1);
if try!(self.backing.get(self.column, &key)).is_some() {
return Err(BaseDataError::AlreadyExists(key).into());
}

View File

@@ -95,7 +95,7 @@ impl EarlyMergeDB {
}
fn morph_key(key: &H256, index: u8) -> Bytes {
let mut ret = key.to_bytes();
let mut ret = (&**key).to_owned();
ret.push(index);
ret
}

View File

@@ -296,8 +296,8 @@ fn memorydb_denote() {
for _ in 0..1000 {
let r = H256::random();
let k = r.sha3();
let (v, rc) = m.denote(&k, r.to_bytes());
assert_eq!(v, r.as_slice());
let (v, rc) = m.denote(&k, r.to_vec());
assert_eq!(v, &*r);
assert_eq!(rc, 0);
}

View File

@@ -18,9 +18,7 @@
extern crate sha3 as sha3_ext;
use std::io;
use std::mem::uninitialized;
use tiny_keccak::Keccak;
use bytes::{Populatable};
use hash::{H256, FixedHash};
use self::sha3_ext::*;
@@ -57,15 +55,14 @@ pub trait Hashable {
impl<T> Hashable for T where T: AsRef<[u8]> {
fn sha3(&self) -> H256 {
unsafe {
let mut ret: H256 = uninitialized();
self.sha3_into(ret.as_slice_mut());
ret
}
let mut ret: H256 = H256::zero();
self.sha3_into(&mut *ret);
ret
}
fn sha3_into(&self, dest: &mut [u8]) {
let input: &[u8] = self.as_ref();
unsafe {
let input: &[u8] = self.as_ref();
sha3_256(dest.as_mut_ptr(), dest.len(), input.as_ptr(), input.len());
}
}