Make rand dependency optional (but enabled by default)

Most users probably don't use the rand impls, so the `rand` crate pulls
a large number of dependencies into the dependency tree which is just
wasted compilation time.
This commit is contained in:
Lukas Kalbertodt 2019-05-02 11:24:50 +02:00
parent 93786bfc9a
commit 4bd8ebb38d
No known key found for this signature in database
GPG key ID: 3CBAF4153F818627
7 changed files with 39 additions and 15 deletions

View file

@ -17,6 +17,7 @@ keywords = ["gamedev", "math", "matrix", "vector", "quaternion"]
name = "cgmath" name = "cgmath"
[features] [features]
default = ["rand"]
unstable = [] unstable = []
swizzle = [] swizzle = []
@ -24,7 +25,7 @@ swizzle = []
approx = "0.3" approx = "0.3"
mint = { version = "0.5", optional = true } mint = { version = "0.5", optional = true }
num-traits = "0.2" num-traits = "0.2"
rand = "0.6" rand = { version = "0.6", optional = true }
serde = { version = "1.0", features = ["serde_derive"], optional = true } serde = { version = "1.0", features = ["serde_derive"], optional = true }
simd = { version = "0.2", optional = true } simd = { version = "0.2", optional = true }

View file

@ -20,9 +20,11 @@ use std::f64;
use std::iter; use std::iter;
use std::ops::*; use std::ops::*;
use rand::Rng; #[cfg(feature = "rand")]
use rand::distributions::{Distribution, Standard}; use rand::{
use rand::distributions::uniform::SampleUniform; Rng,
distributions::{Distribution, Standard, uniform::SampleUniform},
};
use num_traits::{cast, Bounded}; use num_traits::{cast, Bounded};
use structure::*; use structure::*;
@ -209,6 +211,7 @@ macro_rules! impl_angle {
} }
} }
#[cfg(feature = "rand")]
impl<S> Distribution<$Angle<S>> for Standard impl<S> Distribution<$Angle<S>> for Standard
where Standard: Distribution<S>, where Standard: Distribution<S>,
S: BaseFloat + SampleUniform { S: BaseFloat + SampleUniform {

View file

@ -13,8 +13,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand")]
use rand::Rng; use rand::{
Rng,
distributions::{Distribution, Standard},
};
use num_traits::cast; use num_traits::cast;
use structure::*; use structure::*;
@ -186,6 +189,7 @@ impl<A: Angle> approx::UlpsEq for Euler<A> {
} }
} }
#[cfg(feature = "rand")]
impl<A> Distribution<Euler<A>> for Standard impl<A> Distribution<Euler<A>> for Standard
where Standard: Distribution<A>, where Standard: Distribution<A>,
A: Angle { A: Angle {

View file

@ -58,6 +58,7 @@ extern crate approx;
#[cfg(feature = "mint")] #[cfg(feature = "mint")]
pub extern crate mint; pub extern crate mint;
#[cfg(feature = "rand")]
extern crate rand; extern crate rand;
pub extern crate num_traits; pub extern crate num_traits;

View file

@ -13,8 +13,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use rand::distributions::{Standard, Distribution}; #[cfg(feature = "rand")]
use rand::Rng; use rand::{
Rng,
distributions::{Standard, Distribution},
};
use num_traits::{cast, NumCast}; use num_traits::{cast, NumCast};
use std::fmt; use std::fmt;
use std::iter; use std::iter;
@ -1536,6 +1539,7 @@ impl<S: fmt::Debug> fmt::Debug for Matrix4<S> {
} }
} }
#[cfg(feature = "rand")]
impl<S> Distribution<Matrix2<S>> for Standard impl<S> Distribution<Matrix2<S>> for Standard
where where
Standard: Distribution<Vector2<S>>, Standard: Distribution<Vector2<S>>,
@ -1549,6 +1553,7 @@ impl<S> Distribution<Matrix2<S>> for Standard
} }
} }
#[cfg(feature = "rand")]
impl<S> Distribution<Matrix3<S>> for Standard impl<S> Distribution<Matrix3<S>> for Standard
where Standard: Distribution<Vector3<S>>, where Standard: Distribution<Vector3<S>>,
S: BaseFloat { S: BaseFloat {
@ -1562,6 +1567,7 @@ impl<S> Distribution<Matrix3<S>> for Standard
} }
} }
#[cfg(feature = "rand")]
impl<S> Distribution<Matrix4<S>> for Standard impl<S> Distribution<Matrix4<S>> for Standard
where Standard: Distribution<Vector4<S>>, where Standard: Distribution<Vector4<S>>,
S: BaseFloat { S: BaseFloat {

View file

@ -17,8 +17,11 @@ use std::iter;
use std::mem; use std::mem;
use std::ops::*; use std::ops::*;
use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand")]
use rand::Rng; use rand::{
Rng,
distributions::{Distribution, Standard},
};
use num_traits::{cast, NumCast}; use num_traits::{cast, NumCast};
use structure::*; use structure::*;
@ -870,6 +873,7 @@ index_operators!(S, [S], RangeTo<usize>);
index_operators!(S, [S], RangeFrom<usize>); index_operators!(S, [S], RangeFrom<usize>);
index_operators!(S, [S], RangeFull); index_operators!(S, [S], RangeFull);
#[cfg(feature = "rand")]
impl<S> Distribution<Quaternion<S>> for Standard impl<S> Distribution<Quaternion<S>> for Standard
where Standard: Distribution<S>, where Standard: Distribution<S>,
Standard: Distribution<Vector3<S>>, Standard: Distribution<Vector3<S>>,

View file

@ -13,8 +13,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use rand::distributions::{Distribution, Standard}; #[cfg(feature = "rand")]
use rand::Rng; use rand::{
Rng,
distributions::{Distribution, Standard},
};
use num_traits::{Bounded, NumCast}; use num_traits::{Bounded, NumCast};
use std::fmt; use std::fmt;
use std::iter; use std::iter;
@ -245,6 +248,7 @@ macro_rules! impl_vector {
} }
} }
#[cfg(feature = "rand")]
impl<S> Distribution<$VectorN<S>> for Standard impl<S> Distribution<$VectorN<S>> for Standard
where Standard: Distribution<S>, where Standard: Distribution<S>,
S: BaseFloat { S: BaseFloat {
@ -502,6 +506,7 @@ macro_rules! impl_vector_default {
} }
} }
#[cfg(feature = "rand")]
impl<S> Distribution<$VectorN<S>> for Standard impl<S> Distribution<$VectorN<S>> for Standard
where S: BaseFloat, where S: BaseFloat,
Standard: Distribution<S> { Standard: Distribution<S> {