From d2eb51bc291d555cc559d337b34ec0c502927397 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Mon, 7 Oct 2024 01:29:52 -0700 Subject: [PATCH] Remove some unsafe, update to zerocopy 0.8.0 (#1502) --- Cargo.toml | 2 +- rand_core/Cargo.toml | 2 +- rand_core/src/impls.rs | 4 ++-- src/rng.rs | 16 +++------------- 4 files changed, 7 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9924c00723..2e860f7ffe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,7 @@ rand_core = { path = "rand_core", version = "=0.9.0-alpha.1", default-features = log = { version = "0.4.4", optional = true } serde = { version = "1.0.103", features = ["derive"], optional = true } rand_chacha = { path = "rand_chacha", version = "=0.9.0-alpha.1", default-features = false, optional = true } -zerocopy = { version = "0.7.33", default-features = false, features = ["simd"] } +zerocopy = { version = "0.8.0", default-features = false, features = ["simd"] } [dev-dependencies] rand_pcg = { path = "rand_pcg", version = "=0.9.0-alpha.1" } diff --git a/rand_core/Cargo.toml b/rand_core/Cargo.toml index dc4084d69a..5d7ec72c4c 100644 --- a/rand_core/Cargo.toml +++ b/rand_core/Cargo.toml @@ -32,4 +32,4 @@ serde = ["dep:serde"] # enables serde for BlockRng wrapper [dependencies] serde = { version = "1", features = ["derive"], optional = true } getrandom = { version = "0.2", optional = true } -zerocopy = { version = "0.7.33", default-features = false } +zerocopy = { version = "0.8.0", default-features = false } diff --git a/rand_core/src/impls.rs b/rand_core/src/impls.rs index 34230e1582..267049c787 100644 --- a/rand_core/src/impls.rs +++ b/rand_core/src/impls.rs @@ -19,7 +19,7 @@ use crate::RngCore; use core::cmp::min; -use zerocopy::AsBytes; +use zerocopy::{Immutable, IntoBytes}; /// Implement `next_u64` via `next_u32`, little-endian order. pub fn next_u64_via_u32(rng: &mut R) -> u64 { @@ -53,7 +53,7 @@ pub fn fill_bytes_via_next(rng: &mut R, dest: &mut [u8]) { } } -trait Observable: AsBytes + Copy { +trait Observable: IntoBytes + Immutable + Copy { fn to_le(self) -> Self; } impl Observable for u32 { diff --git a/src/rng.rs b/src/rng.rs index 7c9e887a2d..45361a00ab 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -12,8 +12,8 @@ use crate::distr::uniform::{SampleRange, SampleUniform}; use crate::distr::{self, Distribution, Standard}; use core::num::Wrapping; -use core::{mem, slice}; use rand_core::RngCore; +use zerocopy::IntoBytes; /// User-level interface for RNGs /// @@ -374,12 +374,7 @@ macro_rules! impl_fill { #[inline(never)] // in micro benchmarks, this improves performance fn fill(&mut self, rng: &mut R) { if self.len() > 0 { - rng.fill_bytes(unsafe { - slice::from_raw_parts_mut(self.as_mut_ptr() - as *mut u8, - mem::size_of_val(self) - ) - }); + rng.fill_bytes(self.as_mut_bytes()); for x in self { *x = x.to_le(); } @@ -391,12 +386,7 @@ macro_rules! impl_fill { #[inline(never)] fn fill(&mut self, rng: &mut R) { if self.len() > 0 { - rng.fill_bytes(unsafe { - slice::from_raw_parts_mut(self.as_mut_ptr() - as *mut u8, - self.len() * mem::size_of::<$t>() - ) - }); + rng.fill_bytes(self.as_mut_bytes()); for x in self { *x = Wrapping(x.0.to_le()); }