#![deny(missing_docs)]
#![forbid(unsafe_code)]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/bytecodealliance/cap-std/main/media/cap-std.svg"
)]
#![doc(
html_favicon_url = "https://raw.githubusercontent.com/bytecodealliance/cap-std/main/media/cap-std.ico"
)]
#[doc(hidden)]
pub use ambient_authority::ambient_authority_known_at_compile_time;
pub use ambient_authority::{ambient_authority, AmbientAuthority};
pub use rand::{distributions, seq, CryptoRng, Error, Fill, Rng, RngCore, SeedableRng};
pub mod prelude {
pub use crate::distributions::Distribution;
#[cfg(feature = "small_rng")]
pub use crate::rngs::SmallRng;
pub use crate::rngs::{CapRng, StdRng};
pub use crate::seq::{IteratorRandom, SliceRandom};
pub use crate::{random, thread_rng, CryptoRng, Rng, RngCore, SeedableRng};
}
pub mod rngs {
use super::AmbientAuthority;
pub use rand::rngs::{adapter, mock, StdRng};
#[cfg(feature = "small_rng")]
pub use rand::rngs::SmallRng;
#[derive(Clone, Copy, Debug)]
pub struct OsRng(());
impl OsRng {
#[inline]
pub const fn default(ambient_authority: AmbientAuthority) -> Self {
let _ = ambient_authority;
Self(())
}
}
impl crate::RngCore for OsRng {
#[inline]
fn next_u32(&mut self) -> u32 {
rand::rngs::OsRng.next_u32()
}
#[inline]
fn next_u64(&mut self) -> u64 {
rand::rngs::OsRng.next_u64()
}
#[inline]
fn fill_bytes(&mut self, bytes: &mut [u8]) {
rand::rngs::OsRng.fill_bytes(bytes)
}
#[inline]
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), crate::Error> {
rand::rngs::OsRng.try_fill_bytes(bytes)
}
}
impl crate::CryptoRng for OsRng {}
#[derive(Clone, Debug)]
pub struct CapRng {
pub(super) inner: rand::rngs::ThreadRng,
}
impl CapRng {
#[inline]
pub fn default(ambient_authority: AmbientAuthority) -> Self {
crate::thread_rng(ambient_authority)
}
}
impl crate::RngCore for CapRng {
#[inline]
fn next_u32(&mut self) -> u32 {
self.inner.next_u32()
}
#[inline]
fn next_u64(&mut self) -> u64 {
self.inner.next_u64()
}
#[inline]
fn fill_bytes(&mut self, bytes: &mut [u8]) {
self.inner.fill_bytes(bytes)
}
#[inline]
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), crate::Error> {
self.inner.try_fill_bytes(bytes)
}
}
impl crate::CryptoRng for CapRng {}
}
#[inline]
pub fn thread_rng(ambient_authority: AmbientAuthority) -> rngs::CapRng {
let _ = ambient_authority;
rngs::CapRng {
inner: rand::thread_rng(),
}
}
#[inline]
pub fn std_rng_from_entropy(ambient_authority: AmbientAuthority) -> rngs::StdRng {
let _ = ambient_authority;
rand::rngs::StdRng::from_entropy()
}
#[inline]
pub fn random<T>(ambient_authority: AmbientAuthority) -> T
where
crate::distributions::Standard: crate::distributions::Distribution<T>,
{
let _ = ambient_authority;
rand::random()
}