From cdb0e8c8a1b3a0bc455f9095a64f55eadc9b9ab5 Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Fri, 8 Jun 2018 13:39:02 +0200 Subject: [PATCH 1/3] Bumped rand and fixed compiler issues --- Cargo.toml | 2 +- src/angle.rs | 13 ++++++++----- src/euler.rs | 10 ++++++---- src/matrix.rs | 26 +++++++++++++++++--------- src/quaternion.rs | 10 +++++++--- src/vector.rs | 9 ++++++--- 6 files changed, 45 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 905eb68..9842c79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ swizzle = [] approx = "0.2" mint = { version = "0.5", optional = true } num-traits = "0.2" -rand = "0.4" +rand = "0.5" 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 188b90e..5fd0db8 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -20,8 +20,9 @@ use std::f64; use std::iter; use std::ops::*; -use rand::{Rand, Rng}; -use rand::distributions::range::SampleRange; +use rand::Rng; +use rand::distributions::{Distribution, Standard}; +use rand::distributions::uniform::SampleUniform; use num_traits::{cast, Bounded}; use structure::*; @@ -207,10 +208,12 @@ macro_rules! impl_angle { S::ulps_eq(&self.0, &other.0, epsilon, max_ulps) } } - - impl Rand for $Angle { + + impl Distribution<$Angle> for Standard + where Standard: Distribution, + S: BaseFloat + SampleUniform { #[inline] - fn rand(rng: &mut R) -> $Angle { + fn sample(&self, rng: &mut R) -> $Angle { $Angle(rng.gen_range(cast(-$hi).unwrap(), cast($hi).unwrap())) } } diff --git a/src/euler.rs b/src/euler.rs index c5bfa31..f175218 100644 --- a/src/euler.rs +++ b/src/euler.rs @@ -13,7 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; use num_traits::cast; use structure::*; @@ -185,9 +186,10 @@ impl approx::UlpsEq for Euler { } } -impl Rand for Euler { - #[inline] - fn rand(rng: &mut R) -> Euler { +impl Distribution> for Standard + where Standard: Distribution, + A: Angle { + fn sample(&self, rng: &mut R) -> Euler { Euler { x: rng.gen(), y: rng.gen(), diff --git a/src/matrix.rs b/src/matrix.rs index f000b4f..8236d81 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -13,7 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use rand::{Rand, Rng}; +use rand::distributions::{Standard, Distribution}; +use rand::Rng; use num_traits::{cast, NumCast}; use std::fmt; use std::iter; @@ -1529,19 +1530,24 @@ impl fmt::Debug for Matrix4 { } } -impl Rand for Matrix2 { +impl Distribution> for Standard + where + Standard: Distribution>, + S: BaseFloat { #[inline] - fn rand(rng: &mut R) -> Matrix2 { + fn sample(&self, rng: &mut R) -> Matrix2 { Matrix2 { - x: rng.gen(), - y: rng.gen(), + x: self.sample(rng), + y: self.sample(rng), } } } -impl Rand for Matrix3 { +impl Distribution> for Standard + where Standard: Distribution>, + S: BaseFloat { #[inline] - fn rand(rng: &mut R) -> Matrix3 { + fn sample(&self, rng: &mut R) -> Matrix3 { Matrix3 { x: rng.gen(), y: rng.gen(), @@ -1550,9 +1556,11 @@ impl Rand for Matrix3 { } } -impl Rand for Matrix4 { +impl Distribution> for Standard + where Standard: Distribution>, + S: BaseFloat { #[inline] - fn rand(rng: &mut R) -> Matrix4 { + fn sample(&self, rng: &mut R) -> Matrix4 { Matrix4 { x: rng.gen(), y: rng.gen(), diff --git a/src/quaternion.rs b/src/quaternion.rs index b2f01a4..e5a1315 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -17,7 +17,8 @@ use std::iter; use std::mem; use std::ops::*; -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; use num_traits::{cast, NumCast}; use structure::*; @@ -864,9 +865,12 @@ index_operators!(S, [S], RangeTo); index_operators!(S, [S], RangeFrom); index_operators!(S, [S], RangeFull); -impl Rand for Quaternion { +impl Distribution> for Standard + where Standard: Distribution, + Standard: Distribution>, + S: BaseFloat { #[inline] - fn rand(rng: &mut R) -> Quaternion { + fn sample(&self, rng: &mut R) -> Quaternion { Quaternion::from_sv(rng.gen(), rng.gen()) } } diff --git a/src/vector.rs b/src/vector.rs index 0e9da00..d27cb81 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -13,7 +13,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use rand::{Rand, Rng}; +use rand::distributions::{Distribution, Standard}; +use rand::Rng; use num_traits::{Bounded, NumCast}; use std::fmt; use std::iter; @@ -244,9 +245,11 @@ macro_rules! impl_vector { } } - impl Rand for $VectorN { + impl Distribution<$VectorN> for Standard + where Standard: Distribution, + S: BaseFloat { #[inline] - fn rand(rng: &mut R) -> $VectorN { + fn sample(&self, rng: &mut R) -> $VectorN { $VectorN { $($field: rng.gen()),+ } } } From df4f27e40e872721535b9319f04d8176b186e799 Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Fri, 8 Jun 2018 14:01:26 +0200 Subject: [PATCH 2/3] Fixed warnings --- benches/common/macros.rs | 6 +++--- benches/construction.rs | 5 +++-- benches/mat.rs | 3 ++- benches/quat.rs | 2 +- benches/vec.rs | 3 ++- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/benches/common/macros.rs b/benches/common/macros.rs index 54eac6d..265a1af 100644 --- a/benches/common/macros.rs +++ b/benches/common/macros.rs @@ -19,7 +19,7 @@ macro_rules! bench_binop { fn $name(bh: &mut Bencher) { const LEN: usize = 1 << 13; - let mut rng = IsaacRng::new_unseeded(); + let mut rng = IsaacRng::from_entropy(); let elems1: Vec<$t1> = (0..LEN).map(|_| rng.gen::<$t1>()).collect(); let elems2: Vec<$t2> = (0..LEN).map(|_| rng.gen::<$t2>()).collect(); @@ -42,7 +42,7 @@ macro_rules! bench_unop { fn $name(bh: &mut Bencher) { const LEN: usize = 1 << 13; - let mut rng = IsaacRng::new_unseeded(); + let mut rng = IsaacRng::from_entropy(); let mut elems: Vec<$t> = (0..LEN).map(|_| rng.gen::<$t>()).collect(); let mut i = 0; @@ -64,7 +64,7 @@ macro_rules! bench_construction { fn $name(bh: &mut Bencher) { const LEN: usize = 1 << 13; - let mut rng = IsaacRng::new_unseeded(); + let mut rng = IsaacRng::from_entropy(); $(let $args: Vec<$types> = (0..LEN).map(|_| rng.gen::<$types>()).collect();)* let mut i = 0; diff --git a/benches/construction.rs b/benches/construction.rs index b3a2a7b..97244e8 100644 --- a/benches/construction.rs +++ b/benches/construction.rs @@ -14,12 +14,13 @@ // limitations under the License. #![feature(test)] +#![allow(unused_macros)] extern crate cgmath; extern crate rand; extern crate test; -use rand::{IsaacRng, Rng}; +use rand::{IsaacRng, Rng, FromEntropy}; use test::Bencher; use cgmath::*; @@ -30,7 +31,7 @@ mod macros; fn bench_from_axis_angle>(bh: &mut Bencher) { const LEN: usize = 1 << 13; - let mut rng = IsaacRng::new_unseeded(); + let mut rng = IsaacRng::from_entropy(); let axis: Vec<_> = (0..LEN).map(|_| rng.gen::>()).collect(); let angle: Vec<_> = (0..LEN).map(|_| rng.gen::>()).collect(); diff --git a/benches/mat.rs b/benches/mat.rs index 5400792..f6fbf6e 100644 --- a/benches/mat.rs +++ b/benches/mat.rs @@ -14,12 +14,13 @@ // limitations under the License. #![feature(test)] +#![allow(unused_macros)] extern crate cgmath; extern crate rand; extern crate test; -use rand::{IsaacRng, Rng}; +use rand::{IsaacRng, Rng, FromEntropy}; use std::ops::*; use test::Bencher; diff --git a/benches/quat.rs b/benches/quat.rs index 57b0f93..0176216 100644 --- a/benches/quat.rs +++ b/benches/quat.rs @@ -19,7 +19,7 @@ extern crate cgmath; extern crate rand; extern crate test; -use rand::{IsaacRng, Rng}; +use rand::{IsaacRng, Rng, FromEntropy}; use std::ops::*; use test::Bencher; diff --git a/benches/vec.rs b/benches/vec.rs index 7d06275..c1e62d6 100644 --- a/benches/vec.rs +++ b/benches/vec.rs @@ -14,12 +14,13 @@ // limitations under the License. #![feature(test)] +#![allow(unused_macros)] extern crate cgmath; extern crate rand; extern crate test; -use rand::{IsaacRng, Rng}; +use rand::{IsaacRng, Rng, FromEntropy}; use std::ops::*; use test::Bencher; From bd88d9e93fbcb9405507fb75d037dbaeca35e20a Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Fri, 8 Jun 2018 14:35:12 +0200 Subject: [PATCH 3/3] Fixed a compiler issue with the 'simd' feature --- src/vector.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/vector.rs b/src/vector.rs index d27cb81..9acc7b3 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -502,9 +502,11 @@ macro_rules! impl_vector_default { } } - impl Rand for $VectorN { + impl Distribution<$VectorN> for Standard + where S: BaseFloat, + Standard: Distribution { #[inline] - fn rand(rng: &mut R) -> $VectorN { + fn sample(&self, rng: &mut R) -> $VectorN { $VectorN { $($field: rng.gen()),+ } } }