-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement Random
for array
#136732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Implement Random
for array
#136732
Changes from 4 commits
f1580f3
80d776e
5435be8
b7d7fc6
b33af51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,8 @@ use crate::ops::{ | |
ChangeOutputType, ControlFlow, FromResidual, Index, IndexMut, NeverShortCircuit, Residual, Try, | ||
}; | ||
use crate::ptr::{null, null_mut}; | ||
use crate::slice::{Iter, IterMut}; | ||
use crate::random::{Random, RandomSource}; | ||
use crate::slice::{self, Iter, IterMut}; | ||
|
||
mod ascii; | ||
mod drain; | ||
|
@@ -426,6 +427,59 @@ impl<T: Clone, const N: usize> Clone for [T; N] { | |
} | ||
} | ||
|
||
#[unstable(feature = "random", issue = "130703")] | ||
impl<T: Random, const N: usize> Random for [T; N] { | ||
fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||
SpecArrayRandom::random(source) | ||
} | ||
} | ||
|
||
#[unstable(feature = "random", issue = "130703")] | ||
trait SpecArrayRandom: Sized { | ||
fn random<const N: usize>(source: &mut (impl RandomSource + ?Sized)) -> [Self; N]; | ||
} | ||
|
||
#[unstable(feature = "random", issue = "130703")] | ||
impl<T: Random> SpecArrayRandom for T { | ||
default fn random<const N: usize>(source: &mut (impl RandomSource + ?Sized)) -> [T; N] { | ||
from_fn(|_| T::random(source)) | ||
} | ||
} | ||
|
||
macro_rules! impl_random_for_integer_array { | ||
($t:ty) => { | ||
#[unstable(feature = "random", issue = "130703")] | ||
impl SpecArrayRandom for $t { | ||
fn random<const N: usize>(source: &mut (impl RandomSource + ?Sized)) -> [$t; N] { | ||
let mut buf = [const { MaybeUninit::<$t>::uninit() }; N]; | ||
// SAFETY: all elements in the buffer were initialized with | ||
// random bytes. | ||
unsafe { | ||
let bytes = slice::from_raw_parts_mut( | ||
&raw mut buf as *mut u8, | ||
N * (<$t>::BITS as usize / 8), | ||
); | ||
source.fill_bytes(bytes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unsound: you create a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally this needs a trait bound like |
||
mem::transmute_copy(&buf) | ||
joboet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_random_for_integer_array!(u8); | ||
impl_random_for_integer_array!(u16); | ||
impl_random_for_integer_array!(u32); | ||
impl_random_for_integer_array!(u64); | ||
impl_random_for_integer_array!(u128); | ||
impl_random_for_integer_array!(usize); | ||
impl_random_for_integer_array!(i8); | ||
impl_random_for_integer_array!(i16); | ||
impl_random_for_integer_array!(i32); | ||
impl_random_for_integer_array!(i64); | ||
impl_random_for_integer_array!(i128); | ||
impl_random_for_integer_array!(isize); | ||
|
||
trait SpecArrayClone: Clone { | ||
fn clone<const N: usize>(array: &[Self; N]) -> [Self; N]; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.