-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,8 @@ | |
|
||
#[cfg(feature = "std")] | ||
use std::time::Duration; | ||
use core::borrow::Borrow; | ||
|
||
|
||
use Rng; | ||
use distributions::Distribution; | ||
|
@@ -229,12 +231,10 @@ pub trait UniformSampler: Sized { | |
/// sampling only a single value from the specified range. The default | ||
/// implementation simply calls `UniformSampler::new` then `sample` on the | ||
/// result. | ||
fn sample_single<R: Rng + ?Sized>(low: Self::X, high: Self::X, rng: &mut R) | ||
fn sample_single<R: Rng + ?Sized, B1, B2>(low: B1, high: B2, rng: &mut R) | ||
-> Self::X | ||
{ | ||
let uniform: Self = UniformSampler::new(low, high); | ||
uniform.sample(rng) | ||
} | ||
where B1: Borrow<Self::X> + Sized, | ||
B2: Borrow<Self::X> + Sized; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sicking
Author
Owner
|
||
} | ||
|
||
impl<X: SampleUniform> From<::core::ops::Range<X>> for Uniform<X> { | ||
|
@@ -362,10 +362,13 @@ macro_rules! uniform_int_impl { | |
} | ||
} | ||
|
||
fn sample_single<R: Rng + ?Sized>(low: Self::X, | ||
high: Self::X, | ||
rng: &mut R) -> Self::X | ||
fn sample_single<R: Rng + ?Sized, B1, B2>(low_b: B1, high_b: B2, rng: &mut R) | ||
-> Self::X | ||
where B1: Borrow<Self::X> + Sized, | ||
B2: Borrow<Self::X> + Sized | ||
{ | ||
let low = *low_b.borrow(); | ||
let high = *high_b.borrow(); | ||
This comment has been minimized.
Sorry, something went wrong.
dhardy
|
||
assert!(low < high, | ||
"Uniform::sample_single called with low >= high"); | ||
let range = high.wrapping_sub(low) as $unsigned as $u_large; | ||
|
@@ -565,9 +568,13 @@ macro_rules! uniform_float_impl { | |
value1_2 * self.scale + self.offset | ||
} | ||
|
||
fn sample_single<R: Rng + ?Sized>(low: Self::X, | ||
high: Self::X, | ||
rng: &mut R) -> Self::X { | ||
fn sample_single<R: Rng + ?Sized, B1, B2>(low_b: B1, high_b: B2, rng: &mut R) | ||
-> Self::X | ||
where B1: Borrow<Self::X> + Sized, | ||
B2: Borrow<Self::X> + Sized | ||
{ | ||
let low = *low_b.borrow(); | ||
let high = *high_b.borrow(); | ||
assert!(low < high, | ||
"Uniform::sample_single called with low >= high"); | ||
let scale = high - low; | ||
|
@@ -679,6 +686,15 @@ impl UniformSampler for UniformDuration { | |
|
||
self.offset + d | ||
} | ||
|
||
fn sample_single<R: Rng + ?Sized, B1, B2>(low: B1, high: B2, rng: &mut R) | ||
-> Self::X | ||
where B1: Borrow<Self::X> + Sized, | ||
B2: Borrow<Self::X> + Sized | ||
{ | ||
let uniform: Self = UniformSampler::new(*low.borrow(), *high.borrow()); | ||
uniform.sample(rng) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
@@ -809,6 +825,7 @@ mod tests { | |
|
||
#[test] | ||
fn test_custom_uniform() { | ||
use core::borrow::Borrow; | ||
#[derive(Clone, Copy, PartialEq, PartialOrd)] | ||
struct MyF32 { | ||
x: f32, | ||
|
@@ -830,6 +847,13 @@ mod tests { | |
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Self::X { | ||
MyF32 { x: self.inner.sample(rng) } | ||
} | ||
fn sample_single<R: Rng + ?Sized, B1, B2>(low: B1, high: B2, rng: &mut R) | ||
-> Self::X | ||
where B1: Borrow<Self::X> + Sized, | ||
B2: Borrow<Self::X> + Sized | ||
{ | ||
Self::new(*low.borrow(), *high.borrow()).sample(rng) | ||
} | ||
} | ||
impl SampleUniform for MyF32 { | ||
type Sampler = UniformMyF32; | ||
|
Can we keep the default implementation? I guess this version would require
X: Clone
but presumably the full implementation would useBorrow
innew
too, so wouldn't.Edit: I see there's less motivation for making this change to
new
, but in any case I don't think requiringX: Clone
is a big deal?