Skip to content

Commit 5216f9a

Browse files
committed
Remove some unsafe code
1 parent 9a02c81 commit 5216f9a

File tree

4 files changed

+7
-18
lines changed

4 files changed

+7
-18
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ alloc = ["rand_core/alloc"]
4343
getrandom = ["rand_core/getrandom"]
4444

4545
# Option (requires nightly Rust): experimental SIMD support
46-
simd_support = []
46+
simd_support = ["zerocopy/simd-nightly"]
4747

4848
# Option (enabled by default): enable StdRng
4949
std_rng = ["rand_chacha"]
@@ -69,6 +69,7 @@ rand_core = { path = "rand_core", version = "0.7.0" }
6969
log = { version = "0.4.4", optional = true }
7070
serde = { version = "1.0.103", features = ["derive"], optional = true }
7171
rand_chacha = { path = "rand_chacha", version = "0.4.0", default-features = false, optional = true }
72+
zerocopy = { version = "0.7.20", default-features = false, features = ["simd"] }
7273

7374
[target.'cfg(unix)'.dependencies]
7475
# Used for fork protection (reseeding.rs)

rand_core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ serde1 = ["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.20", default-features = false }

rand_core/src/impls.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
2020
use crate::RngCore;
2121
use core::cmp::min;
22+
use zerocopy::AsBytes;
2223

2324
/// Implement `next_u64` via `next_u32`, little-endian order.
2425
pub fn next_u64_via_u32<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
@@ -52,31 +53,18 @@ pub fn fill_bytes_via_next<R: RngCore + ?Sized>(rng: &mut R, dest: &mut [u8]) {
5253
}
5354
}
5455

55-
trait Observable: Copy {
56+
trait Observable: AsBytes + Copy {
5657
fn to_le(self) -> Self;
57-
58-
// Contract: observing self is memory-safe (implies no uninitialised padding)
59-
fn as_byte_slice(x: &[Self]) -> &[u8];
6058
}
6159
impl Observable for u32 {
6260
fn to_le(self) -> Self {
6361
self.to_le()
6462
}
65-
fn as_byte_slice(x: &[Self]) -> &[u8] {
66-
let ptr = x.as_ptr() as *const u8;
67-
let len = x.len() * core::mem::size_of::<Self>();
68-
unsafe { core::slice::from_raw_parts(ptr, len) }
69-
}
7063
}
7164
impl Observable for u64 {
7265
fn to_le(self) -> Self {
7366
self.to_le()
7467
}
75-
fn as_byte_slice(x: &[Self]) -> &[u8] {
76-
let ptr = x.as_ptr() as *const u8;
77-
let len = x.len() * core::mem::size_of::<Self>();
78-
unsafe { core::slice::from_raw_parts(ptr, len) }
79-
}
8068
}
8169

8270
/// Fill dest from src
@@ -98,7 +86,7 @@ fn fill_via_chunks<T: Observable>(src: &mut [T], dest: &mut [u8]) -> (usize, usi
9886
}
9987
}
10088

101-
dest[..byte_len].copy_from_slice(&T::as_byte_slice(&src[..num_chunks])[..byte_len]);
89+
dest[..byte_len].copy_from_slice(&<[T]>::as_bytes(&src[..num_chunks])[..byte_len]);
10290

10391
(num_chunks, byte_len)
10492
}

src/distributions/integer.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ macro_rules! x86_intrinsic_impl {
134134
let mut buf = [0_u8; mem::size_of::<$intrinsic>()];
135135
rng.fill_bytes(&mut buf);
136136
// x86 is little endian so no need for conversion
137-
// SAFETY: we know [u8; N] and $intrinsic have the same size
138-
unsafe { mem::transmute_copy(&buf) }
137+
zerocopy::transmute!(buf)
139138
}
140139
}
141140
)+};

0 commit comments

Comments
 (0)