-
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 1 commit
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,6 +17,7 @@ use crate::ops::{ | |||||||||||||||||||||||||||
| ChangeOutputType, ControlFlow, FromResidual, Index, IndexMut, NeverShortCircuit, Residual, Try, | ||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||
| use crate::ptr::{null, null_mut}; | ||||||||||||||||||||||||||||
| use crate::random::{Random, RandomSource}; | ||||||||||||||||||||||||||||
| use crate::slice::{Iter, IterMut}; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| mod ascii; | ||||||||||||||||||||||||||||
|
|
@@ -426,6 +427,18 @@ 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 { | ||||||||||||||||||||||||||||
| let mut buf = [const { MaybeUninit::uninit() }; N]; | ||||||||||||||||||||||||||||
| for elem in &mut buf { | ||||||||||||||||||||||||||||
| elem.write(T::random(source)); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| // SAFETY: all elements of the array were initialized. | ||||||||||||||||||||||||||||
| unsafe { mem::transmute_copy(&buf) } | ||||||||||||||||||||||||||||
Urgau marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
| let mut buf = [const { MaybeUninit::uninit() }; N]; | |
| for elem in &mut buf { | |
| elem.write(T::random(source)); | |
| } | |
| // SAFETY: all elements of the array were initialized. | |
| unsafe { mem::transmute_copy(&buf) } | |
| let mut buf = [const { MaybeUninit::<T>::uninit() }; N]; | |
| // SAFETY: this initializes every element in the buffer with random bytes. | |
| unsafe { | |
| let slice = core::slice::from_raw_parts_mut(&raw mut buf as *mut u8, N * mem::size_of::<T>()); | |
| source.fill_bytes(slice); | |
| mem::transmute_copy(&buf) | |
| } |
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.
This is unsound for generic Ts. There's no guarantee that a Type's random impl shovels those bytes into their memory representation. A trivial example would be NonZero<u8> which could implement Random via rejection sampling. Setting it to 0 would be unsound.
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.
@the8472 I did notice that, so we would need to restrict this to types that can hold arbitrary bytes (which currently includes every type implementing Random except bool). Otherwise, use a slower custom implementation with rejection sampling or modulo.
Uh oh!
There was an error while loading. Please reload this page.