commit | f702b7d22699331b4cd5f5b73335b52d82ae62fd | [log] [tgz] |
---|---|---|
author | Haibo Huang <hhb@google.com> | Wed Feb 10 12:42:26 2021 +0000 |
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | Wed Feb 10 12:42:26 2021 +0000 |
tree | 69b4d09fb2fac223fbd9a285ef985e82a2db2840 | |
parent | c7c0e0ea1855dab664b99559b1e57db58e797de0 [diff] | |
parent | 894bb99a08e122b09e86043b8df6c28f2e1c8edb [diff] |
Upgrade rust/crates/arbitrary to 1.0.0-rc2 am: 3dd43e7d58 am: 5e537b19c1 am: 3b02aa53ae am: 894bb99a08 Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/arbitrary/+/1582216 MUST ONLY BE SUBMITTED BY AUTOMERGER Change-Id: I96f0d1188cec9d53ee16529d3be245ca7ddd9d06
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 docs.rs
!
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).
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 [dependencies] arbitrary = { version = "0.4", features = ["derive"] }
And then you can simply add #[derive(Arbitrary)]
annotations to your types:
// rgb.rs use arbitrary::Arbitrary; #[derive(Arbitrary)] pub struct Rgb { pub r: u8, pub g: u8, pub b: u8, }
Arbitrary
By HandAlternatively, you can write an Arbitrary
implementation by hand:
// rgb.rs 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.