-
Notifications
You must be signed in to change notification settings - Fork 14k
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: main
Are you sure you want to change the base?
Implement Random for array
#136732
Changes from 3 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,47 @@ 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] { | ||
| default fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||
| from_fn(|_| T::random(source)) | ||
| } | ||
| } | ||
|
|
||
| macro_rules! impl_random_for_integer_array { | ||
| ($t:ty) => { | ||
| #[unstable(feature = "random", issue = "130703")] | ||
| impl<const N: usize> Random for [$t; N] { | ||
| fn random(source: &mut (impl RandomSource + ?Sized)) -> Self { | ||
| 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), | ||
| ); | ||
joboet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| source.fill_bytes(bytes); | ||
|
Member
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
Contributor
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.
Outdated
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]; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specializations should be hidden behind a private trait. https://std-dev-guide.rust-lang.org/policy/specialization.html
Alternatively you can avoid specialization by implementing a buffering random source wrapper as mentioned in #136732 (comment)