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"
[features]
default = ["rand"]
unstable = []
swizzle = []
@ -24,7 +25,7 @@ swizzle = []
approx = "0.3"
mint = { version = "0.5", optional = true }
num-traits = "0.2"
rand = "0.6"
rand = { version = "0.6", optional = true }
serde = { version = "1.0", features = ["serde_derive"], optional = true }
simd = { version = "0.2", optional = true }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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