use core::ops::DerefMut;
use std::ops::Deref;
use rand::RngCore;
pub struct BufRand<R: RngCore> {
bit_buf: u64,
shift_counter: u8,
rand: R,
}
impl<R: RngCore> DerefMut for BufRand<R> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.rand
}
}
impl<R: RngCore> Deref for BufRand<R> {
type Target = R;
fn deref(&self) -> &Self::Target {
&self.rand
}
}
impl<R: RngCore> BufRand<R> {
pub fn new(rand: R) -> Self {
BufRand {
bit_buf: 0,
shift_counter: 0xff, rand,
}
}
pub fn next_bool(&mut self) -> bool {
if self.shift_counter >= 64 {
self.bit_buf = self.next_u64();
self.shift_counter = 0;
}
let out = self.bit_buf % 2 == 0;
self.bit_buf >>= 1;
self.shift_counter += 1;
out
}
pub fn rand_char_case(&mut self, c: &char) -> String {
if self.next_bool() {
c.to_uppercase().to_string()
} else {
c.to_lowercase().to_string()
}
}
pub fn rand_string_case(&mut self, s: &str) -> String {
s.chars()
.into_iter()
.map(|c| self.rand_char_case(&c))
.collect()
}
}