From 8e6e5f9e68d33ff906acd1f37f9eb38e5e33c7a5 Mon Sep 17 00:00:00 2001 From: "Lorenzo (Mec-iS)" Date: Tue, 8 Nov 2022 11:47:31 +0000 Subject: [PATCH] Use getrandom as default (for no-std feature) --- .github/CONTRIBUTING.md | 6 ++++++ .gitignore | 3 ++- Cargo.toml | 20 +++++++++----------- src/rand_custom.rs | 7 ++++++- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 48bce72..15b3906 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -25,11 +25,17 @@ Take a look to the conventions established by existing code: * Every module should provide a Rust doctest, a brief test embedded with the documentation that explains how to use the procedure implemented. * Every module should provide comprehensive tests at the end, in its `mod tests {}` sub-module. These tests can be flagged or not with configuration flags to allow WebAssembly target. * Run `cargo doc --no-deps --open` and read the generated documentation in the browser to be sure that your changes reflects in the documentation and new code is documented. + +#### digging deeper * a nice overview of the codebase is given by [static analyzer](https://mozilla.github.io/rust-code-analysis/metrics.html): ``` $ cargo install rust-code-analysis-cli +// print metrics for every module $ rust-code-analysis-cli -m -O json -o . -p src/ --pr +// print full AST for a module +$ rust-code-analysis-cli -p src/algorithm/neighbour/fastpair.rs --ls 22 --le 213 -d > ast.txt ``` +* find more information about what happens in your binary with [`twiggy`](https://rustwasm.github.io/twiggy/install.html). This need a compiled binary so create a brief `main {}` function using `smartcore` and then point `twiggy` to that file. ## Issue Report Process diff --git a/.gitignore b/.gitignore index e2976f7..0983a15 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ out.svg FlameGraph/ out.stacks -*.json \ No newline at end of file +*.json +*.txt \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 0c3adda..0dc84ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ exclude = [ ".gitignore", "smartcore.iml", "smartcore.svg", + "tests/" ] [dependencies] @@ -25,6 +26,7 @@ ndarray = { version = "0.15", optional = true } num-traits = "0.2.12" num = "0.4" rand = { version = "0.8.5", default-features = false, features = ["small_rng"] } +getrandom = { version = "*", features = ["js"] } rand_distr = { version = "0.4", optional = true } serde = { version = "1", features = ["derive"], optional = true } @@ -32,25 +34,21 @@ serde = { version = "1", features = ["derive"], optional = true } default = [] serde = ["dep:serde"] ndarray-bindings = ["dep:ndarray"] -datasets = ["dep:rand_distr", "std", "serde"] -std = ["rand/std_rng", "rand/std"] -# wasm32 only -js = ["getrandom/js"] +datasets = ["dep:rand_distr", "std_rand", "serde"] +std_rand = ["rand/std_rng", "rand/std"] [target.'cfg(target_arch = "wasm32")'.dependencies] getrandom = { version = "0.2", optional = true } -[dev-dependencies] -itertools = "*" -criterion = { version = "0.4", default-features = false } -serde_json = "1.0" -bincode = "1.3.1" - [target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dev-dependencies] wasm-bindgen-test = "0.3" +[dev-dependencies] +itertools = "*" +serde_json = "1.0" +bincode = "1.3.1" + [workspace] -resolver = "2" [profile.test] debug = 1 diff --git a/src/rand_custom.rs b/src/rand_custom.rs index 15f9e73..d06c344 100644 --- a/src/rand_custom.rs +++ b/src/rand_custom.rs @@ -1,5 +1,7 @@ #[cfg(not(feature = "std"))] pub(crate) use rand::rngs::SmallRng as RngImpl; +#[cfg(not(feature = "std"))] +use getrandom; #[cfg(feature = "std")] pub(crate) use rand::rngs::StdRng as RngImpl; use rand::SeedableRng; @@ -13,7 +15,10 @@ pub(crate) fn get_rng_impl(seed: Option) -> RngImpl { use rand::RngCore; RngImpl::seed_from_u64(rand::thread_rng().next_u64()) } else { - panic!("seed number needed for non-std build"); + // non-std build, use getrandom + let mut buf = [0u8; 64]; + getrandom::getrandom(&mut buf).unwrap(); + RngImpl::seed_from_u64(buf[0] as u64) } } }