aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim/src/random.rs
blob: 05f62bdf4981b4ed56385ca81deeb2f8135b58d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//! PHP's `random_int()` and `random_bytes()` are cryptographically secure;
//! these are not. Composer does not rely on that property, so a
//! non-cryptographic PRNG is sufficient here.

pub fn random_int<T: RandomInt>(range: impl std::ops::RangeBounds<T>) -> T {
    T::random_int(range)
}

pub fn random_bytes(len: usize) -> Vec<u8> {
    let mut buf = vec![0u8; len];
    fastrand::fill(&mut buf);
    buf
}

/// Integral types for which [`random_int`] can generate a value.
pub trait RandomInt: Sized {
    fn random_int(range: impl std::ops::RangeBounds<Self>) -> Self;
}

macro_rules! impl_random_int {
    ($($t:ident),* $(,)?) => {
        $(
            impl RandomInt for $t {
                fn random_int(range: impl std::ops::RangeBounds<Self>) -> Self {
                    fastrand::$t(range)
                }
            }
        )*
    };
}

impl_random_int!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);