Skip to content

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a version that uses fill_bytes().

Suggested change
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)
}

Copy link
Member

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.

Copy link

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.

}
}

trait SpecArrayClone: Clone {
fn clone<const N: usize>(array: &[Self; N]) -> [Self; N];
}
Expand Down
Loading