#random #compile-time #macro #parser

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

#2135 in Procedural macros

Download history 18/week @ 2024-03-10 1/week @ 2024-03-17 55/week @ 2024-03-31 53/week @ 2024-04-07 58/week @ 2024-04-14 20/week @ 2024-04-21 12/week @ 2024-04-28 19/week @ 2024-05-12 27/week @ 2024-05-19 6/week @ 2024-05-26 50/week @ 2024-06-02 7/week @ 2024-06-09 1/week @ 2024-06-16 13/week @ 2024-06-23

71 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

~210KB