From f78781aa84142f95f6397431958fc60bef555ca0 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Sun, 15 Sep 2019 12:01:43 +0100 Subject: [PATCH] Uniform: replace inner field with struct tuple --- src/distributions/uniform.rs | 48 ++++++++++++++---------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/src/distributions/uniform.rs b/src/distributions/uniform.rs index 8c90f4e4bd2..498cd5df701 100644 --- a/src/distributions/uniform.rs +++ b/src/distributions/uniform.rs @@ -66,9 +66,7 @@ //! struct MyF32(f32); //! //! #[derive(Clone, Copy, Debug)] -//! struct UniformMyF32 { -//! inner: UniformFloat, -//! } +//! struct UniformMyF32(UniformFloat); //! //! impl UniformSampler for UniformMyF32 { //! type X = MyF32; @@ -76,9 +74,7 @@ //! where B1: SampleBorrow + Sized, //! B2: SampleBorrow + Sized //! { -//! UniformMyF32 { -//! inner: UniformFloat::::new(low.borrow().0, high.borrow().0), -//! } +//! UniformMyF32(UniformFloat::::new(low.borrow().0, high.borrow().0)) //! } //! fn new_inclusive(low: B1, high: B2) -> Self //! where B1: SampleBorrow + Sized, @@ -87,7 +83,7 @@ //! UniformSampler::new(low, high) //! } //! fn sample(&self, rng: &mut R) -> Self::X { -//! MyF32(self.inner.sample(rng)) +//! MyF32(self.0.sample(rng)) //! } //! } //! @@ -166,9 +162,7 @@ use packed_simd::*; /// [`new`]: Uniform::new /// [`new_inclusive`]: Uniform::new_inclusive #[derive(Clone, Copy, Debug)] -pub struct Uniform { - inner: X::Sampler, -} +pub struct Uniform(X::Sampler); impl Uniform { /// Create a new `Uniform` instance which samples uniformly from the half @@ -177,7 +171,7 @@ impl Uniform { where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized { - Uniform { inner: X::Sampler::new(low, high) } + Uniform(X::Sampler::new(low, high)) } /// Create a new `Uniform` instance which samples uniformly from the closed @@ -186,13 +180,13 @@ impl Uniform { where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized { - Uniform { inner: X::Sampler::new_inclusive(low, high) } + Uniform(X::Sampler::new_inclusive(low, high)) } } impl Distribution for Uniform { fn sample(&self, rng: &mut R) -> X { - self.inner.sample(rng) + self.0.sample(rng) } } @@ -1211,18 +1205,14 @@ mod tests { x: f32, } #[derive(Clone, Copy, Debug)] - struct UniformMyF32 { - inner: UniformFloat, - } + struct UniformMyF32(UniformFloat); impl UniformSampler for UniformMyF32 { type X = MyF32; fn new(low: B1, high: B2) -> Self where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized { - UniformMyF32 { - inner: UniformFloat::::new(low.borrow().x, high.borrow().x), - } + UniformMyF32(UniformFloat::::new(low.borrow().x, high.borrow().x)) } fn new_inclusive(low: B1, high: B2) -> Self where B1: SampleBorrow + Sized, @@ -1231,7 +1221,7 @@ mod tests { UniformSampler::new(low, high) } fn sample(&self, rng: &mut R) -> Self::X { - MyF32 { x: self.inner.sample(rng) } + MyF32 { x: self.0.sample(rng) } } } impl SampleUniform for MyF32 { @@ -1250,21 +1240,21 @@ mod tests { #[test] fn test_uniform_from_std_range() { let r = Uniform::from(2u32..7); - assert_eq!(r.inner.low, 2); - assert_eq!(r.inner.range, 5); + assert_eq!(r.0.low, 2); + assert_eq!(r.0.range, 5); let r = Uniform::from(2.0f64..7.0); - assert_eq!(r.inner.low, 2.0); - assert_eq!(r.inner.scale, 5.0); + assert_eq!(r.0.low, 2.0); + assert_eq!(r.0.scale, 5.0); } #[test] fn test_uniform_from_std_range_inclusive() { let r = Uniform::from(2u32..=6); - assert_eq!(r.inner.low, 2); - assert_eq!(r.inner.range, 5); + assert_eq!(r.0.low, 2); + assert_eq!(r.0.range, 5); let r = Uniform::from(2.0f64..=7.0); - assert_eq!(r.inner.low, 2.0); - assert!(r.inner.scale > 5.0); - assert!(r.inner.scale < 5.0 + 1e-14); + assert_eq!(r.0.low, 2.0); + assert!(r.0.scale > 5.0); + assert!(r.0.scale < 5.0 + 1e-14); } }