Skip to main content

cryprot_core/
rand_compat.rs

1//! Compatability wrapper between rand_core 0.10 and rand_core 0.6.
2use rand::CryptoRng;
3use rand_core::Rng;
4
5/// Compatability wrapper between rand_core 0.10 and rand_core 0.6.
6///
7/// This implements the [`rand_core_0_6::RngCore`] and
8/// [`rand_core_0_6::CryptoRng`] for any version 0.10 RNG that implements the
9/// corresponding traits.
10pub struct RngCompat<R>(pub R);
11
12impl<R: Rng> rand_core_0_6::RngCore for RngCompat<R> {
13    #[inline]
14    fn next_u32(&mut self) -> u32 {
15        self.0.next_u32()
16    }
17
18    #[inline]
19    fn next_u64(&mut self) -> u64 {
20        self.0.next_u64()
21    }
22
23    #[inline]
24    fn fill_bytes(&mut self, dest: &mut [u8]) {
25        self.0.fill_bytes(dest);
26    }
27
28    #[inline]
29    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_0_6::Error> {
30        self.0.fill_bytes(dest);
31        Ok(())
32    }
33}
34
35impl<R: CryptoRng> rand_core_0_6::CryptoRng for RngCompat<R> {}