Skip to content

Commit

Permalink
Remove some unsafe, update to zerocopy 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
joshlf committed Oct 4, 2024
1 parent bc33411 commit 893b108
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion rand_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
16 changes: 3 additions & 13 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down Expand Up @@ -374,12 +374,7 @@ macro_rules! impl_fill {
#[inline(never)] // in micro benchmarks, this improves performance
fn fill<R: Rng + ?Sized>(&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();
}
Expand All @@ -391,12 +386,7 @@ macro_rules! impl_fill {
#[inline(never)]
fn fill<R: Rng + ?Sized>(&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());
}
Expand Down

0 comments on commit 893b108

Please sign in to comment.