Debian Codegraph @

.cargo/vendor/bitflags/src/external/arbitrary.rs

raw
1 //! Specialized fuzzing for flags types using `arbitrary`.
2
3 use crate::Flags;
4
5 /**
6 Generate some arbitrary flags value with only known bits set.
7 */
8 pub fn arbitrary<'a, B: Flags>(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<B>
9 where
10 B::Bits: arbitrary::Arbitrary<'a>,
11 {
12 B::from_bits(u.arbitrary()?).ok_or_else(|| arbitrary::Error::IncorrectFormat)
13 }
14
15 #[cfg(test)]
16 mod tests {
17 use arbitrary::Arbitrary;
18
19 bitflags! {
20 #[derive(Arbitrary)]
21 struct Color: u32 {
22 const RED = 0x1;
23 const GREEN = 0x2;
24 const BLUE = 0x4;
25 }
26 }
27
28 #[test]
29 fn test_arbitrary() {
30 let mut unstructured = arbitrary::Unstructured::new(&[0_u8; 256]);
31 let _color = Color::arbitrary(&mut unstructured);
32 }
33 }