#random #compile-time #parser #macro

macro get-random-const

Simple compile-time random generator

6 stable releases

2.0.3 Apr 22, 2023
2.0.2 Apr 21, 2023
2.0.1 Jul 17, 2020
1.0.1 Apr 25, 2020

#2372 in Procedural macros

Download history 2/week @ 2024-07-17 3/week @ 2024-07-24 5/week @ 2024-07-31 14/week @ 2024-08-07 9/week @ 2024-08-14 11/week @ 2024-08-21 25/week @ 2024-08-28 17/week @ 2024-09-04 28/week @ 2024-09-11 51/week @ 2024-09-18 42/week @ 2024-09-25 19/week @ 2024-10-02 48/week @ 2024-10-09 17/week @ 2024-10-16 1/week @ 2024-10-30

66 downloads per month
Used in xxhash-c

BSL-1.0 license

7KB
93 lines

get-random-const

Crates.io Documentation

Macro to generate random at compile time

use get_random_const::random;

const RANDOM_U8: u8 = random!(u8);
assert_ne!(RANDOM_U8, 0);

static RANDOM_I32: i32 = random!(i32);
assert_ne!(RANDOM_I32, 0);

let random_u32 = random!(u32);
assert_ne!(random_u32, 0);

let random_array = random!([u32;5]);
assert_eq!(random_array.len(), 5);

for elem in random_array.iter() {
    assert_ne!(*elem, 0);
}

let random_array: [u32; 0] = random!([u32;0]); //Well, I guess you can if you want?
assert_eq!(random_array.len(), 0);

lib.rs:

Simple compile time random generator

Example

use get_random_const::random;

const RANDOM_U8: u8 = random!(u8);
assert_ne!(RANDOM_U8, 0);

static RANDOM_I32: i32 = random!(i32);
assert_ne!(RANDOM_I32, 0);

assert_ne!(random!(u8), 0u8);
assert_ne!(random!(i8), 0i8);
assert_ne!(random!(u16), 0u16);
assert_ne!(random!(i16), 0i16);
assert_ne!(random!(u32), 0u32);
assert_ne!(random!(i32), 0i32);
assert_ne!(random!(u64), 0u64);
assert_ne!(random!(i64), 0i64);
assert_ne!(random!(u128), 0u128);
assert_ne!(random!(i128), 0i128);
assert_ne!(random!(usize), 0usize);
assert_ne!(random!(isize), 0isize);

let random_array = random!([u32;5]);
assert_eq!(random_array.len(), 5);

for elem in random_array.iter() {
    assert_ne!(*elem, 0);
}

let random_array: [u32; 0] = random!([u32;0]); //Well, I guess you can if you want?
assert_eq!(random_array.len(), 0);

Dependencies

~215KB