Merge branch 'master' of github.com:gavofyork/ethcore-util into network

This commit is contained in:
arkpar 2015-12-22 22:23:53 +01:00
commit 416c2ec3e4
5 changed files with 61 additions and 1 deletions

View File

@ -13,6 +13,7 @@ pub enum EthcoreError {
FromHex(FromHexError),
BaseData(BaseDataError),
BadSize,
UnknownName
}
impl From<FromHexError> for EthcoreError {
@ -39,4 +40,4 @@ macro_rules! assimilate {
)
}
assimilate!(FromHex);
assimilate!(BaseData);*/
assimilate!(BaseData);*/

View File

@ -66,6 +66,7 @@ pub mod trie;
pub mod nibbleslice;
pub mod heapsizeof;
pub mod squeeze;
pub mod semantic_version;
pub mod network;

29
src/semantic_version.rs Normal file
View File

@ -0,0 +1,29 @@
/// A version value with strict meaning. Use `to_u32` to convert to a simple integer.
///
/// # Example
/// ```
/// extern crate ethcore_util as util;
/// use util::semantic_version::*;
///
/// fn main() {
/// assert_eq!(SemanticVersion::new(1, 2, 3).as_u32(), 0x010203);
/// }
/// ```
pub struct SemanticVersion {
/// Major version - API/feature removals & breaking changes.
pub major: u8,
/// Minor version - API/feature additions.
pub minor: u8,
/// Tiny version - bug fixes.
pub tiny: u8,
}
impl SemanticVersion {
/// Create a new object.
pub fn new(major: u8, minor: u8, tiny: u8) -> SemanticVersion { SemanticVersion{major: major, minor: minor, tiny: tiny} }
/// Convert to a `u32` representation.
pub fn as_u32(&self) -> u32 { ((self.major as u32) << 16) + ((self.minor as u32) << 8) + self.tiny as u32 }
}
// TODO: implement Eq, Comparison and Debug/Display for SemanticVersion.

View File

@ -11,8 +11,11 @@ enum Operation {
Delete(H256),
}
/// How many insertions and removals were done in an `apply` operation.
pub struct Score {
/// Number of insertions.
pub inserts: usize,
/// Number of removals.
pub removes: usize,
}
@ -54,6 +57,7 @@ impl Journal {
}
}
/// Apply this journal to the HashDB `db` and return the number of insertions and removals done.
pub fn apply(self, db: &mut HashDB) -> Score {
trace!("applying {:?} changes", self.0.len());
let mut ret = Score{inserts: 0, removes: 0};

View File

@ -97,6 +97,16 @@ macro_rules! construct_uint {
}
}
#[inline]
pub fn zero() -> $name {
From::from(0u64)
}
#[inline]
pub fn one() -> $name {
From::from(1u64)
}
/// Multiplication by u32
fn mul_u32(self, other: u32) -> $name {
let $name(ref arr) = self;
@ -125,6 +135,21 @@ macro_rules! construct_uint {
impl_map_from!($name, u8, u64);
impl_map_from!($name, u16, u64);
impl_map_from!($name, u32, u64);
impl_map_from!($name, usize, u64);
impl From<i64> for $name {
fn from(value: i64) -> $name {
match value >= 0 {
true => From::from(value as u64),
false => { panic!("Unsigned integer can't be created from negative value"); }
}
}
}
impl_map_from!($name, i8, i64);
impl_map_from!($name, i16, i64);
impl_map_from!($name, i32, i64);
impl_map_from!($name, isize, i64);
impl<'a> From<&'a [u8]> for $name {
fn from(bytes: &[u8]) -> $name {