get rid of populatable and bytesconvertable traits (#2019)
This commit is contained in:
committed by
Arkadiy Paronyan
parent
3439c06a1c
commit
9a5668f802
@@ -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>;
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,16 +18,13 @@
|
||||
extern crate sha3 as sha3_ext;
|
||||
|
||||
use std::io;
|
||||
use std::mem::uninitialized;
|
||||
use tiny_keccak::Keccak;
|
||||
use bytes::{BytesConvertable, Populatable};
|
||||
use hash::{H256, FixedHash};
|
||||
use self::sha3_ext::*;
|
||||
|
||||
/// Get the SHA3 (i.e. Keccak) hash of the empty bytes string.
|
||||
pub const SHA3_EMPTY: H256 = H256( [0xc5, 0xd2, 0x46, 0x01, 0x86, 0xf7, 0x23, 0x3c, 0x92, 0x7e, 0x7d, 0xb2, 0xdc, 0xc7, 0x03, 0xc0, 0xe5, 0x00, 0xb6, 0x53, 0xca, 0x82, 0x27, 0x3b, 0x7b, 0xfa, 0xd8, 0x04, 0x5d, 0x85, 0xa4, 0x70] );
|
||||
|
||||
|
||||
/// Types implementing this trait are sha3able.
|
||||
///
|
||||
/// ```
|
||||
@@ -50,17 +47,16 @@ pub trait Hashable {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Hashable for T where T: BytesConvertable {
|
||||
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_slice();
|
||||
sha3_256(dest.as_mut_ptr(), dest.len(), input.as_ptr(), input.len());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user