• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 use arbitrary::{Arbitrary, Unstructured};
6 
7 #[derive(Arbitrary, Debug)]
8 enum MyEnum {
9     UnitVariant,
10     TupleVariant(bool, u32),
11     StructVariant { x: i8, y: (u8, i32) },
12 }
13 
main()14 fn main() {
15     let raw = b"This is some raw, unstructured data!";
16 
17     let mut unstructured = Unstructured::new(raw);
18 
19     let instance = MyEnum::arbitrary(&mut unstructured)
20         .expect("`unstructured` has enough underlying data to create all variants of `MyEnum`");
21 
22     println!("Here is an arbitrary enum: {:?}", instance);
23 }
24