@@ -702,12 +702,19 @@ macro_rules! impl_as_byte_slice {
702
702
( $t: ty) => {
703
703
impl AsByteSliceMut for [ $t] {
704
704
fn as_byte_slice_mut( & mut self ) -> & mut [ u8 ] {
705
- unsafe {
706
- slice:: from_raw_parts_mut( & mut self [ 0 ]
707
- as * mut $t
708
- as * mut u8 ,
709
- self . len( ) * mem:: size_of:: <$t>( )
710
- )
705
+ if self . len( ) == 0 {
706
+ unsafe {
707
+ // must not use null pointer
708
+ slice:: from_raw_parts_mut( 0x1 as * mut u8 , 0 )
709
+ }
710
+ } else {
711
+ unsafe {
712
+ slice:: from_raw_parts_mut( & mut self [ 0 ]
713
+ as * mut $t
714
+ as * mut u8 ,
715
+ self . len( ) * mem:: size_of:: <$t>( )
716
+ )
717
+ }
711
718
}
712
719
}
713
720
@@ -762,7 +769,7 @@ macro_rules! impl_as_byte_slice_arrays {
762
769
}
763
770
} ;
764
771
}
765
- 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 , ) ;
772
+ 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 , ) ;
766
773
impl_as_byte_slice_arrays ! ( !div 4096 , N , N , N , N , N , N , N , ) ;
767
774
768
775
/// Iterator which will generate a stream of random items.
@@ -1061,6 +1068,14 @@ mod test {
1061
1068
assert_eq ! ( array, [ x as u32 , ( x >> 32 ) as u32 ] ) ;
1062
1069
assert_eq ! ( rng. next_u32( ) , x as u32 ) ;
1063
1070
}
1071
+
1072
+ #[ test]
1073
+ fn test_fill_empty ( ) {
1074
+ let mut array = [ 0u32 ; 0 ] ;
1075
+ let mut rng = StepRng :: new ( 0 , 1 ) ;
1076
+ rng. fill ( & mut array) ;
1077
+ rng. fill ( & mut array[ ..] ) ;
1078
+ }
1064
1079
1065
1080
#[ test]
1066
1081
fn test_gen_range ( ) {
0 commit comments