@@ -92,6 +92,7 @@ pub mod seq;
92
92
93
93
94
94
use core:: { mem, slice} ;
95
+ use core:: num:: Wrapping ;
95
96
use distributions:: { Distribution , Standard } ;
96
97
use distributions:: uniform:: { SampleUniform , UniformSampler , SampleBorrow } ;
97
98
@@ -398,6 +399,7 @@ impl AsByteSliceMut for [u8] {
398
399
}
399
400
400
401
macro_rules! impl_as_byte_slice {
402
+ ( ) => { } ;
401
403
( $t: ty) => {
402
404
impl AsByteSliceMut for [ $t] {
403
405
fn as_byte_slice_mut( & mut self ) -> & mut [ u8 ] {
@@ -422,26 +424,47 @@ macro_rules! impl_as_byte_slice {
422
424
}
423
425
}
424
426
}
427
+
428
+ impl AsByteSliceMut for [ Wrapping <$t>] {
429
+ fn as_byte_slice_mut( & mut self ) -> & mut [ u8 ] {
430
+ if self . len( ) == 0 {
431
+ unsafe {
432
+ // must not use null pointer
433
+ slice:: from_raw_parts_mut( 0x1 as * mut u8 , 0 )
434
+ }
435
+ } else {
436
+ unsafe {
437
+ slice:: from_raw_parts_mut( self . as_mut_ptr( )
438
+ as * mut u8 ,
439
+ self . len( ) * mem:: size_of:: <$t>( )
440
+ )
441
+ }
442
+ }
443
+ }
444
+
445
+ fn to_le( & mut self ) {
446
+ for x in self {
447
+ * x = Wrapping ( x. 0 . to_le( ) ) ;
448
+ }
449
+ }
450
+ }
451
+ } ;
452
+ ( $t: ty, $( $tt: ty, ) * ) => {
453
+ impl_as_byte_slice!( $t) ;
454
+ // TODO: this could replace above impl once Rust #32463 is fixed
455
+ // impl_as_byte_slice!(Wrapping<$t>);
456
+ impl_as_byte_slice!( $( $tt, ) * ) ;
425
457
}
426
458
}
427
459
428
- impl_as_byte_slice ! ( u16 ) ;
429
- impl_as_byte_slice ! ( u32 ) ;
430
- impl_as_byte_slice ! ( u64 ) ;
460
+ impl_as_byte_slice ! ( u16 , u32 , u64 , usize , ) ;
431
461
#[ cfg( all( rustc_1_26, not( target_os = "emscripten" ) ) ) ] impl_as_byte_slice ! ( u128 ) ;
432
- impl_as_byte_slice ! ( usize ) ;
433
- impl_as_byte_slice ! ( i8 ) ;
434
- impl_as_byte_slice ! ( i16 ) ;
435
- impl_as_byte_slice ! ( i32 ) ;
436
- impl_as_byte_slice ! ( i64 ) ;
462
+ impl_as_byte_slice ! ( i8 , i16 , i32 , i64 , isize , ) ;
437
463
#[ cfg( all( rustc_1_26, not( target_os = "emscripten" ) ) ) ] impl_as_byte_slice ! ( i128 ) ;
438
- impl_as_byte_slice ! ( isize ) ;
439
464
440
465
macro_rules! impl_as_byte_slice_arrays {
441
466
( $n: expr, ) => { } ;
442
- ( $n: expr, $N: ident, $( $NN: ident, ) * ) => {
443
- impl_as_byte_slice_arrays!( $n - 1 , $( $NN, ) * ) ;
444
-
467
+ ( $n: expr, $N: ident) => {
445
468
impl <T > AsByteSliceMut for [ T ; $n] where [ T ] : AsByteSliceMut {
446
469
fn as_byte_slice_mut( & mut self ) -> & mut [ u8 ] {
447
470
self [ ..] . as_byte_slice_mut( )
@@ -452,25 +475,19 @@ macro_rules! impl_as_byte_slice_arrays {
452
475
}
453
476
}
454
477
} ;
478
+ ( $n: expr, $N: ident, $( $NN: ident, ) * ) => {
479
+ impl_as_byte_slice_arrays!( $n, $N) ;
480
+ impl_as_byte_slice_arrays!( $n - 1 , $( $NN, ) * ) ;
481
+ } ;
455
482
( !div $n: expr, ) => { } ;
456
483
( !div $n: expr, $N: ident, $( $NN: ident, ) * ) => {
484
+ impl_as_byte_slice_arrays!( $n, $N) ;
457
485
impl_as_byte_slice_arrays!( !div $n / 2 , $( $NN, ) * ) ;
458
-
459
- impl <T > AsByteSliceMut for [ T ; $n] where [ T ] : AsByteSliceMut {
460
- fn as_byte_slice_mut( & mut self ) -> & mut [ u8 ] {
461
- self [ ..] . as_byte_slice_mut( )
462
- }
463
-
464
- fn to_le( & mut self ) {
465
- self [ ..] . to_le( )
466
- }
467
- }
468
486
} ;
469
487
}
470
488
impl_as_byte_slice_arrays ! ( 32 , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , N , ) ;
471
489
impl_as_byte_slice_arrays ! ( !div 4096 , N , N , N , N , N , N , N , ) ;
472
490
473
-
474
491
/// Generates a random value using the thread-local random number generator.
475
492
///
476
493
/// This is simply a shortcut for `thread_rng().gen()`. See [`thread_rng`] for
@@ -568,6 +585,12 @@ mod test {
568
585
rng. fill ( & mut array[ ..] ) ;
569
586
assert_eq ! ( array, [ x as u32 , ( x >> 32 ) as u32 ] ) ;
570
587
assert_eq ! ( rng. next_u32( ) , x as u32 ) ;
588
+
589
+ // Check equivalence using wrapped arrays
590
+ let mut warray = [ Wrapping ( 0u32 ) ; 2 ] ;
591
+ rng. fill ( & mut warray[ ..] ) ;
592
+ assert_eq ! ( array[ 0 ] , warray[ 0 ] . 0 ) ;
593
+ assert_eq ! ( array[ 1 ] , warray[ 1 ] . 0 ) ;
571
594
}
572
595
573
596
#[ test]
0 commit comments