@@ -120,7 +120,7 @@ mod other;
120120pub use other:: { UniformChar , UniformDuration } ;
121121
122122use core:: fmt;
123- use core:: ops:: { Range , RangeInclusive } ;
123+ use core:: ops:: { Range , RangeInclusive , RangeTo , RangeToInclusive } ;
124124
125125use crate :: distr:: Distribution ;
126126use crate :: { Rng , RngCore } ;
@@ -439,6 +439,41 @@ impl<T: SampleUniform + PartialOrd> SampleRange<T> for RangeInclusive<T> {
439439 }
440440}
441441
442+ macro_rules! impl_sample_range_u {
443+ ( $t: ty) => {
444+ impl SampleRange <$t> for RangeTo <$t> {
445+ #[ inline]
446+ fn sample_single<R : RngCore + ?Sized >( self , rng: & mut R ) -> Result <$t, Error > {
447+ <$t as SampleUniform >:: Sampler :: sample_single( 0 , self . end, rng)
448+ }
449+
450+ #[ inline]
451+ fn is_empty( & self ) -> bool {
452+ 0 == self . end
453+ }
454+ }
455+
456+ impl SampleRange <$t> for RangeToInclusive <$t> {
457+ #[ inline]
458+ fn sample_single<R : RngCore + ?Sized >( self , rng: & mut R ) -> Result <$t, Error > {
459+ <$t as SampleUniform >:: Sampler :: sample_single_inclusive( 0 , self . end, rng)
460+ }
461+
462+ #[ inline]
463+ fn is_empty( & self ) -> bool {
464+ false
465+ }
466+ }
467+ } ;
468+ }
469+
470+ impl_sample_range_u ! ( u8 ) ;
471+ impl_sample_range_u ! ( u16 ) ;
472+ impl_sample_range_u ! ( u32 ) ;
473+ impl_sample_range_u ! ( u64 ) ;
474+ impl_sample_range_u ! ( u128 ) ;
475+ impl_sample_range_u ! ( usize ) ;
476+
442477#[ cfg( test) ]
443478mod tests {
444479 use super :: * ;
0 commit comments