From 4a035a469e2d6bf76a351b2f21ed5c49c3796963 Mon Sep 17 00:00:00 2001 From: Shun Sakai Date: Sun, 9 Feb 2025 13:13:57 +0900 Subject: [PATCH] Change to use `array::from_fn` Change `Distribution<[T; N]> for StandardUniform` to use `array::from_fn` to create an array. This reduces the `unsafe` code. --- src/distr/other.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/distr/other.rs b/src/distr/other.rs index 9890bdafe6..15a61a51e7 100644 --- a/src/distr/other.rs +++ b/src/distr/other.rs @@ -10,6 +10,7 @@ #[cfg(feature = "alloc")] use alloc::string::String; +use core::array; use core::char; use core::num::Wrapping; @@ -18,7 +19,6 @@ use crate::distr::SampleString; use crate::distr::{Distribution, StandardUniform, Uniform}; use crate::Rng; -use core::mem::{self, MaybeUninit}; #[cfg(feature = "simd_support")] use core::simd::prelude::*; #[cfg(feature = "simd_support")] @@ -238,13 +238,7 @@ where { #[inline] fn sample(&self, _rng: &mut R) -> [T; N] { - let mut buff: [MaybeUninit; N] = unsafe { MaybeUninit::uninit().assume_init() }; - - for elem in &mut buff { - *elem = MaybeUninit::new(_rng.random()); - } - - unsafe { mem::transmute_copy::<_, _>(&buff) } + array::from_fn(|_| _rng.random()) } }