Skip to content

Commit 45a9ee8

Browse files
committed
Remove some unsafe, update to zerocopy 0.8.0
1 parent bc33411 commit 45a9ee8

File tree

2 files changed

+4
-14
lines changed

2 files changed

+4
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ rand_core = { path = "rand_core", version = "=0.9.0-alpha.1", default-features =
7070
log = { version = "0.4.4", optional = true }
7171
serde = { version = "1.0.103", features = ["derive"], optional = true }
7272
rand_chacha = { path = "rand_chacha", version = "=0.9.0-alpha.1", default-features = false, optional = true }
73-
zerocopy = { version = "0.7.33", default-features = false, features = ["simd"] }
73+
zerocopy = { version = "0.8.0", default-features = false, features = ["simd"] }
7474

7575
[dev-dependencies]
7676
rand_pcg = { path = "rand_pcg", version = "=0.9.0-alpha.1" }

src/rng.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use crate::distr::uniform::{SampleRange, SampleUniform};
1313
use crate::distr::{self, Distribution, Standard};
1414
use core::num::Wrapping;
15-
use core::{mem, slice};
1615
use rand_core::RngCore;
16+
use zerocopy::IntoBytes;
1717

1818
/// User-level interface for RNGs
1919
///
@@ -374,12 +374,7 @@ macro_rules! impl_fill {
374374
#[inline(never)] // in micro benchmarks, this improves performance
375375
fn fill<R: Rng + ?Sized>(&mut self, rng: &mut R) {
376376
if self.len() > 0 {
377-
rng.fill_bytes(unsafe {
378-
slice::from_raw_parts_mut(self.as_mut_ptr()
379-
as *mut u8,
380-
mem::size_of_val(self)
381-
)
382-
});
377+
rng.fill_bytes(self.as_mut_bytes());
383378
for x in self {
384379
*x = x.to_le();
385380
}
@@ -391,12 +386,7 @@ macro_rules! impl_fill {
391386
#[inline(never)]
392387
fn fill<R: Rng + ?Sized>(&mut self, rng: &mut R) {
393388
if self.len() > 0 {
394-
rng.fill_bytes(unsafe {
395-
slice::from_raw_parts_mut(self.as_mut_ptr()
396-
as *mut u8,
397-
self.len() * mem::size_of::<$t>()
398-
)
399-
});
389+
rng.fill_bytes(self.as_mut_bytes());
400390
for x in self {
401391
*x = Wrapping(x.0.to_le());
402392
}

0 commit comments

Comments
 (0)