Crates.io | rand_derive2 |
lib.rs | rand_derive2 |
version | 0.1.21 |
source | src |
created_at | 2020-08-13 12:59:29.67013 |
updated_at | 2023-06-18 07:25:43.407312 |
description | Generate customizable random types with the rand crate |
homepage | |
repository | https://github.com/jasperav/rand_derive2 |
max_upload_size | |
id | 276214 |
size | 33,324 |
Derive macro for generating random types with the rand
crate.
It will implement the rand::distributions::Standard
for a given type.
Check out the example crate or follow the instructions below.
[dependencies]
rand_derive2 = "0.1"
rand = "0.8"
use rand_derive2::RandGen;
Alternatively, use this to global import the macro:
#[macro_use]
extern crate rand_derive2;
#[derive(RandGen)]
struct MyStruct {}
fn generate_my_struct() -> MyStruct {
rand::random()
}
// Or like this
fn generate_my_struct_direct() -> MyStruct {
MyStruct::generate_random()
}
Note: all things that can be customized is covered in the example crate
To make sure an option is never generated with None
, add the rand_derive(none)
attribute on top of the property.
To make sure an option is never generated with Some
, add the rand_derive(some)
attribute on top of the property.
If a variant should never be generated, add the rand_derive(skip)
attribute on the variant.
If you want a custom value for one of the properties, add the rand_derive(custom)
attribute.
A trait is created called TestDataProviderFor$TYPE$.
This trait will require the user to provider the values.
Panic implementation of the property, making the type unable to be random generated.
Add the rand_derive(panic)
attribute for this case.
Place rand_derive(default)
above a field to make it generate the default value. Note: for a Vec, this will create a vec
with 1 element inside it which holds the default value.
Can be placed above types which holds a Vec. It will generate an empty vec.
Place rand_derive(fixed = "MY_VALUE")
above a field to make it generate the fixed value.
It calls rng.gen()
on all the fields.
It will generate a random variant.
It doesn't make sense to generate a type with a lifetime attached, because than a type with the owned values must be created somewhere, but where? It could be maybe done by leaking the type that owns the values, but that isn't implemented at the moment.
rand::distributions::Standard
)