Make the rand dependency optional.

This commit is contained in:
Crozet Sébastien 2021-03-02 12:25:12 +01:00
parent 7fdac06365
commit c32172b6b9
15 changed files with 106 additions and 60 deletions

3
.github/Xargo.toml vendored
View File

@ -1,2 +1,5 @@
[target.x86_64-unknown-linux-gnu.dependencies] [target.x86_64-unknown-linux-gnu.dependencies]
alloc = {}
[target.thumbv7em-none-eabihf.dependencies]
alloc = {} alloc = {}

View File

@ -90,7 +90,11 @@ jobs:
components: rustfmt components: rustfmt
- name: install xargo - name: install xargo
run: cp .github/Xargo.toml .; rustup component add rust-src; cargo install -f xargo; run: cp .github/Xargo.toml .; rustup component add rust-src; cargo install -f xargo;
- name: build - name: build x86_64-unknown-linux-gnu
run: xargo build --verbose --no-default-features --target=x86_64-unknown-linux-gnu; run: xargo build --verbose --no-default-features --target=x86_64-unknown-linux-gnu;
- name: build --feature alloc - name: build x86_64-unknown-linux-gnu --features rand-no-std
run: xargo build --verbose --no-default-features --target=x86_64-unknown-linux-gnu;
- name: build x86_64-unknown-linux-gnu --features alloc
run: xargo build --verbose --no-default-features --features alloc --target=x86_64-unknown-linux-gnu; run: xargo build --verbose --no-default-features --features alloc --target=x86_64-unknown-linux-gnu;
- name: build thumbv7em-none-eabihf
run: xargo build --verbose --no-default-features --target=thumbv7em-none-eabihf;

View File

@ -23,12 +23,14 @@ path = "src/lib.rs"
[features] [features]
default = [ "std" ] default = [ "std" ]
std = [ "matrixmultiply", "rand/std", "rand/std_rng", "rand_distr", "simba/std" ] std = [ "matrixmultiply", "simba/std" ]
rand-no-std = [ "rand-package" ]
rand = [ "rand-no-std", "rand-package/std", "rand-package/std_rng", "rand_distr" ]
arbitrary = [ "quickcheck" ] arbitrary = [ "quickcheck" ]
serde-serialize = [ "serde", "num-complex/serde" ] serde-serialize = [ "serde", "num-complex/serde" ]
abomonation-serialize = [ "abomonation" ] abomonation-serialize = [ "abomonation" ]
sparse = [ ] sparse = [ ]
debug = [ "approx/num-complex", "rand/std" ] debug = [ "approx/num-complex", "rand" ]
alloc = [ ] alloc = [ ]
io = [ "pest", "pest_derive" ] io = [ "pest", "pest_derive" ]
compare = [ "matrixcompare-core" ] compare = [ "matrixcompare-core" ]
@ -43,8 +45,7 @@ slow-tests = []
[dependencies] [dependencies]
typenum = "1.12" typenum = "1.12"
generic-array = "0.14" generic-array = "0.14"
rand = { version = "0.8", default-features = false } rand-package = { package = "rand", version = "0.8", optional = true, default-features = false }
getrandom = { version = "0.2", default-features = false, features = [ "js" ] } # For wasm
num-traits = { version = "0.2", default-features = false } num-traits = { version = "0.2", default-features = false }
num-complex = { version = "0.3", default-features = false } num-complex = { version = "0.3", default-features = false }
num-rational = { version = "0.3", default-features = false } num-rational = { version = "0.3", default-features = false }

View File

