rand_core: Replace transmutes with safe code
This commit is contained in:
parent
1c516ebd66
commit
183fadb81e
@ -17,7 +17,6 @@
|
||||
//! to/from byte sequences, and since its purpose is reproducibility,
|
||||
//! non-reproducible sources (e.g. `OsRng`) need not bother with it.
|
||||
|
||||
use core::intrinsics::transmute;
|
||||
use core::ptr::copy_nonoverlapping;
|
||||
use core::slice;
|
||||
use core::cmp::min;
|
||||
@ -44,21 +43,15 @@ pub fn fill_bytes_via_next<R: RngCore + ?Sized>(rng: &mut R, dest: &mut [u8]) {
|
||||
while left.len() >= 8 {
|
||||
let (l, r) = {left}.split_at_mut(8);
|
||||
left = r;
|
||||
let chunk: [u8; 8] = unsafe {
|
||||
transmute(rng.next_u64().to_le())
|
||||
};
|
||||
let chunk: [u8; 8] = rng.next_u64().to_le_bytes();
|
||||
l.copy_from_slice(&chunk);
|
||||
}
|
||||
let n = left.len();
|
||||
if n > 4 {
|
||||
let chunk: [u8; 8] = unsafe {
|
||||
transmute(rng.next_u64().to_le())
|
||||
};
|
||||
let chunk: [u8; 8] = rng.next_u64().to_le_bytes();
|
||||
left.copy_from_slice(&chunk[..n]);
|
||||
} else if n > 0 {
|
||||
let chunk: [u8; 4] = unsafe {
|
||||
transmute(rng.next_u32().to_le())
|
||||
};
|
||||
let chunk: [u8; 4] = rng.next_u32().to_le_bytes();
|
||||
left.copy_from_slice(&chunk[..n]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user