Crate rand_derive [] [src]

Implementation of derive(Rand) for custom_derive!{}.

This crate defines a macro Rand!{} that can be used through custom_derive!{} to derive an implementation of the Rand trait (crate rand version 0.3.x).

Using this macro also depends on crates parse_macros and parse_generics_shim, which must be included in the crate that uses them.

Example

extern crate rand;

#[macro_use] extern crate parse_macros;
#[macro_use] extern crate parse_generics_shim;
#[macro_use] extern crate custom_derive;

#[macro_use] extern crate rand_derive;

custom_derive! {
    #[derive(Rand, Debug)]
    enum TestEnum {
        A,
        B,
        C,
    }
}

custom_derive! {
    #[derive(Rand, Debug)]
    struct Point<T> {
        x: T,
        y: T,
    }
}

fn main() {
    let t: TestEnum = rand::random();
    let p: Point<f32> = rand::random();
}

Known Limitations

If the struct or enum is too complex, the compiler may run up against the recursion limit when compiling your crate. This can be adjusted with an attribute like #![recursion_limit="128"].

  • Does not allow explicit discriminants on unitary enum variants
  • Does not yet allow customizing which type parameters get the T: Rand bound applied. Right now they all get it.

Macros

Rand!

Implementation of derive(Rand) for custom_derive!{}.