Skip to content

Commit

Permalink
Implement Distribution<usize> for RangeTo(Inclusive)<usize>
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Jul 1, 2019
1 parent b25b657 commit 431e99d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/distributions/uniform.rs
Expand Up @@ -111,7 +111,7 @@
use std::time::Duration;
#[cfg(not(feature = "std"))]
use core::time::Duration;
use core::ops::{Range, RangeInclusive};
use core::ops::{Range, RangeInclusive, RangeTo, RangeToInclusive};

use crate::Rng;
use crate::distributions::Distribution;
Expand Down Expand Up @@ -306,6 +306,18 @@ impl<T: SampleUniform> Distribution<T> for RangeInclusive<T> {
}
}

impl Distribution<usize> for RangeTo<usize> {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize {
<usize as SampleUniform>::Sampler::sample_single(0, self.end, rng)
}
}

impl Distribution<usize> for RangeToInclusive<usize> {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> usize {
<usize as SampleUniform>::Sampler::sample_single_inclusive(0, self.end, rng)
}
}

/// Helper trait similar to [`Borrow`] but implemented
/// only for SampleUniform and references to SampleUniform in
/// order to resolve ambiguity issues.
Expand Down

0 comments on commit 431e99d

Please sign in to comment.