Snap for 7168215 from 70c40b60a87145d56a055653a49bd85714e61617 to simpleperf-release

Change-Id: I38e52d5f26bb639563a7e322f6560f33b53cc99e
tree: d31bda0402651d782a457726bed3b9088cf824f5
  1. .cargo_vcs_info.json
  2. .github/
  3. .gitignore
  4. Android.bp
  6. Cargo.toml
  7. Cargo.toml.orig
  12. OWNERS
  14. examples/
  15. patches/
  17. src/
  18. tests/


The Arbitrary crate lets you construct arbitrary instance of a type.

This crate is primarily intended to be combined with a fuzzer like libFuzzer and cargo-fuzz or AFL, and to help you turn the raw, untyped byte buffers that they produce into well-typed, valid, structured values. This allows you to combine structure-aware test case generation with coverage-guided, mutation-based fuzzers.


Read the API documentation on!


Say you're writing a color conversion library, and you have an Rgb struct to represent RGB colors. You might want to implement Arbitrary for Rgb so that you could take arbitrary Rgb instances in a test function that asserts some property (for example, asserting that RGB converted to HSL and converted back to RGB always ends up exactly where we started).

Automatically Deriving Arbitrary

Automatically deriving the Arbitrary trait is the recommended way to implement Arbitrary for your types.

Automatically deriving Arbitrary requires you to enable the "derive" cargo feature:

# Cargo.toml

arbitrary = { version = "0.4", features = ["derive"] }

And then you can simply add #[derive(Arbitrary)] annotations to your types:


use arbitrary::Arbitrary;

pub struct Rgb {
    pub r: u8,
    pub g: u8,
    pub b: u8,

Implementing Arbitrary By Hand

Alternatively, you can write an Arbitrary implementation by hand:


use arbitrary::{Arbitrary, Result, Unstructured};

#[derive(Copy, Clone, Debug)]
pub struct Rgb {
    pub r: u8,
    pub g: u8,
    pub b: u8,

impl<'a> Arbitrary<'a> for Rgb {
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
        let r = u8::arbitrary(u)?;
        let g = u8::arbitrary(u)?;
        let b = u8::arbitrary(u)?;
        Ok(Rgb { r, g, b })


Licensed under dual MIT or Apache-2.0 at your choice.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.