#random #enums #rand #struct #derive

enum-derived

Generate random instances of your enums and structs

24 releases

0.9.2 Mar 4, 2025
0.8.2 Mar 7, 2023

#404 in Rust patterns

Download history 288/week @ 2024-11-18 186/week @ 2024-11-25 485/week @ 2024-12-02 273/week @ 2024-12-09 399/week @ 2024-12-16 66/week @ 2024-12-23 349/week @ 2024-12-30 509/week @ 2025-01-06 793/week @ 2025-01-13 651/week @ 2025-01-20 718/week @ 2025-01-27 868/week @ 2025-02-03 396/week @ 2025-02-10 538/week @ 2025-02-17 900/week @ 2025-02-24 991/week @ 2025-03-03

2,838 downloads per month

MIT license

12KB
167 lines

Enum-Derived

Use enum-derived's Rand macro to generate random variants of your enums and structs. All fields are populated with independent random values.

Need custom constraints applied to a variant or field? Use the #[custom_rand(your_function)] attribute to override the default behavior or extend support to types without default support.

Need some variants to be generated more ofter? Use the #[weight(VARIANT_WEIGHT)] to change the distribution.

crates.io Build


Rand

Rand allows for a random variant of an enum, or struct, to be generated.

The [rand] crate's rand::random method is used for the default implementation of [Rand]. Unsupported variants can us the #[custom_rand(your_function)] to extend the functionality.

Note

Support for String, Vec, HashMap<K, V>, and HashSet has been added. The implementation for String and Vec will create an instance with a lenght between 1 and 64 elements. The implementation for HashMap and HashSet will create an instance with 1 to 16 elements.

Example

use rand::{thread_rng, Rng};
use enum_derived::Rand;

#[derive(Rand)]
struct Weather {
    wind_speed: u8,
    #[custom_rand(rand_temp)]
    temperature: f32,
    cloudy: bool,
    location: String,
}

#[derive(Rand)]
enum TravelLogEntry {
    Airplane {
        weather: Weather,
        altitude: u16
    },
    Boat(
        Weather,
        #[custom_rand(rand_boat_speed)]
        u32,
    ),
    #[custom_rand(always_has_sunroof)]
    Car {
        has_sunroof: bool,
    },
    #[weight(3)]
    SpaceShip,
}

#[derive(Rand)]
pub struct TravelLog(Vec<TravelLogEntry>);

fn sample_rand_call() {
    let travel_log = TravelLog::rand();
}

fn always_has_sunroof() -> TravelLogEntry {
    TravelLogEntry::Car { has_sunroof: true }
}

fn rand_boat_speed() -> u32 {
    thread_rng().gen_range(5..50)
}

fn rand_temp() -> f32 {
   thread_rng().gen_range(-20.0..120.0)
}

Dependencies

~2MB
~43K SLoC