#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]
pub trait Rand0_6CompatExt {
fn compat(self) -> Rand0_6CompatWrapper<Self>
where
Self: Sized,
{
Rand0_6CompatWrapper(self)
}
fn compat_by_ref(&mut self) -> Rand0_6CompatWrapper<&mut Self>
where
Self: Sized,
{
Rand0_6CompatWrapper(self)
}
}
impl<T> Rand0_6CompatExt for T where T: rand_core::TryRng + ?Sized {}
pub struct Rand0_6CompatWrapper<R: ?Sized>(R);
impl<R> Rand0_6CompatWrapper<R> {
pub fn new(inner: R) -> Self {
Self(inner)
}
pub fn into_inner(self) -> R {
self.0
}
}
#[cfg(not(feature = "std"))]
impl<R> rand_core_06::RngCore for Rand0_6CompatWrapper<R>
where
R: rand_core::TryRng + ?Sized,
{
fn next_u32(&mut self) -> u32 {
self.0.try_next_u32().unwrap()
}
fn next_u64(&mut self) -> u64 {
self.0.try_next_u64().unwrap()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.try_fill_bytes(dest).unwrap()
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
self.0
.try_fill_bytes(dest)
.map_err(|_| rand_core_06::Error::from(core::num::NonZeroU32::new(1).unwrap()))
}
}
#[cfg(feature = "std")]
impl<R> rand_core_06::RngCore for Rand0_6CompatWrapper<R>
where
R: rand_core::TryRng + ?Sized,
R::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
{
fn next_u32(&mut self) -> u32 {
self.0.try_next_u32().unwrap()
}
fn next_u64(&mut self) -> u64 {
self.0.try_next_u64().unwrap()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.try_fill_bytes(dest).unwrap()
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core_06::Error> {
self.0
.try_fill_bytes(dest)
.map_err(rand_core_06::Error::new)
}
}
impl<R> rand_core_06::CryptoRng for Rand0_6CompatWrapper<R> where R: rand_core::TryCryptoRng + ?Sized
{}
#[cfg(test)]
mod tests {
use super::*;
use rand::rngs::SysRng;
use rand_core_06::RngCore as RngCore06;
fn foo<R>(rng: &mut R)
where
R: RngCore06,
{
let _: u32 = rng.next_u32();
}
#[test]
fn test_compat_os_rng() {
foo(&mut SysRng.compat());
}
#[test]
fn test_compat_thread_rng() {
let mut rng = rand::rng();
foo(&mut (rng.compat_by_ref()));
foo(&mut rng.compat());
}
}