@@ -110,7 +110,6 @@ mod other;
110110pub use other:: { UniformChar , UniformDuration } ;
111111
112112use core:: fmt;
113- use core:: ops:: { Range , RangeInclusive , RangeTo , RangeToInclusive } ;
114113
115114use crate :: Rng ;
116115use crate :: distr:: Distribution ;
@@ -371,22 +370,40 @@ pub trait UniformSampler: Sized {
371370 }
372371}
373372
374- impl < X : SampleUniform > TryFrom < Range < X > > for Uniform < X > {
373+ impl < X : SampleUniform > TryFrom < :: core :: ops :: Range < X > > for Uniform < X > {
375374 type Error = Error ;
376375
377- fn try_from ( r : Range < X > ) -> Result < Uniform < X > , Error > {
376+ fn try_from ( r : :: core :: ops :: Range < X > ) -> Result < Uniform < X > , Error > {
378377 Uniform :: new ( r. start , r. end )
379378 }
380379}
381380
382- impl < X : SampleUniform > TryFrom < RangeInclusive < X > > for Uniform < X > {
381+ #[ cfg( feature = "new_range_api" ) ]
382+ impl < X : SampleUniform > TryFrom < :: core:: range:: Range < X > > for Uniform < X > {
383+ type Error = Error ;
384+
385+ fn try_from ( r : :: core:: range:: Range < X > ) -> Result < Uniform < X > , Error > {
386+ Uniform :: new ( r. start , r. end )
387+ }
388+ }
389+
390+ impl < X : SampleUniform > TryFrom < :: core:: ops:: RangeInclusive < X > > for Uniform < X > {
383391 type Error = Error ;
384392
385393 fn try_from ( r : :: core:: ops:: RangeInclusive < X > ) -> Result < Uniform < X > , Error > {
386394 Uniform :: new_inclusive ( r. start ( ) , r. end ( ) )
387395 }
388396}
389397
398+ #[ cfg( feature = "new_range_api" ) ]
399+ impl < X : SampleUniform > TryFrom < :: core:: range:: RangeInclusive < X > > for Uniform < X > {
400+ type Error = Error ;
401+
402+ fn try_from ( r : :: core:: range:: RangeInclusive < X > ) -> Result < Uniform < X > , Error > {
403+ Uniform :: new_inclusive ( r. start , r. last )
404+ }
405+ }
406+
390407/// Helper trait similar to [`Borrow`] but implemented
391408/// only for [`SampleUniform`] and references to [`SampleUniform`]
392409/// in order to resolve ambiguity issues.
@@ -429,7 +446,20 @@ pub trait SampleRange<T> {
429446 fn is_empty ( & self ) -> bool ;
430447}
431448
432- impl < T : SampleUniform + PartialOrd > SampleRange < T > for Range < T > {
449+ impl < T : SampleUniform + PartialOrd > SampleRange < T > for :: core:: ops:: Range < T > {
450+ #[ inline]
451+ fn sample_single < R : Rng + ?Sized > ( self , rng : & mut R ) -> Result < T , Error > {
452+ T :: Sampler :: sample_single ( self . start , self . end , rng)
453+ }
454+
455+ #[ inline]
456+ fn is_empty ( & self ) -> bool {
457+ !( self . start < self . end )
458+ }
459+ }
460+
461+ #[ cfg( feature = "new_range_api" ) ]
462+ impl < T : SampleUniform + PartialOrd > SampleRange < T > for :: core:: range:: Range < T > {
433463 #[ inline]
434464 fn sample_single < R : Rng + ?Sized > ( self , rng : & mut R ) -> Result < T , Error > {
435465 T :: Sampler :: sample_single ( self . start , self . end , rng)
@@ -441,7 +471,7 @@ impl<T: SampleUniform + PartialOrd> SampleRange<T> for Range<T> {
441471 }
442472}
443473
444- impl < T : SampleUniform + PartialOrd > SampleRange < T > for RangeInclusive < T > {
474+ impl < T : SampleUniform + PartialOrd > SampleRange < T > for :: core :: ops :: RangeInclusive < T > {
445475 #[ inline]
446476 fn sample_single < R : Rng + ?Sized > ( self , rng : & mut R ) -> Result < T , Error > {
447477 T :: Sampler :: sample_single_inclusive ( self . start ( ) , self . end ( ) , rng)
@@ -453,9 +483,22 @@ impl<T: SampleUniform + PartialOrd> SampleRange<T> for RangeInclusive<T> {
453483 }
454484}
455485
486+ #[ cfg( feature = "new_range_api" ) ]
487+ impl < T : SampleUniform + PartialOrd > SampleRange < T > for :: core:: range:: RangeInclusive < T > {
488+ #[ inline]
489+ fn sample_single < R : Rng + ?Sized > ( self , rng : & mut R ) -> Result < T , Error > {
490+ T :: Sampler :: sample_single_inclusive ( self . start , self . last , rng)
491+ }
492+
493+ #[ inline]
494+ fn is_empty ( & self ) -> bool {
495+ !( self . start <= self . last )
496+ }
497+ }
498+
456499macro_rules! impl_sample_range_u {
457500 ( $t: ty) => {
458- impl SampleRange <$t> for RangeTo <$t> {
501+ impl SampleRange <$t> for :: core :: ops :: RangeTo <$t> {
459502 #[ inline]
460503 fn sample_single<R : Rng + ?Sized >( self , rng: & mut R ) -> Result <$t, Error > {
461504 <$t as SampleUniform >:: Sampler :: sample_single( 0 , self . end, rng)
@@ -467,7 +510,7 @@ macro_rules! impl_sample_range_u {
467510 }
468511 }
469512
470- impl SampleRange <$t> for RangeToInclusive <$t> {
513+ impl SampleRange <$t> for :: core :: ops :: RangeToInclusive <$t> {
471514 #[ inline]
472515 fn sample_single<R : Rng + ?Sized >( self , rng: & mut R ) -> Result <$t, Error > {
473516 <$t as SampleUniform >:: Sampler :: sample_single_inclusive( 0 , self . end, rng)
@@ -478,6 +521,24 @@ macro_rules! impl_sample_range_u {
478521 false
479522 }
480523 }
524+
525+ // `core::range::RangeTo` is set to be a re-export of `core::ops::RangeTo`:
526+ // > A Rust version in the near future will also add `core::range::RangeFull` and `core::range::RangeTo`
527+ // as re-exports from `core::ops` (these do not implement `Iterator` and already implement `Copy`)
528+ // Source: https://blog.rust-lang.org/2026/05/28/Rust-1.96.0/#new-range-types
529+
530+ #[ cfg( feature = "new_range_api" ) ]
531+ impl SampleRange <$t> for :: core:: range:: RangeToInclusive <$t> {
532+ #[ inline]
533+ fn sample_single<R : Rng + ?Sized >( self , rng: & mut R ) -> Result <$t, Error > {
534+ <$t as SampleUniform >:: Sampler :: sample_single_inclusive( 0 , self . last, rng)
535+ }
536+
537+ #[ inline]
538+ fn is_empty( & self ) -> bool {
539+ false
540+ }
541+ }
481542 } ;
482543}
483544
0 commit comments