Skip to content

Commit d18485c

Browse files
FreezyLemonYeungOnion
authored andcommitted
build: make rand an optional dependency
1 parent bee36d9 commit d18485c

35 files changed

+117
-89
lines changed

Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,26 @@ path = "src/lib.rs"
2222
[[bench]]
2323
name = "order_statistics"
2424
harness = false
25+
required-features = ["rand"]
2526

2627
[features]
27-
default = ["nalgebra"]
28+
default = ["nalgebra", "rand"]
2829
nalgebra = ["dep:nalgebra"]
30+
rand = ["dep:rand", "nalgebra?/rand"]
2931

3032
[dependencies]
31-
rand = "0.8"
3233
approx = "0.5.0"
3334
num-traits = "0.2.14"
3435

36+
[dependencies.rand]
37+
version = "0.8"
38+
optional = true
39+
3540
[dependencies.nalgebra]
3641
version = "0.32"
3742
optional = true
3843
default-features = false
39-
features = ["rand", "std"]
44+
features = ["std"]
4045

4146
[dev-dependencies]
4247
criterion = "0.5"

benches/order_statistics.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
extern crate rand;
21
extern crate statrs;
32
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
43
use rand::prelude::*;

src/distribution/bernoulli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::distribution::{Binomial, BinomialError, Discrete, DiscreteCDF};
22
use crate::statistics::*;
3-
use rand::Rng;
43

54
/// Implements the
65
/// [Bernoulli](https://en.wikipedia.org/wiki/Bernoulli_distribution)
@@ -85,8 +84,9 @@ impl std::fmt::Display for Bernoulli {
8584
}
8685
}
8786

87+
#[cfg(feature = "rand")]
8888
impl ::rand::distributions::Distribution<f64> for Bernoulli {
89-
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
89+
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
9090
rng.gen_bool(self.p()) as u8 as f64
9191
}
9292
}

src/distribution/beta.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::distribution::{Continuous, ContinuousCDF};
22
use crate::function::{beta, gamma};
33
use crate::statistics::*;
4-
use rand::Rng;
54

65
/// Implements the [Beta](https://en.wikipedia.org/wiki/Beta_distribution)
76
/// distribution
@@ -121,8 +120,9 @@ impl std::fmt::Display for Beta {
121120
}
122121
}
123122

123+
#[cfg(feature = "rand")]
124124
impl ::rand::distributions::Distribution<f64> for Beta {
125-
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
125+
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
126126
// Generated by sampling two gamma distributions and normalizing.
127127
let x = super::gamma::sample_unchecked(rng, self.shape_a, 1.0);
128128
let y = super::gamma::sample_unchecked(rng, self.shape_b, 1.0);

src/distribution/binomial.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::distribution::{Discrete, DiscreteCDF};
22
use crate::function::{beta, factorial};
33
use crate::statistics::*;
4-
use rand::Rng;
54
use std::f64;
65

76
/// Implements the
@@ -110,8 +109,9 @@ impl std::fmt::Display for Binomial {
110109
}
111110
}
112111

112+
#[cfg(feature = "rand")]
113113
impl ::rand::distributions::Distribution<f64> for Binomial {
114-
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
114+
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
115115
(0..self.n).fold(0.0, |acc, _| {
116116
let n: f64 = rng.gen();
117117
if n < self.p {

src/distribution/categorical.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::distribution::{Discrete, DiscreteCDF};
22
use crate::statistics::*;
3-
use rand::Rng;
43
use std::f64;
54

65
/// Implements the
@@ -124,8 +123,9 @@ impl std::fmt::Display for Categorical {
124123
}
125124
}
126125

126+
#[cfg(feature = "rand")]
127127
impl ::rand::distributions::Distribution<f64> for Categorical {
128-
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
128+
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
129129
sample_unchecked(rng, &self.cdf)
130130
}
131131
}
@@ -322,7 +322,8 @@ impl Discrete<u64, f64> for Categorical {
322322

323323
/// Draws a sample from the categorical distribution described by `cdf`
324324
/// without doing any bounds checking
325-
pub fn sample_unchecked<R: Rng + ?Sized>(rng: &mut R, cdf: &[f64]) -> f64 {
325+
#[cfg(feature = "rand")]
326+
pub fn sample_unchecked<R: ::rand::Rng + ?Sized>(rng: &mut R, cdf: &[f64]) -> f64 {
326327
let draw = rng.gen::<f64>() * cdf.last().unwrap();
327328
cdf.iter()
328329
.enumerate()

src/distribution/cauchy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::distribution::{Continuous, ContinuousCDF};
22
use crate::statistics::*;
3-
use rand::Rng;
43
use std::f64;
54

65
/// Implements the [Cauchy](https://en.wikipedia.org/wiki/Cauchy_distribution)
@@ -111,8 +110,9 @@ impl std::fmt::Display for Cauchy {
111110
}
112111
}
113112

113+
#[cfg(feature = "rand")]
114114
impl ::rand::distributions::Distribution<f64> for Cauchy {
115-
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> f64 {
115+
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
116116
self.location + self.scale * (f64::consts::PI * (r.gen::<f64>() - 0.5)).tan()
117117
}
118118
}

src/distribution/chi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::distribution::{Continuous, ContinuousCDF};
22
use crate::function::gamma;
33
use crate::statistics::*;
4-
use rand::Rng;
54
use std::f64;
65

76
/// Implements the [Chi](https://en.wikipedia.org/wiki/Chi_distribution)
@@ -94,8 +93,9 @@ impl std::fmt::Display for Chi {
9493
}
9594
}
9695

96+
#[cfg(feature = "rand")]
9797
impl ::rand::distributions::Distribution<f64> for Chi {
98-
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
98+
fn sample<R: ::rand::Rng + ?Sized>(&self, rng: &mut R) -> f64 {
9999
(0..self.freedom as i64)
100100
.fold(0.0, |acc, _| {
101101
acc + super::normal::sample_unchecked(rng, 0.0, 1.0).powf(2.0)

src/distribution/chi_squared.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::distribution::{Continuous, ContinuousCDF, Gamma, GammaError};
22
use crate::statistics::*;
3-
use rand::Rng;
43
use std::f64;
54

65
/// Implements the
@@ -101,8 +100,9 @@ impl std::fmt::Display for ChiSquared {
101100
}
102101
}
103102

103+
#[cfg(feature = "rand")]
104104
impl ::rand::distributions::Distribution<f64> for ChiSquared {
105-
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> f64 {
105+
fn sample<R: ::rand::Rng + ?Sized>(&self, r: &mut R) -> f64 {
106106
::rand::distributions::Distribution::sample(&self.g, r)
107107
}
108108
}

src/distribution/dirac.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::distribution::ContinuousCDF;
22
use crate::statistics::*;
3-
use rand::Rng;
43

54
/// Implements the [Dirac Delta](https://en.wikipedia.org/wiki/Dirac_delta_function#As_a_distribution)
65
/// distribution
@@ -69,8 +68,9 @@ impl std::fmt::Display for Dirac {
6968
}
7069
}
7170

71+
#[cfg(feature = "rand")]
7272
impl ::rand::distributions::Distribution<f64> for Dirac {
73-
fn sample<R: Rng + ?Sized>(&self, _: &mut R) -> f64 {
73+
fn sample<R: ::rand::Rng + ?Sized>(&self, _: &mut R) -> f64 {
7474
self.0
7575
}
7676
}

0 commit comments

Comments
 (0)