memberlist_proto/
arbitrary_impl.rs1use super::{Label, Meta};
2
3use arbitrary::{Arbitrary, Result, Unstructured};
4
5impl<'a> Arbitrary<'a> for Meta {
6 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
7 let len = u.int_in_range(0..=Self::MAX_SIZE)?;
8 let mut buf = Vec::with_capacity(len);
9 for _ in 0..len {
10 buf.push(u.arbitrary::<u8>()?);
11 }
12 Ok(Meta::try_from(buf).unwrap())
13 }
14}
15
16#[cfg(any(
17 feature = "crc32",
18 feature = "xxhash64",
19 feature = "xxhash32",
20 feature = "xxhash3",
21 feature = "murmur3",
22))]
23impl<'a> Arbitrary<'a> for super::ChecksumAlgorithm {
24 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
25 Ok(Self::from(u.arbitrary::<u8>()?))
26 }
27}
28
29#[cfg(any(
30 feature = "zstd",
31 feature = "snappy",
32 feature = "lz4",
33 feature = "brotli",
34))]
35impl<'a> Arbitrary<'a> for super::CompressAlgorithm {
36 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
37 Ok(Self::from(u.arbitrary::<u16>()?))
38 }
39}
40
41#[cfg(feature = "encryption")]
42impl<'a> Arbitrary<'a> for super::EncryptionAlgorithm {
43 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
44 Ok(Self::from(u.arbitrary::<u8>()?))
45 }
46}
47
48#[cfg(feature = "encryption")]
49impl<'a> Arbitrary<'a> for super::SecretKeys {
50 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
51 Ok(u.arbitrary::<Vec<super::SecretKey>>()?.into())
52 }
53}
54
55impl<'a> Arbitrary<'a> for Label {
56 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
57 let mut s = String::new();
58 while s.len() < 253 {
59 let c = u.arbitrary::<char>()?;
60 let char_len = c.len_utf8();
61
62 if s.len() + char_len > 253 {
63 break;
64 }
65 s.push(c);
66 }
67
68 Ok(Label(s.into()))
69 }
70}
71
72pub(super) fn bytes(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<bytes::Bytes> {
73 u.arbitrary::<Vec<u8>>().map(Into::into)
74}
75
76pub(super) fn triomphe_arc<'a, T: arbitrary::Arbitrary<'a>>(
77 u: &mut arbitrary::Unstructured<'a>,
78) -> arbitrary::Result<triomphe::Arc<[T]>> {
79 u.arbitrary::<Vec<T>>().map(Into::into)
80}
81
82pub(super) fn from_u8<T: From<u8>>(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<T> {
83 Ok(T::from(u.arbitrary::<u8>()?))
84}
85
86impl<'a> Arbitrary<'a> for super::State {
87 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
88 from_u8(u)
89 }
90}
91
92impl<'a> Arbitrary<'a> for super::ProtocolVersion {
93 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
94 from_u8(u)
95 }
96}
97
98impl<'a> Arbitrary<'a> for super::DelegateVersion {
99 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
100 from_u8(u)
101 }
102}