1 //! A simple example of deriving the `Arbitrary` trait for an `enum`.
2 //!
3 //! Note that this requires enabling the "derive" cargo feature.
4 
5 // Various enums/fields that we are deriving `Arbitrary` for aren't actually
6 // used except to show off the derive.
7 #![allow(dead_code)]
8 
9 use arbitrary::{Arbitrary, Unstructured};
10 
11 #[derive(Arbitrary, Debug)]
12 enum MyEnum {
13     UnitVariant,
14     TupleVariant(bool, u32),
15     StructVariant { x: i8, y: (u8, i32) },
16 }
17 
main()18 fn main() {
19     let raw = b"This is some raw, unstructured data!";
20 
21     let mut unstructured = Unstructured::new(raw);
22 
23     let instance = MyEnum::arbitrary(&mut unstructured)
24         .expect("`unstructured` has enough underlying data to create all variants of `MyEnum`");
25 
26     println!("Here is an arbitrary enum: {:?}", instance);
27 }
28