From 4bd8ebb38da02d4a80fc8dfaf52966887d83bf9b Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Thu, 2 May 2019 11:24:50 +0200 Subject: [PATCH 1/3] 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. --- Cargo.toml | 3 ++- src/angle.rs | 9 ++++++--- src/euler.rs | 8 ++++++-- src/lib.rs | 1 + src/matrix.rs | 12 +++++++++--- src/quaternion.rs | 10 +++++++--- src/vector.rs | 11 ++++++++--- 7 files changed, 39 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6554189..32df254 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/angle.rs b/src/angle.rs index f932914..75e46c9 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -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 Distribution<$Angle> for Standard where Standard: Distribution, S: BaseFloat + SampleUniform { diff --git a/src/euler.rs b/src/euler.rs index f281e3d..ea09969 100644 --- a/src/euler.rs +++ b/src/euler.rs @@ -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 approx::UlpsEq for Euler { } } +#[cfg(feature = "rand")] impl Distribution> for Standard where Standard: Distribution, A: Angle { diff --git a/src/lib.rs b/src/lib.rs index b840cb8..bf06094 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/matrix.rs b/src/matrix.rs index 3219b1b..8b50535 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -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 fmt::Debug for Matrix4 { } } +#[cfg(feature = "rand")] impl Distribution> for Standard - where + where Standard: Distribution>, S: BaseFloat { #[inline] @@ -1549,6 +1553,7 @@ impl Distribution> for Standard } } +#[cfg(feature = "rand")] impl Distribution> for Standard where Standard: Distribution>, S: BaseFloat { @@ -1562,6 +1567,7 @@ impl Distribution> for Standard } } +#[cfg(feature = "rand")] impl Distribution> for Standard where Standard: Distribution>, S: BaseFloat { diff --git a/src/quaternion.rs b/src/quaternion.rs index 1d9b4c3..3bd6b42 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -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); index_operators!(S, [S], RangeFrom); index_operators!(S, [S], RangeFull); -impl Distribution> for Standard +#[cfg(feature = "rand")] +impl Distribution> for Standard where Standard: Distribution, Standard: Distribution>, S: BaseFloat { diff --git a/src/vector.rs b/src/vector.rs index ad97329..1f569b1 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -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 Distribution<$VectorN> for Standard where Standard: Distribution, S: BaseFloat { @@ -502,7 +506,8 @@ macro_rules! impl_vector_default { } } - impl Distribution<$VectorN> for Standard + #[cfg(feature = "rand")] + impl Distribution<$VectorN> for Standard where S: BaseFloat, Standard: Distribution { #[inline] From 9e650fd8ad92948ff172687318e0bb8ab90c6e29 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Thu, 2 May 2019 18:05:14 +0200 Subject: [PATCH 2/3] Adjust Travis-CI config to build with and without `rand` --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 98fd627..9a793d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ cache: cargo env: - CARGO_FEATURES="" - - CARGO_FEATURES="mint serde" + - CARGO_FEATURES="mint serde rand" matrix: include: @@ -21,8 +21,8 @@ matrix: - rust: nightly script: - - cargo build --features "$CARGO_FEATURES" - - cargo test --features "$CARGO_FEATURES" + - cargo build --no-default-features --features "$CARGO_FEATURES" + - cargo test --no-default-features --features "$CARGO_FEATURES" - | if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then cargo bench --features "$CARGO_FEATURES" From 31cb9eb574ee07d1b34fea1a32e6259cfe1db442 Mon Sep 17 00:00:00 2001 From: Lukas Kalbertodt Date: Fri, 3 May 2019 16:31:14 +0200 Subject: [PATCH 3/3] Remove `rand` from default features --- .travis.yml | 7 ++++--- Cargo.toml | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9a793d5..dfac6df 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,9 +21,10 @@ matrix: - rust: nightly script: - - cargo build --no-default-features --features "$CARGO_FEATURES" - - cargo test --no-default-features --features "$CARGO_FEATURES" + - cargo build --features "$CARGO_FEATURES" + - cargo test --features "$CARGO_FEATURES" - | if [[ "$TRAVIS_RUST_VERSION" == "nightly" ]]; then - cargo bench --features "$CARGO_FEATURES" + # The benchmark always needs the 'rand' feature + cargo bench --features "$CARGO_FEATURES rand" fi diff --git a/Cargo.toml b/Cargo.toml index 32df254..cb9662d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,6 @@ keywords = ["gamedev", "math", "matrix", "vector", "quaternion"] name = "cgmath" [features] -default = ["rand"] unstable = [] swizzle = []