diff --git a/Cargo.toml b/Cargo.toml index e445a013..872012ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,9 +20,11 @@ name = "nalgebra" path = "src/lib.rs" [features] -default = [ "std" ] -std = [ "matrixmultiply", "rand/std", "alga/std", "rand_distr" ] -stdweb = [ "rand/stdweb" ] +default = [ "std", "rand_with_std" ] +std = [ "matrixmultiply", "alga/std" ] +# Since Cargo doesn't support multiplicitave features, use this to enable rand+std: +rand_with_std = [ "rand/std", "std", "rand_distr" ] +stdweb = [ "rand", "rand/stdweb" ] arbitrary = [ "quickcheck" ] serde-serialize = [ "serde", "serde_derive", "num-complex/serde" ] abomonation-serialize = [ "abomonation" ] @@ -34,7 +36,7 @@ io = [ "pest", "pest_derive" ] [dependencies] typenum = "1.10" generic-array = "0.12" -rand = { version = "0.7", default-features = false } +rand = { version = "0.7", optional = true, default-features = false } rand_distr = { version = "0.2", optional = true } num-traits = { version = "0.2", default-features = false } num-complex = { version = "0.2", default-features = false } diff --git a/ci/test.sh b/ci/test.sh index 233961f3..cf843be3 100755 --- a/ci/test.sh +++ b/ci/test.sh @@ -5,6 +5,7 @@ set -ev if [ -z "$NO_STD" ]; then if [ -z "$LAPACK" ]; then cargo test --verbose --no-default-features --lib; + cargo test --verbose --no-default-features --features "std" --tests; cargo test --verbose; cargo test --verbose "arbitrary"; cargo test --verbose --all-features; @@ -12,4 +13,4 @@ if [ -z "$NO_STD" ]; then else cd nalgebra-lapack; cargo test --verbose; fi -fi \ No newline at end of file +fi diff --git a/src/base/construction.rs b/src/base/construction.rs index 91f454b3..04fda0d6 100644 --- a/src/base/construction.rs +++ b/src/base/construction.rs @@ -4,14 +4,12 @@ use crate::base::storage::Owned; use quickcheck::{Arbitrary, Gen}; use num::{Bounded, One, Zero}; -use rand::distributions::{Distribution, Standard}; -use rand::Rng; -#[cfg(feature = "std")] -use rand_distr::StandardNormal; +#[cfg(feature = "rand")] +use rand::{Rng, distributions::{Distribution, Standard}}; use std::iter; use typenum::{self, Cmp, Greater}; -#[cfg(feature = "std")] +#[cfg(feature = "rand_distr")] use alga::general::RealField; use alga::general::{ClosedAdd, ClosedMul}; @@ -240,7 +238,7 @@ where DefaultAllocator: Allocator /// Creates a matrix filled with random values. #[inline] - #[cfg(feature = "std")] + #[cfg(feature = "rand_with_std")] pub fn new_random_generic(nrows: R, ncols: C) -> Self where Standard: Distribution { let mut rng = rand::thread_rng(); @@ -248,6 +246,7 @@ where DefaultAllocator: Allocator } /// Creates a matrix filled with random values from the given distribution. + #[cfg(feature = "rand")] #[inline] pub fn from_distribution_generic + ?Sized, G: Rng + ?Sized>( nrows: R, @@ -548,6 +547,7 @@ macro_rules! impl_constructors( } /// Creates a matrix or vector filled with random values from the given distribution. + #[cfg(feature = "rand")] #[inline] pub fn from_distribution + ?Sized, G: Rng + ?Sized>( $($args: usize,)* @@ -558,6 +558,7 @@ macro_rules! impl_constructors( } } + #[cfg(feature = "rand")] impl MatrixMN where DefaultAllocator: Allocator, @@ -565,7 +566,7 @@ macro_rules! impl_constructors( /// Creates a matrix filled with random values. #[inline] - #[cfg(feature = "std")] + #[cfg(feature = "rand_with_std")] pub fn new_random($($args: usize),*) -> Self { Self::new_random_generic($($gargs),*) } @@ -761,6 +762,7 @@ where } } +#[cfg(feature = "rand")] impl Distribution> for Standard where DefaultAllocator: Allocator, @@ -796,11 +798,11 @@ where } // TODO(specialization): faster impls possible for D≤4 (see rand_distr::{UnitCircle, UnitSphere}) -#[cfg(feature = "std")] +#[cfg(feature = "rand_distr")] impl Distribution>> for Standard where DefaultAllocator: Allocator, - StandardNormal: Distribution, + rand_distr::StandardNormal: Distribution, { /// Generate a uniformly distributed random unit vector. #[inline] @@ -808,7 +810,7 @@ where Unit::new_normalize(VectorN::from_distribution_generic( D::name(), U1, - &StandardNormal, + &rand_distr::StandardNormal, rng, )) } diff --git a/src/base/helper.rs b/src/base/helper.rs index ef85a477..04a82bb8 100644 --- a/src/base/helper.rs +++ b/src/base/helper.rs @@ -1,3 +1,4 @@ +#![cfg(feature = "rand")] #[cfg(feature = "arbitrary")] use quickcheck::{Arbitrary, Gen}; use rand::distributions::{Distribution, Standard}; diff --git a/src/geometry/isometry_construction.rs b/src/geometry/isometry_construction.rs index 979c3955..25562d0a 100644 --- a/src/geometry/isometry_construction.rs +++ b/src/geometry/isometry_construction.rs @@ -4,8 +4,8 @@ use crate::base::storage::Owned; use quickcheck::{Arbitrary, Gen}; use num::One; -use rand::distributions::{Distribution, Standard}; -use rand::Rng; +#[cfg(feature = "rand")] +use rand::{Rng, distributions::{Distribution, Standard}}; use alga::general::RealField; use alga::linear::Rotation as AlgaRotation; @@ -75,6 +75,7 @@ where DefaultAllocator: Allocator } } +#[cfg(feature = "rand")] impl Distribution> for Standard where R: AlgaRotation>, diff --git a/src/geometry/orthographic.rs b/src/geometry/orthographic.rs index 22bd05ef..0976ad97 100644 --- a/src/geometry/orthographic.rs +++ b/src/geometry/orthographic.rs @@ -1,7 +1,7 @@ #[cfg(feature = "arbitrary")] use quickcheck::{Arbitrary, Gen}; -use rand::distributions::{Distribution, Standard}; -use rand::Rng; +#[cfg(feature = "rand")] +use rand::{Rng, distributions::{Distribution, Standard}}; #[cfg(feature = "serde-serialize")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::fmt; @@ -10,6 +10,7 @@ use std::mem; use alga::general::RealField; use crate::base::dimension::U3; +#[cfg(feature = "rand")] use crate::base::helper; use crate::base::storage::Storage; use crate::base::{Matrix4, Vector, Vector3}; @@ -678,6 +679,7 @@ impl Orthographic3 { } } +#[cfg(feature = "rand")] impl Distribution> for Standard where Standard: Distribution { diff --git a/src/geometry/perspective.rs b/src/geometry/perspective.rs index ae47ed00..cfc173ca 100644 --- a/src/geometry/perspective.rs +++ b/src/geometry/perspective.rs @@ -1,7 +1,7 @@ #[cfg(feature = "arbitrary")] use quickcheck::{Arbitrary, Gen}; -use rand::distributions::{Distribution, Standard}; -use rand::Rng; +#[cfg(feature = "rand")] +use rand::{Rng, distributions::{Distribution, Standard}}; #[cfg(feature = "serde-serialize")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -11,6 +11,7 @@ use std::mem; use alga::general::RealField; use crate::base::dimension::U3; +#[cfg(feature = "rand")] use crate::base::helper; use crate::base::storage::Storage; use crate::base::{Matrix4, Scalar, Vector, Vector3}; @@ -261,6 +262,7 @@ impl Perspective3 { } } +#[cfg(feature = "rand")] impl Distribution> for Standard where Standard: Distribution { diff --git a/src/geometry/point_construction.rs b/src/geometry/point_construction.rs index 6ad321dc..33b97078 100644 --- a/src/geometry/point_construction.rs +++ b/src/geometry/point_construction.rs @@ -2,8 +2,8 @@ use quickcheck::{Arbitrary, Gen}; use num::{Bounded, One, Zero}; -use rand::distributions::{Distribution, Standard}; -use rand::Rng; +#[cfg(feature = "rand")] +use rand::{Rng, distributions::{Distribution, Standard}}; use alga::general::ClosedDiv; use crate::base::allocator::Allocator; @@ -126,6 +126,7 @@ where DefaultAllocator: Allocator } } +#[cfg(feature = "rand")] impl Distribution> for Standard where DefaultAllocator: Allocator, diff --git a/src/geometry/quaternion_construction.rs b/src/geometry/quaternion_construction.rs index d9ed0c2b..387cc5d0 100644 --- a/src/geometry/quaternion_construction.rs +++ b/src/geometry/quaternion_construction.rs @@ -6,8 +6,8 @@ use crate::base::storage::Owned; use quickcheck::{Arbitrary, Gen}; use num::{One, Zero}; -use rand::distributions::{Distribution, OpenClosed01, Standard}; -use rand::Rng; +#[cfg(feature = "rand")] +use rand::{Rng, distributions::{Distribution, OpenClosed01, Standard}}; use alga::general::RealField; @@ -124,6 +124,7 @@ impl Zero for Quaternion { } } +#[cfg(feature = "rand")] impl Distribution> for Standard where Standard: Distribution { @@ -685,6 +686,7 @@ impl One for UnitQuaternion { } } +#[cfg(feature = "rand")] impl Distribution> for Standard where OpenClosed01: Distribution { @@ -722,10 +724,10 @@ where } } -#[cfg(test)] +#[cfg(all(test, feature="rand"))] mod tests { use super::*; - use rand::{SeedableRng, rngs::SmallRng}; + use rand::{Rng, SeedableRng, rngs::SmallRng}; #[test] fn random_unit_quats_are_unit() { diff --git a/src/geometry/rotation_specialization.rs b/src/geometry/rotation_specialization.rs index 493a1c12..e6a4530f 100644 --- a/src/geometry/rotation_specialization.rs +++ b/src/geometry/rotation_specialization.rs @@ -5,7 +5,9 @@ use quickcheck::{Arbitrary, Gen}; use alga::general::RealField; use num::Zero; +#[cfg(feature = "rand")] use rand::distributions::{Distribution, OpenClosed01, Standard, uniform::SampleUniform}; +#[cfg(feature = "rand")] use rand::Rng; use std::ops::Neg; @@ -230,6 +232,7 @@ impl Rotation2 { } } +#[cfg(feature = "rand")] impl Distribution> for Standard where N: SampleUniform { @@ -817,6 +820,7 @@ impl Rotation3 { } } +#[cfg(feature = "rand")] impl Distribution> for Standard where OpenClosed01: Distribution, N: SampleUniform { diff --git a/src/geometry/similarity_construction.rs b/src/geometry/similarity_construction.rs index 53ec0d76..959bd67e 100644 --- a/src/geometry/similarity_construction.rs +++ b/src/geometry/similarity_construction.rs @@ -4,8 +4,8 @@ use crate::base::storage::Owned; use quickcheck::{Arbitrary, Gen}; use num::One; -use rand::distributions::{Distribution, Standard}; -use rand::Rng; +#[cfg(feature = "rand")] +use rand::{Rng, distributions::{Distribution, Standard}}; use alga::general::RealField; use alga::linear::Rotation as AlgaRotation; @@ -57,6 +57,7 @@ where } } +#[cfg(feature = "rand")] impl Distribution> for Standard where R: AlgaRotation>, diff --git a/src/geometry/translation_construction.rs b/src/geometry/translation_construction.rs index e366bba3..e2285bef 100644 --- a/src/geometry/translation_construction.rs +++ b/src/geometry/translation_construction.rs @@ -4,8 +4,8 @@ use crate::base::storage::Owned; use quickcheck::{Arbitrary, Gen}; use num::{One, Zero}; -use rand::distributions::{Distribution, Standard}; -use rand::Rng; +#[cfg(feature = "rand")] +use rand::{Rng, distributions::{Distribution, Standard}}; use alga::general::ClosedAdd; @@ -47,6 +47,7 @@ where DefaultAllocator: Allocator } } +#[cfg(feature = "rand")] impl Distribution> for Standard where DefaultAllocator: Allocator, diff --git a/src/geometry/unit_complex_construction.rs b/src/geometry/unit_complex_construction.rs index da9a68cf..2934816c 100644 --- a/src/geometry/unit_complex_construction.rs +++ b/src/geometry/unit_complex_construction.rs @@ -3,7 +3,9 @@ use quickcheck::{Arbitrary, Gen}; use num::One; use num_complex::Complex; +#[cfg(feature = "rand_distr")] use rand::distributions::{Distribution, Standard}; +#[cfg(feature = "rand_distr")] use rand::Rng; use alga::general::RealField; @@ -275,7 +277,7 @@ impl One for UnitComplex { } } -#[cfg(feature = "std")] +#[cfg(feature = "rand_distr")] impl Distribution> for Standard where rand_distr::UnitCircle: Distribution<[N; 2]> { @@ -287,17 +289,6 @@ where rand_distr::UnitCircle: Distribution<[N; 2]> } } -#[cfg(not(feature = "std"))] -impl Distribution> for Standard -where N: rand::distributions::uniform::SampleUniform -{ - /// Generate a uniformly distributed random `UnitComplex`. - #[inline] - fn sample<'a, R: Rng + ?Sized>(&self, rng: &mut R) -> UnitComplex { - UnitComplex::from_angle(rng.gen_range(N::zero(), N::two_pi())) - } -} - #[cfg(feature = "arbitrary")] impl Arbitrary for UnitComplex { #[inline] diff --git a/src/lib.rs b/src/lib.rs index 76d479a7..a62577c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,7 +112,6 @@ extern crate matrixmultiply; extern crate num_complex; extern crate num_traits as num; extern crate num_rational; -extern crate rand; extern crate typenum; extern crate alga; diff --git a/src/linalg/symmetric_eigen.rs b/src/linalg/symmetric_eigen.rs index 2a7bb992..729b2b7c 100644 --- a/src/linalg/symmetric_eigen.rs +++ b/src/linalg/symmetric_eigen.rs @@ -354,7 +354,7 @@ mod test { } } - #[cfg(feature = "std")] + #[cfg(feature = "rand_with_std")] #[test] fn wilkinson_shift_random() { for _ in 0..1000 { diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index e4fb4d0c..712802a3 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -466,6 +466,7 @@ fn zip_map() { assert_eq!(computed, expected); } +#[cfg(feature = "rand_with_std")] #[test] #[should_panic] fn trace_panic() {