1 use rand::RngCore; 2 get_rng() -> impl RngCore3pub(crate) fn get_rng() -> impl RngCore { 4 XorShiftStar { 5 a: 0x0123_4567_89AB_CDEF, 6 } 7 } 8 9 /// Simple `Rng` for benchmarking without additional dependencies 10 struct XorShiftStar { 11 a: u64, 12 } 13 14 impl RngCore for XorShiftStar { next_u32(&mut self) -> u3215 fn next_u32(&mut self) -> u32 { 16 self.next_u64() as u32 17 } 18 next_u64(&mut self) -> u6419 fn next_u64(&mut self) -> u64 { 20 // https://en.wikipedia.org/wiki/Xorshift#xorshift* 21 self.a ^= self.a >> 12; 22 self.a ^= self.a << 25; 23 self.a ^= self.a >> 27; 24 self.a.wrapping_mul(0x2545_F491_4F6C_DD1D) 25 } 26 fill_bytes(&mut self, dest: &mut [u8])27 fn fill_bytes(&mut self, dest: &mut [u8]) { 28 for chunk in dest.chunks_mut(8) { 29 let bytes = self.next_u64().to_le_bytes(); 30 let slice = &bytes[..chunk.len()]; 31 chunk.copy_from_slice(slice) 32 } 33 } 34 try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error>35 fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { 36 Ok(self.fill_bytes(dest)) 37 } 38 } 39