Skip to content

Commit 8e6e5f9

Browse files
committed
Use getrandom as default (for no-std feature)
1 parent bf7b714 commit 8e6e5f9

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

.github/CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ Take a look to the conventions established by existing code:
2525
* Every module should provide a Rust doctest, a brief test embedded with the documentation that explains how to use the procedure implemented.
2626
* 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.
2727
* 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.
28+
29+
#### digging deeper
2830
* a nice overview of the codebase is given by [static analyzer](https://mozilla.github.io/rust-code-analysis/metrics.html):
2931
```
3032
$ cargo install rust-code-analysis-cli
33+
// print metrics for every module
3134
$ rust-code-analysis-cli -m -O json -o . -p src/ --pr
35+
// print full AST for a module
36+
$ rust-code-analysis-cli -p src/algorithm/neighbour/fastpair.rs --ls 22 --le 213 -d > ast.txt
3237
```
38+
* 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.
3339

3440
## Issue Report Process
3541

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ out.svg
2727

2828
FlameGraph/
2929
out.stacks
30-
*.json
30+
*.json
31+
*.txt

Cargo.toml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ exclude = [
1616
".gitignore",
1717
"smartcore.iml",
1818
"smartcore.svg",
19+
"tests/"
1920
]
2021

2122
[dependencies]
@@ -25,32 +26,29 @@ ndarray = { version = "0.15", optional = true }
2526
num-traits = "0.2.12"
2627
num = "0.4"
2728
rand = { version = "0.8.5", default-features = false, features = ["small_rng"] }
29+
getrandom = { version = "*", features = ["js"] }
2830
rand_distr = { version = "0.4", optional = true }
2931
serde = { version = "1", features = ["derive"], optional = true }
3032

3133
[features]
3234
default = []
3335
serde = ["dep:serde"]
3436
ndarray-bindings = ["dep:ndarray"]
35-
datasets = ["dep:rand_distr", "std", "serde"]
36-
std = ["rand/std_rng", "rand/std"]
37-
# wasm32 only
38-
js = ["getrandom/js"]
37+
datasets = ["dep:rand_distr", "std_rand", "serde"]
38+
std_rand = ["rand/std_rng", "rand/std"]
3939

4040
[target.'cfg(target_arch = "wasm32")'.dependencies]
4141
getrandom = { version = "0.2", optional = true }
4242

43+
[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dev-dependencies]
44+
wasm-bindgen-test = "0.3"
45+
4346
[dev-dependencies]
4447
itertools = "*"
45-
criterion = { version = "0.4", default-features = false }
4648
serde_json = "1.0"
4749
bincode = "1.3.1"
4850

49-
[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dev-dependencies]
50-
wasm-bindgen-test = "0.3"
51-
5251
[workspace]
53-
resolver = "2"
5452

5553
[profile.test]
5654
debug = 1

src/rand_custom.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[cfg(not(feature = "std"))]
22
pub(crate) use rand::rngs::SmallRng as RngImpl;
3+
#[cfg(not(feature = "std"))]
4+
use getrandom;
35
#[cfg(feature = "std")]
46
pub(crate) use rand::rngs::StdRng as RngImpl;
57
use rand::SeedableRng;
@@ -13,7 +15,10 @@ pub(crate) fn get_rng_impl(seed: Option<u64>) -> RngImpl {
1315
use rand::RngCore;
1416
RngImpl::seed_from_u64(rand::thread_rng().next_u64())
1517
} else {
16-
panic!("seed number needed for non-std build");
18+
// non-std build, use getrandom
19+
let mut buf = [0u8; 64];
20+
getrandom::getrandom(&mut buf).unwrap();
21+
RngImpl::seed_from_u64(buf[0] as u64)
1722
}
1823
}
1924
}

0 commit comments

Comments
 (0)