• Home
  • Raw
  • Download

Lines Matching full:arbitrary

3   <h1><code>Arbitrary</code></h1>
5 …<p><strong>The trait for generating structured data from arbitrary, unstructured input.</strong></…
13 The `Arbitrary` crate lets you construct arbitrary instances of a type.
24 [**Read the API documentation on `docs.rs`!**](https://docs.rs/arbitrary)
29 represent RGB colors. You might want to implement `Arbitrary` for `Rgb` so that
30 you could take arbitrary `Rgb` instances in a test function that asserts some
34 ### Automatically Deriving `Arbitrary`
36 Automatically deriving the `Arbitrary` trait is the recommended way to implement
37 `Arbitrary` for your types.
39 Automatically deriving `Arbitrary` requires you to enable the `"derive"` cargo
46 arbitrary = { version = "1", features = ["derive"] }
49 And then you can simply add `#[derive(Arbitrary)]` annotations to your types:
54 use arbitrary::Arbitrary;
56 #[derive(Arbitrary)]
66 This can be particular handy if your structure uses a type that does not implement `Arbitrary` or y…
69 #[derive(Arbitrary)]
72 #[arbitrary(default)]
76 #[arbitrary(value = 255)]
81 // fn(&mut Unstructured) -> arbitrary::Result<T>
84 #[arbitrary(with = arbitrary_b)]
88 #[arbitrary(with = |u: &mut Unstructured| u.int_in_range(0..=64))]
92 fn arbitrary_b(u: &mut Unstructured) -> arbitrary::Result<u8> {
97 ### Implementing `Arbitrary` By Hand
99 Alternatively, you can write an `Arbitrary` implementation by hand:
104 use arbitrary::{Arbitrary, Result, Unstructured};
113 impl<'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)?;