#![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::{
distr, rand_core, seq, CryptoRng, Fill, Rng, RngCore, SeedableRng, TryCryptoRng, TryRngCore,
};
pub mod prelude {
pub use crate::distr::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::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::TryRngCore for OsRng {
type Error = crate::rand_core::OsError;
#[inline]
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
rand::rngs::OsRng.try_next_u32()
}
#[inline]
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
rand::rngs::OsRng.try_next_u64()
}
#[inline]
fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), Self::Error> {
rand::rngs::OsRng.try_fill_bytes(bytes)
}
}
impl crate::TryCryptoRng 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)
}
}
impl crate::CryptoRng for CapRng {}
}
#[inline]
pub fn thread_rng(ambient_authority: AmbientAuthority) -> rngs::CapRng {
let _ = ambient_authority;
rngs::CapRng { inner: rand::rng() }
}
#[inline]
pub fn std_rng_from_os_rng(ambient_authority: AmbientAuthority) -> rngs::StdRng {
let _ = ambient_authority;
rand::rngs::StdRng::from_os_rng()
}
#[inline]
pub fn random<T>(ambient_authority: AmbientAuthority) -> T
where
crate::distr::StandardUniform: crate::distr::Distribution<T>,
{
let _ = ambient_authority;
rand::random()
}