Skip to content

Commit d2eb51b

Browse files
authored
Remove some unsafe, update to zerocopy 0.8.0 (#1502)
1 parent bc33411 commit d2eb51b

File tree

4 files changed

+7
-17
lines changed

4 files changed

+7
-17
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" }

rand_core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ serde = ["dep:serde"] # enables serde for BlockRng wrapper
3232
[dependencies]
3333
serde = { version = "1", features = ["derive"], optional = true }
3434
getrandom = { version = "0.2", optional = true }
35-
zerocopy = { version = "0.7.33", default-features = false }
35+
zerocopy = { version = "0.8.0", default-features = false }

rand_core/src/impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
use crate::RngCore;
2121
use core::cmp::min;
22-
use zerocopy::AsBytes;
22+
use zerocopy::{Immutable, IntoBytes};
2323

2424
/// Implement `next_u64` via `next_u32`, little-endian order.
2525
pub fn next_u64_via_u32<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
@@ -53,7 +53,7 @@ pub fn fill_bytes_via_next<R: RngCore + ?Sized>(rng: &mut R, dest: &mut [u8]) {
5353
}
5454
}
5555

56-
trait Observable: AsBytes + Copy {
56+
trait Observable: IntoBytes + Immutable + Copy {
5757
fn to_le(self) -> Self;
5858
}
5959
impl Observable for u32 {

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)