Skip to content
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

Use const-generics to support arrays of all sizes #1104

Merged
merged 6 commits into from Mar 25, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/distributions/other.rs
Expand Up @@ -16,6 +16,9 @@ use crate::Rng;

#[cfg(feature = "serde1")]
use serde::{Serialize, Deserialize};
#[cfg(feature = "min_const_gen")]
use std::mem::{self, MaybeUninit};


// ----- Sampling distributions -----

Expand Down Expand Up @@ -158,18 +161,17 @@ tuple_impl! {A, B, C, D, E, F, G, H, I, J, K, L}

#[cfg(feature = "min_const_gen")]
impl<T, const N: usize> Distribution<[T; N]> for Standard
where
Standard: Distribution<T>,
T: Default + Copy,
where Standard: Distribution<T>
{
#[inline]
fn sample<R: Rng + ?Sized>(&self, _rng: &mut R) -> [T; N] {
let mut sample = [Default::default(); N];
for elem in &mut sample {
*elem = _rng.gen();
let mut buff: [MaybeUninit<T>; N] = unsafe { MaybeUninit::uninit().assume_init() };
noslaver marked this conversation as resolved.
Show resolved Hide resolved

for elem in &mut buff {
*elem = MaybeUninit::new(_rng.gen());
noslaver marked this conversation as resolved.
Show resolved Hide resolved
}

sample
unsafe { mem::transmute_copy::<_, _>(&buff) }
}
}

Expand Down