|
| 1 | +use crate::rand::Rng; |
| 2 | +use alloc::vec::Vec; |
| 3 | +use core::{ |
| 4 | + default::Default, |
| 5 | + ops::{Deref, DerefMut}, |
| 6 | +}; |
| 7 | + |
| 8 | +/// A buffered wrapper for any [Rng] implementation. |
| 9 | +/// It will keep unused bytes from the last call to [`Rng::rand`], and use them |
| 10 | +/// for subsequent randomness if needed, rather than throwing them away. |
| 11 | +/// |
| 12 | +/// ```rust |
| 13 | +/// use nanorand::{Rng, BufferedRng, WyRand}; |
| 14 | +/// |
| 15 | +/// let mut thingy = [0u8; 5]; |
| 16 | +/// let mut rng = BufferedRng::new(WyRand::new()); |
| 17 | +/// rng.fill(&mut thingy); |
| 18 | +/// // As WyRand generates 8 bytes of output, and our target is only 5 bytes, |
| 19 | +/// // 3 bytes will remain in the buffer. |
| 20 | +/// assert_eq!(rng.buffered(), 3); |
| 21 | +/// ``` |
| 22 | +#[derive(Clone)] |
| 23 | +pub struct BufferedRng<const OUTPUT: usize, R: Rng<OUTPUT>> { |
| 24 | + rng: R, |
| 25 | + buffer: Vec<u8>, |
| 26 | +} |
| 27 | + |
| 28 | +impl<const OUTPUT: usize, R: Rng<OUTPUT>> BufferedRng<OUTPUT, R> { |
| 29 | + /// Wraps a [`Rng`] generator in a [`BufferedRng`] instance. |
| 30 | + pub fn new(rng: R) -> Self { |
| 31 | + Self { |
| 32 | + rng, |
| 33 | + buffer: Vec::new(), |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Returns how many unused bytes are currently buffered. |
| 38 | + pub fn buffered(&self) -> usize { |
| 39 | + self.buffer.len() |
| 40 | + } |
| 41 | + |
| 42 | + /// Fills the output with random bytes, first using bytes from the internal buffer, |
| 43 | + /// and then using the [`Rng`] generator, and discarding unused bytes into the buffer. |
| 44 | + pub fn fill(&mut self, output: &mut [u8]) { |
| 45 | + let mut remaining = output.len(); |
| 46 | + while remaining > 0 { |
| 47 | + if self.buffer.is_empty() { |
| 48 | + self.buffer.extend_from_slice(&self.rng.rand()); |
| 49 | + } |
| 50 | + let to_copy = core::cmp::min(remaining, self.buffer.len()); |
| 51 | + let output_len = output.len(); |
| 52 | + let start_idx = output_len - remaining; |
| 53 | + output[start_idx..start_idx + to_copy].copy_from_slice(&self.buffer[..to_copy]); |
| 54 | + self.buffer.drain(..to_copy); |
| 55 | + remaining = remaining.saturating_sub(to_copy); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(feature = "std")] |
| 61 | +impl<const OUTPUT: usize, R: Rng<OUTPUT>> std::io::Read for BufferedRng<OUTPUT, R> { |
| 62 | + fn read(&mut self, output: &mut [u8]) -> std::io::Result<usize> { |
| 63 | + self.fill(output); |
| 64 | + Ok(output.len()) |
| 65 | + } |
| 66 | + |
| 67 | + fn read_to_end(&mut self, buf: &mut Vec<u8>) -> std::io::Result<usize> { |
| 68 | + buf.extend_from_slice(&self.buffer); |
| 69 | + Ok(self.buffer.drain(..).count()) |
| 70 | + } |
| 71 | + |
| 72 | + fn read_to_string(&mut self, _buf: &mut String) -> std::io::Result<usize> { |
| 73 | + panic!("attempted to read an rng into a string") |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl<const OUTPUT: usize, R: Rng<OUTPUT>> Deref for BufferedRng<OUTPUT, R> { |
| 78 | + type Target = R; |
| 79 | + |
| 80 | + fn deref(&self) -> &Self::Target { |
| 81 | + &self.rng |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<const OUTPUT: usize, R: Rng<OUTPUT>> DerefMut for BufferedRng<OUTPUT, R> { |
| 86 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 87 | + &mut self.rng |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl<const OUTPUT: usize, R: Rng<OUTPUT> + Default> Default for BufferedRng<OUTPUT, R> { |
| 92 | + fn default() -> Self { |
| 93 | + Self::new(R::default()) |
| 94 | + } |
| 95 | +} |
0 commit comments