Skip to content

Commit 03062f1

Browse files
committed
Move SampleRange to distributions::uniform
1 parent f727b95 commit 03062f1

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

src/distributions/uniform.rs

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 Developers of the Rand project.
1+
// Copyright 2018-2020 Developers of the Rand project.
22
// Copyright 2017 The Rust Project Developers.
33
//
44
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
@@ -110,7 +110,7 @@ use core::ops::{Range, RangeInclusive};
110110
use crate::distributions::float::IntoFloat;
111111
use crate::distributions::utils::{BoolAsSIMD, FloatAsSIMD, FloatSIMDUtils, WideningMultiply};
112112
use crate::distributions::Distribution;
113-
use crate::Rng;
113+
use crate::{Rng, RngCore};
114114

115115
#[cfg(not(feature = "std"))]
116116
#[allow(unused_imports)] // rustc doesn't detect that this is actually used
@@ -338,6 +338,43 @@ where Borrowed: SampleUniform
338338
}
339339
}
340340

341+
/// Range that supports generating a single sample efficiently.
342+
///
343+
/// Any type implementing this trait can be used to specify the sampled range
344+
/// for `Rng::gen_range`.
345+
pub trait SampleRange<T> {
346+
/// Generate a sample from the given range.
347+
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T;
348+
349+
/// Check whether the range is empty.
350+
fn is_empty(&self) -> bool;
351+
}
352+
353+
impl<T: SampleUniform + PartialOrd> SampleRange<T> for Range<T> {
354+
#[inline]
355+
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
356+
T::Sampler::sample_single(self.start, self.end, rng)
357+
}
358+
359+
#[inline]
360+
fn is_empty(&self) -> bool {
361+
!(self.start < self.end)
362+
}
363+
}
364+
365+
impl<T: SampleUniform + PartialOrd> SampleRange<T> for RangeInclusive<T> {
366+
#[inline]
367+
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
368+
T::Sampler::sample_single_inclusive(self.start(), self.end(), rng)
369+
}
370+
371+
#[inline]
372+
fn is_empty(&self) -> bool {
373+
!(self.start() <= self.end())
374+
}
375+
}
376+
377+
341378
////////////////////////////////////////////////////////////////////////////////
342379

343380
// What follows are all back-ends.

src/rng.rs

+1-38
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,10 @@
1010
//! [`Rng`] trait
1111
1212
use rand_core::{Error, RngCore};
13-
use crate::distributions::uniform::{SampleUniform, UniformSampler};
13+
use crate::distributions::uniform::{SampleRange, SampleUniform};
1414
use crate::distributions::{self, Distribution, Standard};
1515
use core::num::Wrapping;
1616
use core::{mem, slice};
17-
use core::ops::{Range, RangeInclusive};
18-
19-
/// Range that supports generating a single sample efficiently.
20-
///
21-
/// Any type implementing this trait can be used to specify the sampled range
22-
/// for `Rng::gen_range`.
23-
pub trait SampleRange<T> {
24-
/// Generate a sample from the given range.
25-
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T;
26-
27-
/// Check whether the range is empty.
28-
fn is_empty(&self) -> bool;
29-
}
30-
31-
impl<T: SampleUniform + PartialOrd> SampleRange<T> for Range<T> {
32-
#[inline]
33-
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
34-
T::Sampler::sample_single(self.start, self.end, rng)
35-
}
36-
37-
#[inline]
38-
fn is_empty(&self) -> bool {
39-
!(self.start < self.end)
40-
}
41-
}
42-
43-
impl<T: SampleUniform + PartialOrd> SampleRange<T> for RangeInclusive<T> {
44-
#[inline]
45-
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
46-
T::Sampler::sample_single_inclusive(self.start(), self.end(), rng)
47-
}
48-
49-
#[inline]
50-
fn is_empty(&self) -> bool {
51-
!(self.start() <= self.end())
52-
}
53-
}
5417

5518
/// An automatically-implemented extension trait on [`RngCore`] providing high-level
5619
/// generic methods for sampling values and other convenience methods.

0 commit comments

Comments
 (0)