fstd 0.1.2

A fast standard library for Rust.
Documentation
//! Utilities for random number generation.
//!
//! The implementation uses [`wyrand`] for now. It's **NOT** cryptographically secure.
//!
//! # Examples
//!
//! The public functions in [`rand`] is **NOT** reproducible due to possible replacement in future library versions.
//!
//! For a reproducible generator, use a named PRNG struct, e.g. [`WyRng`].
//!
//!
//! Generate a random `u64`:
//!
//! ```
//! let r = fstd::rand::u64(..);
//! ```
//! Generate a random `u64` in the `[0,100)`:
//!
//! ```
//! let r = fstd::rand::u64(0..100);
//! ```
//!
//! Create a local PRNG with a random seed and generate a random `u64`:
//!
//! ```
//! use fstd::rand::WyRng;
//! let rng = WyRng::new();
//! let r = rng.u64(..);
//! ```
//!
//! Create a local PRNG with specific seed and generate a random `u64`,
//! it is a reproducible generator.
//!
//! ```
//! use fstd::rand::WyRng;
//! let rng = WyRng::from_seed_u64(123);
//! let r = rng.u64(..);
//! ```

//! [`rand`]: crate::rand
//! [`wyrand`]: https://github.com/wangyi-fudan/wyhash

pub(crate) mod wyrand;

use self::wyrand::THREAD_WYRAND as THREAD_RNG;
use std::ops::RangeBounds;
use std::stringify;
pub use wyrand::WyRng;

macro_rules! api_impl {
    // Raw API.
    ($t:tt,$doc:tt) => {
        #[doc = $doc]
        #[inline(always)]
        pub fn $t() -> $t {
            THREAD_RNG.try_with(|rng| rng.$t()).unwrap()
        }
    };
    // API with specific range.
    ($t:tt) => {
        #[doc = "Returns a random `"]
        #[doc = stringify!($t)]
        #[doc = "` in the given range(thread-safe, but not cryptographically secure)."]
        #[doc = "Panics if the range is invalid."]
        #[inline(always)]
        pub fn $t(range: impl RangeBounds<$t>) -> $t {
            THREAD_RNG.try_with(|rng| rng.$t(range)).unwrap()
        }
    };
}

api_impl!(
    f64,
    "Returns a random `f64` in the `[0.0,1.0)`. (thread-safe, but not cryptographically secure)"
);
api_impl!(
    f32,
    "Returns a random `f32` in the `[0.0,1.0)`. (thread-safe, but not cryptographically secure)"
);

api_impl!(u32);
api_impl!(i32);
api_impl!(u64);
api_impl!(i64);
api_impl!(usize);
api_impl!(isize);