use crate::{
traits::{GenCore, TurboCore},
RngCore,
};
#[cfg(feature = "wyrand")]
use crate::rng::Rng;
#[cfg(feature = "chacha")]
use crate::chacha_rng::ChaChaRng;
#[cfg(feature = "atomic")]
use crate::rng::AtomicRng;
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
#[derive(PartialEq, Eq)]
#[repr(transparent)]
pub struct RandCompat<T: TurboCore + GenCore>(T);
#[cfg(feature = "std")]
impl<T: TurboCore + GenCore + Default> RandCompat<T> {
#[inline]
#[must_use]
pub fn new() -> Self {
Self(T::default())
}
}
#[cfg(feature = "std")]
impl<T: TurboCore + GenCore + Default> Default for RandCompat<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<T: TurboCore + GenCore> RngCore for RandCompat<T> {
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.gen_u32()
}
#[inline]
fn next_u64(&mut self) -> u64 {
self.0.gen_u64()
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.0.fill_bytes(dest);
Ok(())
}
}
impl<T: TurboCore + GenCore> From<T> for RandCompat<T> {
#[inline]
fn from(rng: T) -> Self {
Self(rng)
}
}
#[cfg(feature = "wyrand")]
impl From<RandCompat<Rng>> for Rng {
#[inline]
fn from(rand: RandCompat<Rng>) -> Self {
rand.0
}
}
#[cfg(feature = "atomic")]
impl From<RandCompat<AtomicRng>> for AtomicRng {
#[inline]
fn from(rand: RandCompat<AtomicRng>) -> Self {
rand.0
}
}
#[cfg(feature = "chacha")]
impl From<RandCompat<ChaChaRng>> for ChaChaRng {
#[inline]
fn from(rand: RandCompat<ChaChaRng>) -> Self {
rand.0
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
#[derive(PartialEq, Eq)]
#[repr(transparent)]
pub struct RandBorrowed<'a, T: TurboCore + GenCore>(&'a mut T);
impl<'a, T: TurboCore + GenCore> From<&'a mut T> for RandBorrowed<'a, T> {
#[inline]
fn from(rng: &'a mut T) -> Self {
Self(rng)
}
}
impl<'a, T: TurboCore + GenCore + Default> RngCore for RandBorrowed<'a, T> {
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.gen_u32()
}
#[inline]
fn next_u64(&mut self) -> u64 {
self.0.gen_u64()
}
#[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest);
}
#[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.0.fill_bytes(dest);
Ok(())
}
}