Skip to content

Commit

Permalink
make rand/getrandom dependency explicit with a feature (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
lightyear15 committed Jan 14, 2022
1 parent 452f5d5 commit 9501c25
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
15 changes: 8 additions & 7 deletions Cargo.toml
Expand Up @@ -11,23 +11,24 @@ categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
edition = "2018"
resolver = "2"

[dependencies]
rand = { version = "0.8", default-features = false, features = ["std_rng"] }
rand = { version = "0.8", default-features = false, features = ["std_rng"]}
rayon = { version = "1", optional = true }
colored = { version = "2", optional = true }
num-traits = { version = "0.2", default-features = false }

[dev-dependencies]
rand = { version = "0.8", features = ["std"]}


[features]
default = [ "std" ]
std = [ "rand/std" ]
std = []
parallel = [ "rayon", "std" ]
print-trace = [ "std", "colored" ]

[profile.release]
opt-level = 3
lto = "thin"
incremental = true
getrandom = ["rand/std"]

[profile.bench]
opt-level = 3
Expand Down
28 changes: 20 additions & 8 deletions src/rand_helper.rs
@@ -1,10 +1,10 @@
#[cfg(feature = "std")]
use rand::RngCore;
use rand::{
distributions::{Distribution, Standard},
prelude::StdRng,
Rng,
};
#[cfg(feature = "std")]
use rand::{prelude::ThreadRng, RngCore};

pub use rand;

Expand Down Expand Up @@ -41,20 +41,28 @@ pub fn test_rng() -> impl rand::Rng {
/// Should be used only for tests, not for any real world usage.
#[cfg(feature = "std")]
pub fn test_rng() -> impl rand::Rng {
let is_deterministic =
std::env::vars().any(|(key, val)| key == "DETERMINISTIC_TEST_RNG" && val == "1");
if is_deterministic {
#[cfg(any(feature = "getrandom", test))]
{
let is_deterministic =
std::env::vars().any(|(key, val)| key == "DETERMINISTIC_TEST_RNG" && val == "1");
if is_deterministic {
RngWrapper::Deterministic(test_rng_helper())
} else {
RngWrapper::Randomized(rand::thread_rng())
}
}
#[cfg(not(any(feature = "getrandom", test)))]
{
RngWrapper::Deterministic(test_rng_helper())
} else {
RngWrapper::Randomized(rand::thread_rng())
}
}

/// Helper wrapper to enable `test_rng` to return `impl::Rng`.
#[cfg(feature = "std")]
enum RngWrapper {
Deterministic(StdRng),
Randomized(ThreadRng),
#[cfg(any(feature = "getrandom", test))]
Randomized(rand::rngs::ThreadRng),
}

#[cfg(feature = "std")]
Expand All @@ -63,6 +71,7 @@ impl RngCore for RngWrapper {
fn next_u32(&mut self) -> u32 {
match self {
Self::Deterministic(rng) => rng.next_u32(),
#[cfg(any(feature = "getrandom", test))]
Self::Randomized(rng) => rng.next_u32(),
}
}
Expand All @@ -71,6 +80,7 @@ impl RngCore for RngWrapper {
fn next_u64(&mut self) -> u64 {
match self {
Self::Deterministic(rng) => rng.next_u64(),
#[cfg(any(feature = "getrandom", test))]
Self::Randomized(rng) => rng.next_u64(),
}
}
Expand All @@ -79,6 +89,7 @@ impl RngCore for RngWrapper {
fn fill_bytes(&mut self, dest: &mut [u8]) {
match self {
Self::Deterministic(rng) => rng.fill_bytes(dest),
#[cfg(any(feature = "getrandom", test))]
Self::Randomized(rng) => rng.fill_bytes(dest),
}
}
Expand All @@ -87,6 +98,7 @@ impl RngCore for RngWrapper {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
match self {
Self::Deterministic(rng) => rng.try_fill_bytes(dest),
#[cfg(any(feature = "getrandom", test))]
Self::Randomized(rng) => rng.try_fill_bytes(dest),
}
}
Expand Down

0 comments on commit 9501c25

Please sign in to comment.