Skip to content

Commit

Permalink
Add doc to fill_via_chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Dec 6, 2022
1 parent f2b94b2 commit 0e7741f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rand_core/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,19 @@ impl Observable for u64 {
}
}

/// Fill dest from src
///
/// Returns `(n, byte_len)`. `src[..n]` is consumed (and possibly mutated),
/// `dest[..byte_len]` is filled. `src[n..]` and `dest[byte_len..]` are left
/// unaltered.
fn fill_via_chunks<T: Observable>(src: &mut [T], dest: &mut [u8]) -> (usize, usize) {
let size = core::mem::size_of::<T>();
let byte_len = min(src.len() * size, dest.len());
let num_chunks = (byte_len + size - 1) / size;

// Byte-swap for portability of results:
// Byte-swap for portability of results. This must happen before copying
// since the size of dest is not guaranteed to be a multiple of T or to be
// sufficiently aligned.
if cfg!(target_endian = "big") {
for x in &mut src[..num_chunks] {
*x = x.to_le();
Expand Down

0 comments on commit 0e7741f

Please sign in to comment.