diff --git a/ethbloom/Cargo.toml b/ethbloom/Cargo.toml index 62048d9..a91e068 100644 --- a/ethbloom/Cargo.toml +++ b/ethbloom/Cargo.toml @@ -12,15 +12,15 @@ repository = "https://github.com/debris/ethbloom" tiny-keccak = "1.4" crunchy = { version = "0.1.6", features = ["limit_256"] } fixed-hash = { version = "0.1.3", path = "../fixed-hash" } -ethereum-types-serialize = { version = "0.2", path = "../serialize", optional = true } +ethereum-types-serialize = { version = "0.2.1", path = "../serialize", optional = true } serde = { version = "1.0", optional = true } [dev-dependencies] -rand = "0.4" +rand = { version = "0.4" } rustc-hex = "1.0" [features] -default = ["std", "heapsizeof", "serialize"] +default = ["std", "heapsizeof", "serialize", "fixed-hash/libc"] std = ["fixed-hash/std"] heapsizeof = ["fixed-hash/heapsizeof"] serialize = ["std", "ethereum-types-serialize", "serde"] diff --git a/fixed-hash/Cargo.toml b/fixed-hash/Cargo.toml index db1ef84..9b2a91e 100644 --- a/fixed-hash/Cargo.toml +++ b/fixed-hash/Cargo.toml @@ -8,10 +8,11 @@ description = "Fixed-size hashes" [dependencies] heapsize = { version = "0.4", optional = true } -libc = { version = "0.2", default-features = false } +libc = { version = "0.2", optional = true, default-features = false } rand = { version = "0.4", optional = true } rustc-hex = { version = "1.0", optional = true } [features] +default = ["libc"] std = ["rustc-hex", "rand"] heapsizeof = ["heapsize"] diff --git a/fixed-hash/src/hash.rs b/fixed-hash/src/hash.rs index 58c561a..1c0109e 100644 --- a/fixed-hash/src/hash.rs +++ b/fixed-hash/src/hash.rs @@ -157,21 +157,6 @@ macro_rules! construct_hash { impl Eq for $from {} - impl PartialEq for $from { - fn eq(&self, other: &Self) -> bool { - unsafe { $crate::libc::memcmp(self.0.as_ptr() as *const $crate::libc::c_void, other.0.as_ptr() as *const $crate::libc::c_void, $size) == 0 } - } - } - - impl Ord for $from { - fn cmp(&self, other: &Self) -> ::core::cmp::Ordering { - let r = unsafe { $crate::libc::memcmp(self.0.as_ptr() as *const $crate::libc::c_void, other.0.as_ptr() as *const $crate::libc::c_void, $size) }; - if r < 0 { return ::core::cmp::Ordering::Less } - if r > 0 { return ::core::cmp::Ordering::Greater } - return ::core::cmp::Ordering::Equal; - } - } - impl PartialOrd for $from { fn partial_cmp(&self, other: &Self) -> Option<::core::cmp::Ordering> { Some(self.cmp(other)) @@ -313,6 +298,7 @@ macro_rules! construct_hash { impl_std_for_hash!($from, $size); impl_heapsize_for_hash!($from); + impl_libc_for_hash!($from, $size); } } @@ -421,3 +407,57 @@ macro_rules! impl_std_for_hash_internals { macro_rules! impl_std_for_hash_internals { ($from: ident, $size: tt) => {} } + +#[cfg(feature="libc")] +#[macro_export] +#[doc(hidden)] +macro_rules! impl_heapsize_for_hash { + ($name: ident) => { + impl $crate::heapsize::HeapSizeOf for $name { + fn heap_size_of_children(&self) -> usize { + 0 + } + } + } +} + +#[cfg(feature="libc")] +#[macro_export] +#[doc(hidden)] +macro_rules! impl_libc_for_hash { + ($from: ident, $size: expr) => { + impl PartialEq for $from { + fn eq(&self, other: &Self) -> bool { + unsafe { $crate::libc::memcmp(self.0.as_ptr() as *const $crate::libc::c_void, other.0.as_ptr() as *const $crate::libc::c_void, $size) == 0 } + } + } + + impl Ord for $from { + fn cmp(&self, other: &Self) -> ::core::cmp::Ordering { + let r = unsafe { $crate::libc::memcmp(self.0.as_ptr() as *const $crate::libc::c_void, other.0.as_ptr() as *const $crate::libc::c_void, $size) }; + if r < 0 { return ::core::cmp::Ordering::Less } + if r > 0 { return ::core::cmp::Ordering::Greater } + return ::core::cmp::Ordering::Equal; + } + } + } +} + +#[cfg(not(feature="libc"))] +#[macro_export] +#[doc(hidden)] +macro_rules! impl_libc_for_hash { + ($from: ident, $size: expr) => { + impl PartialEq for $from { + fn eq(&self, other: &Self) -> bool { + &self.0[..] == &other.0[..] + } + } + + impl Ord for $from { + fn cmp(&self, other: &Self) -> ::core::cmp::Ordering { + self.0[..].cmp(&other.0[..]) + } + } + } +} diff --git a/fixed-hash/src/lib.rs b/fixed-hash/src/lib.rs index 0888b59..8cbf255 100644 --- a/fixed-hash/src/lib.rs +++ b/fixed-hash/src/lib.rs @@ -6,9 +6,14 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(feature="libc")] #[doc(hidden)] pub extern crate libc; +#[cfg(not(feature="libc"))] +#[doc(hidden)] +pub mod libc { } + #[cfg(feature="heapsizeof")] #[doc(hidden)] pub extern crate heapsize; diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 20e942a..d1bd503 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -8,7 +8,7 @@ rustc_version = "0.2" [dependencies] crunchy = "0.1.5" -ethereum-types = { path ="../ethereum-types", features = ["std", "heapsizeof"] } +ethereum-types = { version = "0.2.3", path ="../ethereum-types", features = ["std", "heapsizeof"] } quickcheck = "0.6" serde_json = "1.0" uint = { path = "../uint" }