| 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 |
Unit, |
| 14 |
Tuple(bool, u32), |
| 15 |
Struct { |
| 16 |
x: i8, |
| 17 |
y: (u8, i32), |
| 18 |
}, |
| 19 |
|
| 20 |
#[arbitrary(skip)] |
| 21 |
Skipped(usize), |
| 22 |
} |
| 23 |
|
| 24 |
fn main() { |
| 25 |
let raw = b"This is some raw, unstructured data!"; |
| 26 |
|
| 27 |
let mut unstructured = Unstructured::new(raw); |
| 28 |
|
| 29 |
let instance = MyEnum::arbitrary(&mut unstructured) |
| 30 |
.expect("`unstructured` has enough underlying data to create all variants of `MyEnum`"); |
| 31 |
|
| 32 |
println!("Here is an arbitrary enum: {:?}", instance); |
| 33 |
} |