@@ -42,7 +42,7 @@ use distributions::float::IntoFloat;
42
42
/// use rand::distributions::{Distribution, Range};
43
43
///
44
44
/// fn main() {
45
- /// let between = Range::new (10, 10000);
45
+ /// let between = Range::from (10.. 10000);
46
46
/// let mut rng = rand::thread_rng();
47
47
/// let mut sum = 0;
48
48
/// for _ in 0..1000 {
@@ -173,6 +173,9 @@ pub trait RangeImpl: Sized {
173
173
}
174
174
175
175
/// Implementation of `RangeImpl` for integer types.
176
+ ///
177
+ /// Unless you are implementing `RangeImpl` for your own type, this type should
178
+ /// not be used directly, use `Range` instead.
176
179
#[ derive( Clone , Copy , Debug ) ]
177
180
pub struct RangeInt < X > {
178
181
low : X ,
@@ -317,6 +320,12 @@ macro_rules! range_int_impl {
317
320
}
318
321
}
319
322
323
+ impl < X : SampleRange > From < :: core:: ops:: Range < X > > for Range < X > {
324
+ fn from ( r : :: core:: ops:: Range < X > ) -> Range < X > {
325
+ Range :: new ( r. start , r. end )
326
+ }
327
+ }
328
+
320
329
range_int_impl ! { i8 , i8 , u8 , i32 , u32 }
321
330
range_int_impl ! { i16 , i16 , u16 , i32 , u32 }
322
331
range_int_impl ! { i32 , i32 , u32 , i32 , u32 }
@@ -420,6 +429,9 @@ wmul_impl_usize! { u64 }
420
429
421
430
422
431
/// Implementation of `RangeImpl` for float types.
432
+ ///
433
+ /// Unless you are implementing `RangeImpl` for your own type, this type should
434
+ /// not be used directly, use `Range` instead.
423
435
#[ derive( Clone , Copy , Debug ) ]
424
436
pub struct RangeFloat < X > {
425
437
scale : X ,
@@ -454,8 +466,11 @@ macro_rules! range_float_impl {
454
466
// Generate a value in the range [1, 2)
455
467
let value1_2 = ( rng. $next_u( ) >> $bits_to_discard)
456
468
. into_float_with_exponent( 0 ) ;
457
- // Doing multiply before addition allows some architectures to
458
- // use a single instruction.
469
+ // We don't use `f64::mul_add`, because it is not available with
470
+ // `no_std`. Furthermore, it is slower for some targets (but
471
+ // faster for others). However, the order of multiplication and
472
+ // addition is important, because on some platforms (e.g. ARM)
473
+ // it will be optimized to a single (non-FMA) instruction.
459
474
value1_2 * self . scale + self . offset
460
475
}
461
476
}
@@ -469,7 +484,7 @@ range_float_impl! { f64, 64 - 52, next_u64 }
469
484
#[ cfg( test) ]
470
485
mod tests {
471
486
use Rng ;
472
- use distributions:: range:: { Range , RangeImpl , RangeFloat , SampleRange } ;
487
+ use distributions:: range:: { Range , RangeImpl , RangeInt , RangeFloat , SampleRange } ;
473
488
474
489
#[ should_panic]
475
490
#[ test]
@@ -578,4 +593,14 @@ mod tests {
578
593
assert ! ( low <= x && x < high) ;
579
594
}
580
595
}
596
+
597
+ #[ test]
598
+ fn test_range_from_std_range ( ) {
599
+ let r = Range :: from ( 2u32 ..7 ) ;
600
+ assert_eq ! ( r. inner. low, 2 ) ;
601
+ assert_eq ! ( r. inner. range, 5 ) ;
602
+ let r = Range :: from ( 2.0f64 ..7.0 ) ;
603
+ assert_eq ! ( r. inner. offset, -3.0 ) ;
604
+ assert_eq ! ( r. inner. scale, 5.0 ) ;
605
+ }
581
606
}
0 commit comments