Skip to content

Commit

Permalink
Configure features automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
tkaitchuck committed Nov 21, 2020
1 parent 7803a23 commit e36cab3
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 37 deletions.
19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ bench = true
doc = true

[features]
default = ["std", "runtime-rng"]
default = ["std"]

# Enabling this will enable `AHashMap` and `AHashSet`.
std = []

# This will enable runtime key generation.
runtime-rng = ["getrandom", "lazy_static"]

# This is an alternitive to runtime-rng which does compile time key generation so that gerrandom is not required.
# This implies the produced binary will not be identical. If both this and `runtime-rng` are disabled constant keys are used.
# This is an alternitive to runtime key generation which does compile time key generation if gerrandom is not available.
# This implies the produced binary will not be identical. If this is disabled and gerrandom is unavailable constant keys are used.
compile-time-rng = ["const-random"]

[[bench]]
Expand Down Expand Up @@ -64,9 +61,13 @@ codegen-units = 1
[build-dependencies]
version_check = "0.9"

[dependencies]
lazy_static = { version = "1.4.0", features = ["spin_no_std"], optional = true }
getrandom = { version = "0.2.0", optional = true }
[target.'cfg(any(target_os = "linux", target_os = "android", target_os = "windows", target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "dragonfly", target_os = "solaris", target_os = "illumos", target_os = "fuchsia", target_os = "redox", target_os = "cloudabi", target_os = "haiku", target_os = "vxworks", target_os = "emscripten", target_os = "wasi"))'.dependencies]
lazy_static = { version = "1.4.0" }
getrandom = { version = "0.2.0" }
const-random = { version = "0.1.6", optional = true }
serde = { version = "1.0.117", optional = true }

[target.'cfg(not(any(target_os = "linux", target_os = "android", target_os = "windows", target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "dragonfly", target_os = "solaris", target_os = "illumos", target_os = "fuchsia", target_os = "redox", target_os = "cloudabi", target_os = "haiku", target_os = "vxworks", target_os = "emscripten", target_os = "wasi")))'.dependencies]
const-random = { version = "0.1.6", optional = true }
serde = { version = "1.0.117", optional = true }

Expand Down
26 changes: 25 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
#![deny(warnings)]

use std::env;

fn main() {
println!("cargo:rerun-if-changed=build.rs");
if let Some(channel) = version_check::Channel::read() {
if channel.supports_features() {
println!("cargo:rustc-cfg=specialize");
println!("cargo:rustc-cfg=feature=\"specialize\"");
}
}
let os = env::var("CARGO_CFG_TARGET_OS").expect("CARGO_CFG_TARGET_OS was not set");
if os.eq_ignore_ascii_case("linux") ||
os.eq_ignore_ascii_case("android") ||
os.eq_ignore_ascii_case("windows") ||
os.eq_ignore_ascii_case("macos") ||
os.eq_ignore_ascii_case("ios") ||
os.eq_ignore_ascii_case("freebsd") ||
os.eq_ignore_ascii_case("openbsd") ||
os.eq_ignore_ascii_case("dragonfly") ||
os.eq_ignore_ascii_case("solaris") ||
os.eq_ignore_ascii_case("illumos") ||
os.eq_ignore_ascii_case("fuchsia") ||
os.eq_ignore_ascii_case("redox") ||
os.eq_ignore_ascii_case("cloudabi") ||
os.eq_ignore_ascii_case("haiku") ||
os.eq_ignore_ascii_case("vxworks") ||
os.eq_ignore_ascii_case("emscripten") ||
os.eq_ignore_ascii_case("wasi") {
println!("cargo:rustc-cfg=feature=\"runtime-rng\"");
}
}
4 changes: 2 additions & 2 deletions src/aes_hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::convert::*;
use crate::operations::*;
#[cfg(specialize)]
#[cfg(feature = "specialize")]
use crate::HasherExt;
use core::hash::Hasher;
use crate::RandomState;
Expand Down Expand Up @@ -91,7 +91,7 @@ impl AHasher {
}
}

#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl HasherExt for AHasher {
#[inline]
fn hash_u64(self, value: u64) -> u64 {
Expand Down
4 changes: 2 additions & 2 deletions src/fallback_hash.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::convert::*;
use crate::operations::folded_multiply;
#[cfg(specialize)]
#[cfg(feature = "specialize")]
use crate::HasherExt;
use core::hash::Hasher;
use crate::RandomState;
Expand Down Expand Up @@ -106,7 +106,7 @@ impl AHasher {
}
}