@ -7,18 +7,16 @@ use crate::base::storage::Owned;
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use num::{Bounded, One, Zero}; use num::{Bounded, One, Zero};
#[cfg(feature = "std")] #[cfg(feature = "rand-no-std")]
use rand; use rand::{
use rand::distributions::{Distribution, Standard}; distributions::{Distribution, Standard},
use rand::Rng; Rng,
#[cfg(feature = "std")] };
use rand_distr::StandardNormal;
use std::iter; use std::iter;
use std::mem; use std::mem;
use typenum::{self, Cmp, Greater}; use typenum::{self, Cmp, Greater};
#[cfg(feature = "std")]
use simba::scalar::RealField;
use simba::scalar::{ClosedAdd, ClosedMul}; use simba::scalar::{ClosedAdd, ClosedMul};
use crate::base::allocator::Allocator; use crate::base::allocator::Allocator;
@ -281,7 +279,7 @@ where
/// Creates a matrix filled with random values. /// Creates a matrix filled with random values.
#[inline] #[inline]
#[cfg(feature = "std")] #[cfg(feature = "rand")]
pub fn new_random_generic(nrows: R, ncols: C) -> Self pub fn new_random_generic(nrows: R, ncols: C) -> Self
where where
Standard: Distribution<N>, Standard: Distribution<N>,
@ -291,6 +289,7 @@ where
/// Creates a matrix filled with random values from the given distribution. /// Creates a matrix filled with random values from the given distribution.
#[inline] #[inline]
#[cfg(feature = "rand-no-std")]
pub fn from_distribution_generic<Distr: Distribution<N> + ?Sized, G: Rng + ?Sized>( pub fn from_distribution_generic<Distr: Distribution<N> + ?Sized, G: Rng + ?Sized>(
nrows: R, nrows: R,
ncols: C, ncols: C,
@ -589,6 +588,7 @@ macro_rules! impl_constructors(
/// Creates a matrix or vector filled with random values from the given distribution. /// Creates a matrix or vector filled with random values from the given distribution.
#[inline] #[inline]
#[cfg(feature = "rand-no-std")]
pub fn from_distribution<Distr: Distribution<N> + ?Sized, G: Rng + ?Sized>( pub fn from_distribution<Distr: Distribution<N> + ?Sized, G: Rng + ?Sized>(
$($args: usize,)* $($args: usize,)*
distribution: &Distr, distribution: &Distr,
@ -599,7 +599,7 @@ macro_rules! impl_constructors(
/// Creates a matrix filled with random values. /// Creates a matrix filled with random values.
#[inline] #[inline]
#[cfg(feature = "std")] #[cfg(feature = "rand")]
pub fn new_random($($args: usize),*) -> Self pub fn new_random($($args: usize),*) -> Self
where Standard: Distribution<N> { where Standard: Distribution<N> {
Self::new_random_generic($($gargs),*) Self::new_random_generic($($gargs),*)
@ -817,6 +817,7 @@ where
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: Scalar, R: Dim, C: Dim> Distribution<MatrixMN<N, R, C>> for Standard impl<N: Scalar, R: Dim, C: Dim> Distribution<MatrixMN<N, R, C>> for Standard
where where
DefaultAllocator: Allocator<N, R, C>, DefaultAllocator: Allocator<N, R, C>,
@ -851,11 +852,11 @@ where
} }
} }
#[cfg(feature = "std")] #[cfg(feature = "rand")]
impl<N: RealField, D: DimName> Distribution<Unit<VectorN<N, D>>> for Standard impl<N: crate::RealField, D: DimName> Distribution<Unit<VectorN<N, D>>> for Standard
where where
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
StandardNormal: Distribution<N>, rand_distr::StandardNormal: Distribution<N>,
{ {
/// Generate a uniformly distributed random unit vector. /// Generate a uniformly distributed random unit vector.
#[inline] #[inline]
@ -863,7 +864,7 @@ where
Unit::new_normalize(VectorN::from_distribution_generic( Unit::new_normalize(VectorN::from_distribution_generic(
D::name(), D::name(),
U1, U1,
&StandardNormal, &rand_distr::StandardNormal,
rng, rng,
)) ))
} }

View File

@ -1,7 +1,11 @@
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use rand::distributions::{Distribution, Standard};
use rand::Rng; #[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, Standard},
Rng,
};
/// Simple helper function for rejection sampling /// Simple helper function for rejection sampling
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
@ -17,6 +21,7 @@ pub fn reject<F: FnMut(&T) -> bool, T: Arbitrary>(g: &mut Gen, f: F) -> T {
#[doc(hidden)] #[doc(hidden)]
#[inline] #[inline]
#[cfg(feature = "rand-no-std")]
pub fn reject_rand<G: Rng + ?Sized, F: FnMut(&T) -> bool, T>(g: &mut G, f: F) -> T pub fn reject_rand<G: Rng + ?Sized, F: FnMut(&T) -> bool, T>(g: &mut G, f: F) -> T
where where
Standard: Distribution<T>, Standard: Distribution<T>,

View File

@ -4,10 +4,12 @@ use crate::base::storage::Owned;
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use num::One; use num::One;
use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand-no-std")]
use rand::Rng; use rand::{
distributions::{Distribution, Standard},
Rng,
};
use simba::scalar::RealField;
use simba::simd::SimdRealField; use simba::simd::SimdRealField;
use crate::base::allocator::Allocator; use crate::base::allocator::Allocator;
@ -80,7 +82,8 @@ where
} }
} }
impl<N: RealField, D: DimName, R> Distribution<Isometry<N, D, R>> for Standard #[cfg(feature = "rand-no-std")]
impl<N: crate::RealField, D: DimName, R> Distribution<Isometry<N, D, R>> for Standard
where where
R: AbstractRotation<N, D>, R: AbstractRotation<N, D>,
Standard: Distribution<N> + Distribution<R>, Standard: Distribution<N> + Distribution<R>,

View File

@ -1,7 +1,10 @@
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand-no-std")]
use rand::Rng; use rand::{
distributions::{Distribution, Standard},
Rng,
};
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt; use std::fmt;
@ -10,7 +13,6 @@ use std::mem;
use simba::scalar::RealField; use simba::scalar::RealField;
use crate::base::dimension::U3; use crate::base::dimension::U3;
use crate::base::helper;
use crate::base::storage::Storage; use crate::base::storage::Storage;
use crate::base::{Matrix4, Vector, Vector3}; use crate::base::{Matrix4, Vector, Vector3};
@ -684,11 +686,13 @@ impl<N: RealField> Orthographic3<N> {
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: RealField> Distribution<Orthographic3<N>> for Standard impl<N: RealField> Distribution<Orthographic3<N>> for Standard
where where
Standard: Distribution<N>, Standard: Distribution<N>,
{ {
fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Orthographic3<N> { fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> Orthographic3<N> {
use crate::base::helper;
let left = r.gen(); let left = r.gen();
let right = helper::reject_rand(r, |x: &N| *x > left); let right = helper::reject_rand(r, |x: &N| *x > left);
let bottom = r.gen(); let bottom = r.gen();
@ -706,6 +710,7 @@ where
Matrix4<N>: Send, Matrix4<N>: Send,
{ {
fn arbitrary(g: &mut Gen) -> Self { fn arbitrary(g: &mut Gen) -> Self {
use crate::base::helper;
let left = Arbitrary::arbitrary(g); let left = Arbitrary::arbitrary(g);
let right = helper::reject(g, |x: &N| *x > left); let right = helper::reject(g, |x: &N| *x > left);
let bottom = Arbitrary::arbitrary(g); let bottom = Arbitrary::arbitrary(g);

View File

@ -1,7 +1,10 @@
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand-no-std")]
use rand::Rng; use rand::{
distributions::{Distribution, Standard},
Rng,
};
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
@ -11,7 +14,6 @@ use std::mem;
use simba::scalar::RealField; use simba::scalar::RealField;
use crate::base::dimension::U3; use crate::base::dimension::U3;
use crate::base::helper;
use crate::base::storage::Storage; use crate::base::storage::Storage;
use crate::base::{Matrix4, Scalar, Vector, Vector3}; use crate::base::{Matrix4, Scalar, Vector, Vector3};
@ -268,11 +270,13 @@ impl<N: RealField> Perspective3<N> {
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: RealField> Distribution<Perspective3<N>> for Standard impl<N: RealField> Distribution<Perspective3<N>> for Standard
where where
Standard: Distribution<N>, Standard: Distribution<N>,
{ {
fn sample<'a, R: Rng + ?Sized>(&self, r: &'a mut R) -> Perspective3<N> { fn sample<'a, R: Rng + ?Sized>(&self, r: &'a mut R) -> Perspective3<N> {
use crate::base::helper;
let znear = r.gen(); let znear = r.gen();
let zfar = helper::reject_rand(r, |&x: &N| !(x - znear).is_zero()); let zfar = helper::reject_rand(r, |&x: &N| !(x - znear).is_zero());
let aspect = helper::reject_rand(r, |&x: &N| !x.is_zero()); let aspect = helper::reject_rand(r, |&x: &N| !x.is_zero());
@ -284,6 +288,7 @@ where
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
impl<N: RealField + Arbitrary> Arbitrary for Perspective3<N> { impl<N: RealField + Arbitrary> Arbitrary for Perspective3<N> {
fn arbitrary(g: &mut Gen) -> Self { fn arbitrary(g: &mut Gen) -> Self {
use crate::base::helper;
let znear = Arbitrary::arbitrary(g); let znear = Arbitrary::arbitrary(g);
let zfar = helper::reject(g, |&x: &N| !(x - znear).is_zero()); let zfar = helper::reject(g, |&x: &N| !(x - znear).is_zero());
let aspect = helper::reject(g, |&x: &N| !x.is_zero()); let aspect = helper::reject(g, |&x: &N| !x.is_zero());

View File

@ -2,8 +2,11 @@
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use num::{Bounded, One, Zero}; use num::{Bounded, One, Zero};
use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand-no-std")]
use rand::Rng; use rand::{
distributions::{Distribution, Standard},
Rng,
};
use crate::base::allocator::Allocator; use crate::base::allocator::Allocator;
use crate::base::dimension::{DimName, DimNameAdd, DimNameSum, U1}; use crate::base::dimension::{DimName, DimNameAdd, DimNameSum, U1};
@ -138,6 +141,7 @@ where
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: Scalar, D: DimName> Distribution<Point<N, D>> for Standard impl<N: Scalar, D: DimName> Distribution<Point<N, D>> for Standard
where where
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,

View File

@ -5,9 +5,13 @@ use crate::base::storage::Owned;
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, OpenClosed01, Standard},
Rng,
};
use num::{One, Zero}; use num::{One, Zero};
use rand::distributions::{Distribution, OpenClosed01, Standard};
use rand::Rng;
use simba::scalar::RealField; use simba::scalar::RealField;
use simba::simd::SimdBool; use simba::simd::SimdBool;
@ -144,6 +148,7 @@ where
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: SimdRealField> Distribution<Quaternion<N>> for Standard impl<N: SimdRealField> Distribution<Quaternion<N>> for Standard
where where
Standard: Distribution<N>, Standard: Distribution<N>,
@ -812,6 +817,7 @@ where
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: SimdRealField> Distribution<UnitQuaternion<N>> for Standard impl<N: SimdRealField> Distribution<UnitQuaternion<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,

View File

@ -4,8 +4,13 @@ use crate::base::storage::Owned;
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use num::Zero; use num::Zero;
use rand::distributions::{Distribution, OpenClosed01, Standard};
use rand::Rng; #[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, OpenClosed01, Standard},
Rng,
};
use simba::scalar::RealField; use simba::scalar::RealField;
use simba::simd::{SimdBool, SimdRealField}; use simba::simd::{SimdBool, SimdRealField};
use std::ops::Neg; use std::ops::Neg;
@ -256,6 +261,7 @@ impl<N: SimdRealField> Rotation2<N> {
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: SimdRealField> Distribution<Rotation2<N>> for Standard impl<N: SimdRealField> Distribution<Rotation2<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,
@ -912,6 +918,7 @@ impl<N: SimdRealField> Rotation3<N> {
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: SimdRealField> Distribution<Rotation3<N>> for Standard impl<N: SimdRealField> Distribution<Rotation3<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,

View File

@ -4,10 +4,12 @@ use crate::base::storage::Owned;
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use num::One; use num::One;
use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand-no-std")]
use rand::Rng; use rand::{
distributions::{Distribution, Standard},
Rng,
};
use simba::scalar::RealField;
use simba::simd::SimdRealField; use simba::simd::SimdRealField;
use crate::base::allocator::Allocator; use crate::base::allocator::Allocator;
@ -59,7 +61,8 @@ where
} }
} }
impl<N: RealField, D: DimName, R> Distribution<Similarity<N, D, R>> for Standard #[cfg(feature = "rand-no-std")]
impl<N: crate::RealField, D: DimName, R> Distribution<Similarity<N, D, R>> for Standard
where where
R: AbstractRotation<N, D>, R: AbstractRotation<N, D>,
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
@ -107,8 +110,8 @@ where
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
impl<N, D: DimName, R> Arbitrary for Similarity<N, D, R> impl<N, D: DimName, R> Arbitrary for Similarity<N, D, R>
where where
N: RealField + Arbitrary + Send, N: crate::RealField + Arbitrary + Send,
N::Element: RealField, N::Element: crate::RealField,
R: AbstractRotation<N, D> + Arbitrary + Send, R: AbstractRotation<N, D> + Arbitrary + Send,
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,
Owned<N, D>: Send, Owned<N, D>: Send,

View File

@ -4,8 +4,11 @@ use crate::base::storage::Owned;
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
use num::{One, Zero}; use num::{One, Zero};
use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand-no-std")]
use rand::Rng; use rand::{
distributions::{Distribution, Standard},
Rng,
};
use simba::scalar::ClosedAdd; use simba::scalar::ClosedAdd;
@ -49,6 +52,7 @@ where
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: Scalar, D: DimName> Distribution<Translation<N, D>> for Standard impl<N: Scalar, D: DimName> Distribution<Translation<N, D>> for Standard
where where
DefaultAllocator: Allocator<N, D>, DefaultAllocator: Allocator<N, D>,

View File

@ -1,10 +1,14 @@
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen}; use quickcheck::{Arbitrary, Gen};
#[cfg(feature = "rand-no-std")]
use rand::{
distributions::{Distribution, OpenClosed01, Standard},
Rng,
};
use num::One; use num::One;
use num_complex::Complex; use num_complex::Complex;
use rand::distributions::{Distribution, OpenClosed01, Standard};
use rand::Rng;
use crate::base::dimension::{U1, U2}; use crate::base::dimension::{U1, U2};
use crate::base::storage::Storage; use crate::base::storage::Storage;
@ -377,6 +381,7 @@ where
} }
} }
#[cfg(feature = "rand-no-std")]
impl<N: SimdRealField> Distribution<UnitComplex<N>> for Standard impl<N: SimdRealField> Distribution<UnitComplex<N>> for Standard
where where
N::Element: SimdRealField, N::Element: SimdRealField,

View File

@ -89,26 +89,16 @@ an optimized set of tools for computer graphics and physics. Those features incl
#![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))] #![cfg_attr(all(feature = "alloc", not(feature = "std")), feature(alloc))]
#![cfg_attr(feature = "no_unsound_assume_init", allow(unreachable_code))] #![cfg_attr(feature = "no_unsound_assume_init", allow(unreachable_code))]
#[cfg(feature = "arbitrary")]
extern crate quickcheck;
#[cfg(feature = "serde-serialize")] #[cfg(feature = "serde-serialize")]
#[macro_use] #[macro_use]
extern crate serde; extern crate serde;
#[cfg(feature = "abomonation-serialize")] #[cfg(feature = "rand-no-std")]
extern crate abomonation; extern crate rand_package as rand;
#[cfg(feature = "mint")]
extern crate mint;
#[macro_use] #[macro_use]
extern crate approx; extern crate approx;
#[cfg(feature = "std")]
extern crate matrixmultiply;
extern crate num_traits as num; extern crate num_traits as num;
#[cfg(feature = "std")]
extern crate rand_distr;
#[cfg(all(feature = "alloc", not(feature = "std")))] #[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc; extern crate alloc;