Skip to content

Commit f78781a

Browse files
committed
Uniform: replace inner field with struct tuple
1 parent 856fdfb commit f78781a

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

src/distributions/uniform.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,15 @@
6666
//! struct MyF32(f32);
6767
//!
6868
//! #[derive(Clone, Copy, Debug)]
69-
//! struct UniformMyF32 {
70-
//! inner: UniformFloat<f32>,
71-
//! }
69+
//! struct UniformMyF32(UniformFloat<f32>);
7270
//!
7371
//! impl UniformSampler for UniformMyF32 {
7472
//! type X = MyF32;
7573
//! fn new<B1, B2>(low: B1, high: B2) -> Self
7674
//! where B1: SampleBorrow<Self::X> + Sized,
7775
//! B2: SampleBorrow<Self::X> + Sized
7876
//! {
79-
//! UniformMyF32 {
80-
//! inner: UniformFloat::<f32>::new(low.borrow().0, high.borrow().0),
81-
//! }
77+
//! UniformMyF32(UniformFloat::<f32>::new(low.borrow().0, high.borrow().0))
8278
//! }
8379
//! fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
8480
//! where B1: SampleBorrow<Self::X> + Sized,
@@ -87,7 +83,7 @@
8783
//! UniformSampler::new(low, high)
8884
//! }
8985
//! fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
90-
//! MyF32(self.inner.sample(rng))
86+
//! MyF32(self.0.sample(rng))
9187
//! }
9288
//! }
9389
//!
@@ -166,9 +162,7 @@ use packed_simd::*;
166162
/// [`new`]: Uniform::new
167163
/// [`new_inclusive`]: Uniform::new_inclusive
168164
#[derive(Clone, Copy, Debug)]
169-
pub struct Uniform<X: SampleUniform> {
170-
inner: X::Sampler,
171-
}
165+
pub struct Uniform<X: SampleUniform>(X::Sampler);
172166

173167
impl<X: SampleUniform> Uniform<X> {
174168
/// Create a new `Uniform` instance which samples uniformly from the half
@@ -177,7 +171,7 @@ impl<X: SampleUniform> Uniform<X> {
177171
where B1: SampleBorrow<X> + Sized,
178172
B2: SampleBorrow<X> + Sized
179173
{
180-
Uniform { inner: X::Sampler::new(low, high) }
174+
Uniform(X::Sampler::new(low, high))
181175
}
182176

183177
/// Create a new `Uniform` instance which samples uniformly from the closed
@@ -186,13 +180,13 @@ impl<X: SampleUniform> Uniform<X> {
186180
where B1: SampleBorrow<X> + Sized,
187181
B2: SampleBorrow<X> + Sized
188182
{
189-
Uniform { inner: X::Sampler::new_inclusive(low, high) }
183+
Uniform(X::Sampler::new_inclusive(low, high))
190184
}
191185
}
192186

193187
impl<X: SampleUniform> Distribution<X> for Uniform<X> {
194188
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> X {
195-
self.inner.sample(rng)
189+
self.0.sample(rng)
196190
}
197191
}
198192

@@ -1211,18 +1205,14 @@ mod tests {
12111205
x: f32,
12121206
}
12131207
#[derive(Clone, Copy, Debug)]
1214-
struct UniformMyF32 {
1215-
inner: UniformFloat<f32>,
1216-
}
1208+
struct UniformMyF32(UniformFloat<f32>);
12171209
impl UniformSampler for UniformMyF32 {
12181210
type X = MyF32;
12191211
fn new<B1, B2>(low: B1, high: B2) -> Self
12201212
where B1: SampleBorrow<Self::X> + Sized,
12211213
B2: SampleBorrow<Self::X> + Sized
12221214
{
1223-
UniformMyF32 {
1224-
inner: UniformFloat::<f32>::new(low.borrow().x, high.borrow().x),
1225-
}
1215+
UniformMyF32(UniformFloat::<f32>::new(low.borrow().x, high.borrow().x))
12261216
}
12271217
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
12281218
where B1: SampleBorrow<Self::X> + Sized,
@@ -1231,7 +1221,7 @@ mod tests {
12311221
UniformSampler::new(low, high)
12321222
}
12331223
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
1234-
MyF32 { x: self.inner.sample(rng) }
1224+
MyF32 { x: self.0.sample(rng) }
12351225
}
12361226
}
12371227
impl SampleUniform for MyF32 {
@@ -1250,21 +1240,21 @@ mod tests {
12501240
#[test]
12511241
fn test_uniform_from_std_range() {
12521242
let r = Uniform::from(2u32..7);
1253-
assert_eq!(r.inner.low, 2);
1254-
assert_eq!(r.inner.range, 5);
1243+
assert_eq!(r.0.low, 2);
1244+
assert_eq!(r.0.range, 5);
12551245
let r = Uniform::from(2.0f64..7.0);
1256-
assert_eq!(r.inner.low, 2.0);
1257-
assert_eq!(r.inner.scale, 5.0);
1246+
assert_eq!(r.0.low, 2.0);
1247+
assert_eq!(r.0.scale, 5.0);
12581248
}
12591249

12601250
#[test]
12611251
fn test_uniform_from_std_range_inclusive() {
12621252
let r = Uniform::from(2u32..=6);
1263-
assert_eq!(r.inner.low, 2);
1264-
assert_eq!(r.inner.range, 5);
1253+
assert_eq!(r.0.low, 2);
1254+
assert_eq!(r.0.range, 5);
12651255
let r = Uniform::from(2.0f64..=7.0);
1266-
assert_eq!(r.inner.low, 2.0);
1267-
assert!(r.inner.scale > 5.0);
1268-
assert!(r.inner.scale < 5.0 + 1e-14);
1256+
assert_eq!(r.0.low, 2.0);
1257+
assert!(r.0.scale > 5.0);
1258+
assert!(r.0.scale < 5.0 + 1e-14);
12691259
}
12701260
}

0 commit comments

Comments
 (0)