@@ -18,7 +18,7 @@ use crate::ops::{
18
18
} ;
19
19
use crate :: ptr:: { null, null_mut} ;
20
20
use crate :: random:: { Random , RandomSource } ;
21
- use crate :: slice:: { Iter , IterMut } ;
21
+ use crate :: slice:: { self , Iter , IterMut } ;
22
22
23
23
mod ascii;
24
24
mod drain;
@@ -429,11 +429,45 @@ impl<T: Clone, const N: usize> Clone for [T; N] {
429
429
430
430
#[ unstable( feature = "random" , issue = "130703" ) ]
431
431
impl < T : Random , const N : usize > Random for [ T ; N ] {
432
- fn random ( source : & mut ( impl RandomSource + ?Sized ) ) -> Self {
432
+ default fn random ( source : & mut ( impl RandomSource + ?Sized ) ) -> Self {
433
433
from_fn ( |_| T :: random ( source) )
434
434
}
435
435
}
436
436
437
+ macro_rules! impl_random_for_integer_array {
438
+ ( $t: ty) => {
439
+ #[ unstable( feature = "random" , issue = "130703" ) ]
440
+ impl <const N : usize > Random for [ $t; N ] {
441
+ fn random( source: & mut ( impl RandomSource + ?Sized ) ) -> Self {
442
+ let mut buf = [ const { MaybeUninit :: <$t>:: uninit( ) } ; N ] ;
443
+ // SAFETY: all elements in the buffer were initialized with
444
+ // random bytes.
445
+ unsafe {
446
+ let bytes = slice:: from_raw_parts_mut(
447
+ & raw mut buf as * mut u8 ,
448
+ N * mem:: size_of:: <$t>( ) ,
449
+ ) ;
450
+ source. fill_bytes( bytes) ;
451
+ mem:: transmute_copy( & buf)
452
+ }
453
+ }
454
+ }
455
+ } ;
456
+ }
457
+
458
+ impl_random_for_integer_array ! ( u8 ) ;
459
+ impl_random_for_integer_array ! ( u16 ) ;
460
+ impl_random_for_integer_array ! ( u32 ) ;
461
+ impl_random_for_integer_array ! ( u64 ) ;
462
+ impl_random_for_integer_array ! ( u128 ) ;
463
+ impl_random_for_integer_array ! ( usize ) ;
464
+ impl_random_for_integer_array ! ( i8 ) ;
465
+ impl_random_for_integer_array ! ( i16 ) ;
466
+ impl_random_for_integer_array ! ( i32 ) ;
467
+ impl_random_for_integer_array ! ( i64 ) ;
468
+ impl_random_for_integer_array ! ( i128 ) ;
469
+ impl_random_for_integer_array ! ( isize ) ;
470
+
437
471
trait SpecArrayClone : Clone {
438
472
fn clone < const N : usize > ( array : & [ Self ; N ] ) -> [ Self ; N ] ;
439
473
}
0 commit comments