Use getrandom as default (for no-std feature)

This commit is contained in:
Lorenzo (Mec-iS)
2022-11-08 11:47:31 +00:00
committed by morenol
parent 6d529b34d2
commit 669f87f812
4 changed files with 23 additions and 13 deletions
+6
View File
@@ -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
+2 -1
View File
@@ -27,4 +27,5 @@ out.svg
FlameGraph/
out.stacks
*.json
*.json
*.txt
+9 -11
View File
@@ -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
+6 -1
View File
@@ -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<u64>) -> 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)
}
}
}