Skip to content

Commit

Permalink
Small improvements to Rng::fill
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed Sep 11, 2022
1 parent 5e87fd0 commit 0d24fa0
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

use std::cell::Cell;
use std::collections::hash_map::DefaultHasher;
use std::convert::TryInto;
use std::hash::{Hash, Hasher};
use std::ops::{Bound, RangeBounds};
use std::thread;
Expand Down Expand Up @@ -446,9 +447,11 @@ impl Rng {
pub fn fill(&self, slice: &mut [u8]) {
// Filling the buffer in chunks of 8 is much faster.
let mut chunks = slice.chunks_exact_mut(8);
for items in chunks.by_ref() {
let r = self.u64(..);
items.copy_from_slice(&r.to_le_bytes());
for chunk in chunks.by_ref() {
let n = self.gen_u64();
// Safe because the chunks are always 8 bytes exactly.
let bytes: &mut [u8; 8] = chunk.try_into().unwrap();
*bytes = n.to_ne_bytes();
}

for item in chunks.into_remainder() {
Expand Down Expand Up @@ -543,7 +546,7 @@ impl Rng {
/// Panics if the range is empty.
#[inline]
pub fn char(&self, range: impl RangeBounds<char>) -> char {
use std::convert::{TryFrom, TryInto};
use std::convert::TryFrom;

let panic_empty_range = || {
panic!(
Expand Down

0 comments on commit 0d24fa0

Please sign in to comment.