2 unstable releases

0.2.0 Aug 29, 2022
0.1.0 Aug 5, 2022

#267 in #variant

Download history 1050/week @ 2024-03-13 1175/week @ 2024-03-20 532/week @ 2024-03-27 977/week @ 2024-04-03 1004/week @ 2024-04-10 785/week @ 2024-04-17 1692/week @ 2024-04-24 446/week @ 2024-05-01 559/week @ 2024-05-08 490/week @ 2024-05-15 1163/week @ 2024-05-22 764/week @ 2024-05-29 293/week @ 2024-06-05 960/week @ 2024-06-12 348/week @ 2024-06-19 614/week @ 2024-06-26

2,303 downloads per month
Used in 2 crates (via random_variant)

MIT license

10KB
182 lines

RandomVariant

Trait that returns a random variant of the given type, if you are creating a new type to limit some values, instead of deriving the RandomVariant, implement it manually to ensure only correct values are created Unless you also want to try the Result type Useful for hunting down unwraps or other assumptions in your code, The benefit over the crate EveryVariant is that this can be limited to A much smaller set to test, where generting every variant quickly can grow to unmanageable testing sets

Example

    use serde::Serialize;
    use std::net::IpAddr;

    #[derive(RandomVariant, Serialize)]
    enum Message {
        Log(String),
        ErrorCode(u32),
        /// This tuple will fail to serialize if flattened in serde
        Rebooting,
    }

    #[derive(RandomVariant, Serialize)]
    struct LoggedMessage {
        /// Fail to serialize, since flatteing enums is not supported
        #[serde(flatten)]
        t: Message,
        add: IpAddr,
    }
    let mut rng = crate::rand::thread_rng();
    
    /// This allows us to limit the testing set to trying 100 randomized structs
    for _i in 0..100 {
        let v = LoggedMessage::random_variant(&mut rng);
        serde_json::to_string(&v).unwrap();
    }


lib.rs:

Proc macros that can help with generating implements of the EveryVariant trait for structs and enums

Dependencies

~1.5MB
~35K SLoC