@@ -37,35 +37,34 @@ pub fn next_u64_via_u32<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
37
37
( y << 32 ) | x
38
38
}
39
39
40
- macro_rules! fill_bytes_via {
41
- ( $rng: ident, $next_u: ident, $BYTES: expr, $dest: ident) => { {
42
- let mut left = $dest;
43
- while left. len( ) >= $BYTES {
44
- let ( l, r) = { left} . split_at_mut( $BYTES) ;
45
- left = r;
46
- let chunk: [ u8 ; $BYTES] = unsafe {
47
- transmute( $rng. $next_u( ) . to_le( ) )
48
- } ;
49
- l. copy_from_slice( & chunk) ;
50
- }
51
- let n = left. len( ) ;
52
- if n > 0 {
53
- let chunk: [ u8 ; $BYTES] = unsafe {
54
- transmute( $rng. $next_u( ) . to_le( ) )
55
- } ;
56
- left. copy_from_slice( & chunk[ ..n] ) ;
57
- }
58
- } }
59
- }
60
-
61
- /// Implement `fill_bytes` via `next_u32`, little-endian order.
62
- pub fn fill_bytes_via_u32 < R : RngCore + ?Sized > ( rng : & mut R , dest : & mut [ u8 ] ) {
63
- fill_bytes_via ! ( rng, next_u32, 4 , dest)
64
- }
65
-
66
- /// Implement `fill_bytes` via `next_u64`, little-endian order.
67
- pub fn fill_bytes_via_u64 < R : RngCore + ?Sized > ( rng : & mut R , dest : & mut [ u8 ] ) {
68
- fill_bytes_via ! ( rng, next_u64, 8 , dest)
40
+ /// Implement `fill_bytes` via `next_u64` and `next_u32`, little-endian order.
41
+ ///
42
+ /// The fastest way to fill a slice is usually to work as long as possible with
43
+ /// integers. That is why this method mostly uses `next_u64`, and only when
44
+ /// there are 4 or less bytes remaining at the end of the slice it uses
45
+ /// `next_u32` once.
46
+ pub fn fill_bytes_via_next < R : RngCore + ?Sized > ( rng : & mut R , dest : & mut [ u8 ] ) {
47
+ let mut left = dest;
48
+ while left. len ( ) >= 8 {
49
+ let ( l, r) = { left} . split_at_mut ( 8 ) ;
50
+ left = r;
51
+ let chunk: [ u8 ; 8 ] = unsafe {
52
+ transmute ( rng. next_u64 ( ) . to_le ( ) )
53
+ } ;
54
+ l. copy_from_slice ( & chunk) ;
55
+ }
56
+ let n = left. len ( ) ;
57
+ if n >= 4 {
58
+ let chunk: [ u8 ; 8 ] = unsafe {
59
+ transmute ( rng. next_u64 ( ) . to_le ( ) )
60
+ } ;
61
+ left. copy_from_slice ( & chunk[ ..n] ) ;
62
+ } else if n > 0 {
63
+ let chunk: [ u8 ; 4 ] = unsafe {
64
+ transmute ( rng. next_u32 ( ) . to_le ( ) )
65
+ } ;
66
+ left. copy_from_slice ( & chunk[ ..n] ) ;
67
+ }
69
68
}
70
69
71
70
macro_rules! impl_uint_from_fill {
0 commit comments