#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl HasherExt for AHasher {
#[inline]
fn hash_u64(self, value: u64) -> u64 {
Expand Down
4 changes: 2 additions & 2 deletions src/hash_quality_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ mod fallback_tests {
#[test]
fn fallback_keys_affect_every_byte() {
//For fallback second key is not used in every hash.
#[cfg(not(specialize))]
#[cfg(not(feature = "specialize"))]
test_keys_affect_every_byte(0, |a, b| AHasher::new_with_keys(a ^ b, a));
test_keys_affect_every_byte("", |a, b| AHasher::new_with_keys(a ^ b, a));
test_keys_affect_every_byte((0, 0), |a, b| AHasher::new_with_keys(a ^ b, a));
Expand Down Expand Up @@ -437,7 +437,7 @@ mod aes_tests {

#[test]
fn aes_keys_affect_every_byte() {
#[cfg(not(specialize))]
#[cfg(not(feature = "specialize"))]
test_keys_affect_every_byte(0, AHasher::new_with_keys);
test_keys_affect_every_byte("", AHasher::new_with_keys);
test_keys_affect_every_byte((0, 0), AHasher::new_with_keys);
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#![deny(clippy::correctness, clippy::complexity, clippy::perf)]
#![allow(clippy::pedantic, clippy::cast_lossless, clippy::unreadable_literal)]
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
#![cfg_attr(specialize, feature(min_specialization))]
#![cfg_attr(feature = "specialize", feature(min_specialization))]

#[macro_use]
mod convert;
Expand Down Expand Up @@ -127,22 +127,22 @@ pub(crate) trait HasherExt: Hasher {

impl<T: Hasher> HasherExt for T {
#[inline]
#[cfg(specialize)]
#[cfg(feature = "specialize")]
default fn hash_u64(self, value: u64) -> u64 {
value.get_hash(self)
}
#[inline]
#[cfg(not(specialize))]
#[cfg(not(feature = "specialize"))]
fn hash_u64(self, value: u64) -> u64 {
value.get_hash(self)
}
#[inline]
#[cfg(specialize)]
#[cfg(feature = "specialize")]
default fn short_finish(&self) -> u64 {
self.finish()
}
#[inline]
#[cfg(not(specialize))]
#[cfg(not(feature = "specialize"))]
fn short_finish(&self) -> u64 {
self.finish()
}
Expand Down
6 changes: 6 additions & 0 deletions src/random_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ mod test {
assert_ne!(a.build_hasher().finish(), b.build_hasher().finish());
}

#[cfg(feature = "runtime-rng")]
#[test]
fn test_not_pi() {
assert_ne!(PI, seeds());
}

#[test]
fn test_with_seeds_const() {
const _CONST_RANDOM_STATE: RandomState = RandomState::with_seeds(17, 19, 21, 23);
Expand Down
32 changes: 16 additions & 16 deletions src/specialize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(specialize)]
#[cfg(feature = "specialize")]
use crate::HasherExt;
use core::hash::Hash;
use core::hash::Hasher;
Expand All @@ -21,7 +21,7 @@ pub trait CallHasher: Hash {
fn get_hash<H: Hasher>(&self, hasher: H) -> u64;
}

#[cfg(not(specialize))]
#[cfg(not(feature = "specialize"))]
impl<T> CallHasher for T
where
T: Hash,
Expand All @@ -33,7 +33,7 @@ where
}
}

#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl<T> CallHasher for T
where
T: Hash,
Expand All @@ -47,14 +47,14 @@ where

macro_rules! call_hasher_impl {
($typ:ty) => {
#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl CallHasher for $typ {
#[inline]
fn get_hash<H: Hasher>(&self, hasher: H) -> u64 {
hasher.hash_u64(*self as u64)
}
}
#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl CallHasher for &$typ {
#[inline]
fn get_hash<H: Hasher>(&self, hasher: H) -> u64 {
Expand All @@ -72,7 +72,7 @@ call_hasher_impl!(i16);
call_hasher_impl!(i32);
call_hasher_impl!(i64);

#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl CallHasher for u128 {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -81,7 +81,7 @@ impl CallHasher for u128 {
}
}

#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl CallHasher for i128 {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -90,7 +90,7 @@ impl CallHasher for i128 {
}
}

#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl CallHasher for [u8] {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -99,7 +99,7 @@ impl CallHasher for [u8] {
}
}

#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl CallHasher for &[u8] {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -108,7 +108,7 @@ impl CallHasher for &[u8] {
}
}

#[cfg(all(specialize, feature = "std"))]
#[cfg(all(feature = "specialize", feature = "std"))]
impl CallHasher for Vec<u8> {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -117,7 +117,7 @@ impl CallHasher for Vec<u8> {
}
}

#[cfg(all(specialize, feature = "std"))]
#[cfg(all(feature = "specialize", feature = "std"))]
impl CallHasher for &Vec<u8> {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -126,7 +126,7 @@ impl CallHasher for &Vec<u8> {
}
}

#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl CallHasher for str {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -135,7 +135,7 @@ impl CallHasher for str {
}
}

#[cfg(specialize)]
#[cfg(feature = "specialize")]
impl CallHasher for &str {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -144,7 +144,7 @@ impl CallHasher for &str {
}
}

#[cfg(all(specialize, feature = "std"))]
#[cfg(all(feature = "specialize", feature = "std"))]
impl CallHasher for String {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -153,7 +153,7 @@ impl CallHasher for String {
}
}

#[cfg(all(specialize, feature = "std"))]
#[cfg(all(feature = "specialize", feature = "std"))]
impl CallHasher for &String {
#[inline]
fn get_hash<H: Hasher>(&self, mut hasher: H) -> u64 {
Expand All @@ -168,7 +168,7 @@ mod test {
use crate::*;

#[test]
#[cfg(specialize)]
#[cfg(feature = "specialize")]
pub fn test_specialized_invoked() {
let shortened = 0_u64.get_hash(AHasher::new_with_keys(1, 2));
let mut hasher = AHasher::new_with_keys(1, 2);
Expand Down

0 comments on commit e36cab3

Please sign in to comment.