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 all commits
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
51 changes: 51 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,56 @@ 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 = buf.as_bytes_mut().assume_init_mut();
source.fill_bytes(bytes);
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: you create a &mut [u8] and pass it to user code – but nothing guarantees that the user code won't try to read the slice, which would be undefined behaviour. My recommendation would be to use MaybeUninit::zeroed to create buf.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally this needs a trait bound like zerocopy::FromBytes or bytemuck::Pod, but we don't have that in std, so... the macro should be renamed to unsafe_impl_... since safety depends on the macro caller?

MaybeUninit::array_assume_init(buf)
}
}
}
};
}

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];
}
Expand Down
Loading