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.
Rand allows for a random variant of an enum, or struct, to be generated.
The [rand] crates [rand::random] method is used for the default implementation of [Rand]. Unsupported variants can us the #[custom_rand(your_function)]
to extend the functionality.
```rust use enum_derived::Rand;
struct Weather { windspeed: u8, #[customrand(rand_temp)] temperature: f32, cloudy: bool }
enum TravelLog { Airplane { weather: Weather, altitude: u16 }, Boat( Weather, #[customrand()] u32, ), #[customrand(alwayshassunroof)] Car { has_sunroof: bool, }, #[weight(3)] SpaceShip, }
fn main() { let travel_log = TravelLog::rand(); }
fn alwayshassunroof() -> TravelLog { TravelLog::Car { has_sunroof: true } }
fn randboatspeed() -> u32 { threadrng().genrange(5..50) }
fn randtemp() -> f32 { threadrng().gen_range(-20.0..120.0) }
use rand::{thread_rng, Rng}; ```