Make rand optional

Tests pass with default features (rand_with_std)
Tests pass with std only, with --tests restriction (doc tests fail)
This commit is contained in:
Diggory Hardy 2019-09-16 11:32:06 +01:00
parent 01f155b00a
commit 6ebb768309
16 changed files with 56 additions and 45 deletions

View File

@ -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 }

View File

@ -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
fi

View File

@ -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<N, R, C>
/// 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<N> {
let mut rng = rand::thread_rng();
@ -248,6 +246,7 @@ where DefaultAllocator: Allocator<N, R, C>
}
/// Creates a matrix filled with random values from the given distribution.
#[cfg(feature = "rand")]
#[inline]
pub fn from_distribution_generic<Distr: Distribution<N> + ?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<Distr: Distribution<N> + ?Sized, G: Rng + ?Sized>(
$($args: usize,)*
@ -558,6 +558,7 @@ macro_rules! impl_constructors(
}
}
#[cfg(feature = "rand")]
impl<N: Scalar, $($DimIdent: $DimBound, )*> MatrixMN<N $(, $Dims)*>
where
DefaultAllocator: Allocator<N $(, $Dims)*>,
@ -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<N: Scalar, R: Dim, C: Dim> Distribution<MatrixMN<N, R, C>> for Standard
where
DefaultAllocator: Allocator<N, R, C>,
@ -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<N: RealField, D: DimName> Distribution<Unit<VectorN<N, D>>> for Standard
where
DefaultAllocator: Allocator<N, D>,
StandardNormal: Distribution<N>,
rand_distr::StandardNormal: Distribution<N>,
{
/// 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,
))
}

View File

@ -1,3 +1,4 @@
#![cfg(feature = "rand")]
#[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen};
use rand::distributions::{Distribution, Standard};

View File

@ -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<N, D>
}
}
#[cfg(feature = "rand")]
impl<N: RealField, D: DimName, R> Distribution<Isometry<N, D, R>> for Standard
where
R: AlgaRotation<Point<N, D>>,

View File

@ -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<N: RealField> Orthographic3<N> {
}
}
#[cfg(feature = "rand")]
impl<N: RealField> Distribution<Orthographic3<N>> for Standard
where Standard: Distribution<N>
{

View File

@ -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<N: RealField> Perspective3<N> {
}
}
#[cfg(feature = "rand")]
impl<N: RealField> Distribution<Perspective3<N>> for Standard
where Standard: Distribution<N>
{

View File

@ -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<N, D>
}
}
#[cfg(feature = "rand")]
impl<N: Scalar, D: DimName> Distribution<Point<N, D>> for Standard
where
DefaultAllocator: Allocator<N, D>,

View File

@ -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<N: RealField> Zero for Quaternion<N> {
}
}
#[cfg(feature = "rand")]
impl<N: RealField> Distribution<Quaternion<N>> for Standard
where Standard: Distribution<N>
{
@ -685,6 +686,7 @@ impl<N: RealField> One for UnitQuaternion<N> {
}
}
#[cfg(feature = "rand")]
impl<N: RealField> Distribution<UnitQuaternion<N>> for Standard
where OpenClosed01: Distribution<N>
{
@ -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() {

View File

@ -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<N: RealField> Rotation2<N> {
}
}
#[cfg(feature = "rand")]
impl<N: RealField> Distribution<Rotation2<N>> for Standard
where N: SampleUniform
{
@ -817,6 +820,7 @@ impl<N: RealField> Rotation3<N> {
}
}
#[cfg(feature = "rand")]
impl<N: RealField> Distribution<Rotation3<N>> for Standard
where OpenClosed01: Distribution<N>, N: SampleUniform
{

View File

@ -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<N: RealField, D: DimName, R> Distribution<Similarity<N, D, R>> for Standard
where
R: AlgaRotation<Point<N, D>>,

View File

@ -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<N, D>
}
}
#[cfg(feature = "rand")]
impl<N: Scalar, D: DimName> Distribution<Translation<N, D>> for Standard
where
DefaultAllocator: Allocator<N, D>,

View File

@ -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<N: RealField> One for UnitComplex<N> {
}
}
#[cfg(feature = "std")]
#[cfg(feature = "rand_distr")]
impl<N: RealField> Distribution<UnitComplex<N>> 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<N: RealField> Distribution<UnitComplex<N>> 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<N> {
UnitComplex::from_angle(rng.gen_range(N::zero(), N::two_pi()))
}
}
#[cfg(feature = "arbitrary")]
impl<N: RealField + Arbitrary> Arbitrary for UnitComplex<N> {
#[inline]

View File

@ -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;

View File

@ -354,7 +354,7 @@ mod test {
}
}
#[cfg(feature = "std")]
#[cfg(feature = "rand_with_std")]
#[test]
fn wilkinson_shift_random() {
for _ in 0..1000 {

View File

@ -466,6 +466,7 @@ fn zip_map() {
assert_eq!(computed, expected);
}
#[cfg(feature = "rand_with_std")]
#[test]
#[should_panic]
fn trace_panic() {