• Home
Name Date Size #Lines LOC

..--

examples/04-Jul-2025-3420

src/04-Jul-2025-3,4912,047

tests/04-Jul-2025-572444

.android-checksum.jsonD04-Jul-20256 KiB11

.cargo-checksum.jsonD04-Jul-20255.6 KiB11

Android.bpD04-Jul-2025908 3430

CHANGELOG.mdD04-Jul-202514.6 KiB525307

Cargo.lockD04-Jul-20251.5 KiB6456

Cargo.tomlD04-Jul-20251.7 KiB7262

LICENSED04-Jul-202510.6 KiB202169

LICENSE-APACHED04-Jul-202510.6 KiB202169

METADATAD04-Jul-2025406 1817

MODULE_LICENSE_APACHE2D04-Jul-20250

README.mdD04-Jul-20253.4 KiB13093

cargo_embargo.jsonD04-Jul-2025189 1514

publish.shD04-Jul-2025182 156

README.md

1<div align="center">
2
3  <h1><code>Arbitrary</code></h1>
4
5  <p><strong>The trait for generating structured data from arbitrary, unstructured input.</strong></p>
6
7  <img alt="GitHub Actions Status" src="https://github.com/rust-fuzz/rust_arbitrary/workflows/Rust/badge.svg"/>
8
9</div>
10
11## About
12
13The `Arbitrary` crate lets you construct arbitrary instances of a type.
14
15This crate is primarily intended to be combined with a fuzzer like [libFuzzer
16and `cargo-fuzz`](https://github.com/rust-fuzz/cargo-fuzz) or
17[AFL](https://github.com/rust-fuzz/afl.rs), and to help you turn the raw,
18untyped byte buffers that they produce into well-typed, valid, structured
19values. This allows you to combine structure-aware test case generation with
20coverage-guided, mutation-based fuzzers.
21
22## Documentation
23
24[**Read the API documentation on `docs.rs`!**](https://docs.rs/arbitrary)
25
26## Example
27
28Say you're writing a color conversion library, and you have an `Rgb` struct to
29represent RGB colors. You might want to implement `Arbitrary` for `Rgb` so that
30you could take arbitrary `Rgb` instances in a test function that asserts some
31property (for example, asserting that RGB converted to HSL and converted back to
32RGB always ends up exactly where we started).
33
34### Automatically Deriving `Arbitrary`
35
36Automatically deriving the `Arbitrary` trait is the recommended way to implement
37`Arbitrary` for your types.
38
39Automatically deriving `Arbitrary` requires you to enable the `"derive"` cargo
40feature:
41
42```toml
43# Cargo.toml
44
45[dependencies]
46arbitrary = { version = "1", features = ["derive"] }
47```
48
49And then you can simply add `#[derive(Arbitrary)]` annotations to your types:
50
51```rust
52// rgb.rs
53
54use arbitrary::Arbitrary;
55
56#[derive(Arbitrary)]
57pub struct Rgb {
58    pub r: u8,
59    pub g: u8,
60    pub b: u8,
61}
62```
63
64#### Customizing single fields
65
66This can be particular handy if your structure uses a type that does not implement `Arbitrary` or you want to have more customization for particular fields.
67
68```rust
69#[derive(Arbitrary)]
70pub struct Rgba {
71    // set `r` to Default::default()
72    #[arbitrary(default)]
73    pub r: u8,
74
75    // set `g` to 255
76    #[arbitrary(value = 255)]
77    pub g: u8,
78
79    // Generate `b` with a custom function of type
80    //
81    //    fn(&mut Unstructured) -> arbitrary::Result<T>
82    //
83    // where `T` is the field's type.
84    #[arbitrary(with = arbitrary_b)]
85    pub b: u8,
86
87    // Generate `a` with a custom closure (shortuct to avoid a custom function)
88    #[arbitrary(with = |u: &mut Unstructured| u.int_in_range(0..=64))]
89    pub a: u8,
90}
91
92fn arbitrary_b(u: &mut Unstructured) -> arbitrary::Result<u8> {
93    u.int_in_range(64..=128)
94}
95```
96
97### Implementing `Arbitrary` By Hand
98
99Alternatively, you can write an `Arbitrary` implementation by hand:
100
101```rust
102// rgb.rs
103
104use arbitrary::{Arbitrary, Result, Unstructured};
105
106#[derive(Copy, Clone, Debug)]
107pub struct Rgb {
108    pub r: u8,
109    pub g: u8,
110    pub b: u8,
111}
112
113impl<'a> Arbitrary<'a> for Rgb {
114    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
115        let r = u8::arbitrary(u)?;
116        let g = u8::arbitrary(u)?;
117        let b = u8::arbitrary(u)?;
118        Ok(Rgb { r, g, b })
119    }
120}
121```
122
123## License
124
125Licensed under dual MIT or Apache-2.0 at your choice.
126
127Unless you explicitly state otherwise, any contribution intentionally submitted
128for inclusion in this project by you, as defined in the Apache-2.0 license,
129shall be dual licensed as above, without any additional terms or conditions.
130