#random #macro-derive #rand #generate #macro #derive

macro rand_derive2

Generate customizable random types with the rand crate

21 releases

0.1.21 Jun 18, 2023
0.1.18 Oct 18, 2022
0.1.17 Dec 13, 2021
0.1.16 Jan 6, 2021
0.1.15 Nov 27, 2020

#107 in Procedural macros

Download history 73/week @ 2023-12-13 45/week @ 2023-12-20 48/week @ 2023-12-27 40/week @ 2024-01-03 77/week @ 2024-01-10 46/week @ 2024-01-17 21/week @ 2024-01-24 28/week @ 2024-01-31 175/week @ 2024-02-07 91/week @ 2024-02-14 90/week @ 2024-02-21 139/week @ 2024-02-28 87/week @ 2024-03-06 88/week @ 2024-03-13 136/week @ 2024-03-20 143/week @ 2024-03-27

461 downloads per month
Used in 9 crates (8 directly)

MIT/Apache

18KB
381 lines

Rand derive 2

Latest Version Build Status

Derive macro for generating random types with the rand crate. It will implement the rand::distributions::Standard for a given type.

Usage

Check out the example crate or follow the instructions below.

  1. Add this to your Cargo.toml file:
[dependencies]
rand_derive2 = "0.1"
rand = "0.8"
  1. Import the macro somewhere in your file where your type is:
use rand_derive2::RandGen;

Alternatively, use this to global import the macro:

#[macro_use]
extern crate rand_derive2;
  1. Add the RandGen derive macro for your type:
#[derive(RandGen)]
struct MyStruct {}
  1. Generate your struct:
fn generate_my_struct() -> MyStruct { 
    rand::random()
}

// Or like this
fn generate_my_struct_direct() -> MyStruct { 
    MyStruct::generate_random()
}

Customization

Note: all things that can be customized is covered in the example crate

Options

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.

Skip enum variant

If a variant should never be generated, add the rand_derive(skip) attribute on the variant.

Custom value

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.

No rand

Panic implementation of the property, making the type unable to be random generated. Add the rand_derive(panic) attribute for this case.

Default value

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.

Empty

Can be placed above types which holds a Vec. It will generate an empty vec.

Fixed value

Place rand_derive(fixed = "MY_VALUE") above a field to make it generate the fixed value.

How it works

Structs

It calls rng.gen() on all the fields.

Enums

It will generate a random variant.

Types with lifetimes

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.

TODO

  • Recursion for e.g. vec in vec
  • More types from the standard library covered
  • Functions documented
  • Custom trait type/method names
  • Weighted randomization (currently only supports rand::distributions::Standard)

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~345–800KB
~19K SLoC