/rust/registry/src/index.crates.io-6f17d22bba15001f/arbitrary-0.2.0/src/lib.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! The Arbitrary trait crate |
2 | | //! |
3 | | //! This trait provides an `Arbitrary` trait to produce well-typed data from |
4 | | //! byte buffers. The crate additionally provides different flavors of byte |
5 | | //! buffers with useful semantics. |
6 | | #![deny(warnings)] |
7 | | #![deny(bad_style)] |
8 | | #![deny(missing_docs)] |
9 | | #![deny(future_incompatible)] |
10 | | #![deny(nonstandard_style)] |
11 | | #![deny(rust_2018_compatibility)] |
12 | | #![deny(rust_2018_idioms)] |
13 | | #![deny(unused)] |
14 | | |
15 | | use std::borrow::{Cow, ToOwned}; |
16 | | use std::cell::{Cell, RefCell, UnsafeCell}; |
17 | | use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; |
18 | | use std::ffi::{CString, OsString}; |
19 | | use std::iter; |
20 | | use std::path::PathBuf; |
21 | | use std::rc::Rc; |
22 | | use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize}; |
23 | | use std::sync::{Arc, Mutex}; |
24 | | use std::time::Duration; |
25 | | |
26 | | /// Unstructured data from which structured `Arbitrary` data shall be generated. |
27 | | /// |
28 | | /// This could be a random number generator, a static ring buffer of bytes or some such. |
29 | | pub trait Unstructured { |
30 | | /// The error type for [`Unstructured`], see implementations for details |
31 | | type Error; |
32 | | /// Fill a `buffer` with bytes, forming the unstructured data from which |
33 | | /// `Arbitrary` structured data shall be generated. |
34 | | /// |
35 | | /// This operation is fallible to allow implementations based on e.g. I/O. |
36 | | fn fill_buffer(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error>; |
37 | | |
38 | | /// Generate a size for container. |
39 | | /// |
40 | | /// e.g. number of elements in a vector |
41 | 0 | fn container_size(&mut self) -> Result<usize, Self::Error> { |
42 | 0 | <u8 as Arbitrary>::arbitrary(self).map(|x| x as usize) |
43 | 0 | } |
44 | | } |
45 | | |
46 | | /// A trait to generate and shrink arbitrary types from an [`Unstructured`] pool |
47 | | /// of bytes. |
48 | | pub trait Arbitrary: Sized + 'static { |
49 | | /// Generate arbitrary structured data from unstructured data. |
50 | | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error>; |
51 | | |
52 | | /// Generate derived values which are “smaller” than the original one. |
53 | 0 | fn shrink(&self) -> Box<dyn Iterator<Item = Self>> { |
54 | 0 | Box::new(iter::empty()) |
55 | 0 | } |
56 | | } |
57 | | |
58 | | impl Arbitrary for () { |
59 | 0 | fn arbitrary<U: Unstructured + ?Sized>(_: &mut U) -> Result<Self, U::Error> { |
60 | 0 | Ok(()) |
61 | 0 | } |
62 | | } |
63 | | |
64 | | impl Arbitrary for bool { |
65 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
66 | 0 | Ok(<u8 as Arbitrary>::arbitrary(u)? & 1 == 1) |
67 | 0 | } |
68 | | } |
69 | | |
70 | | impl Arbitrary for u8 { |
71 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
72 | 0 | let mut x = [0]; |
73 | 0 | u.fill_buffer(&mut x)?; |
74 | 0 | Ok(x[0]) |
75 | 0 | } Unexecuted instantiation: <u8 as arbitrary::Arbitrary>::arbitrary::<arbitrary::RingBuffer> Unexecuted instantiation: <u8 as arbitrary::Arbitrary>::arbitrary::<_> |
76 | | } |
77 | | |
78 | | impl Arbitrary for i8 { |
79 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
80 | 0 | Ok(<u8 as Arbitrary>::arbitrary(u)? as Self) |
81 | 0 | } |
82 | | } |
83 | | |
84 | | impl Arbitrary for u16 { |
85 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
86 | 0 | let mut x = [0, 0]; |
87 | 0 | u.fill_buffer(&mut x)?; |
88 | 0 | Ok(Self::from(x[0]) | Self::from(x[1]) << 8) |
89 | 0 | } Unexecuted instantiation: <u16 as arbitrary::Arbitrary>::arbitrary::<arbitrary::RingBuffer> Unexecuted instantiation: <u16 as arbitrary::Arbitrary>::arbitrary::<arbitrary::FiniteBuffer> |
90 | | } |
91 | | |
92 | | impl Arbitrary for i16 { |
93 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
94 | 0 | Ok(<u16 as Arbitrary>::arbitrary(u)? as Self) |
95 | 0 | } |
96 | | } |
97 | | |
98 | | impl Arbitrary for u32 { |
99 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
100 | 0 | let mut x = [0, 0, 0, 0]; |
101 | 0 | u.fill_buffer(&mut x)?; |
102 | 0 | Ok(Self::from(x[0]) |
103 | 0 | | Self::from(x[1]) << 8 |
104 | 0 | | Self::from(x[2]) << 16 |
105 | 0 | | Self::from(x[3]) << 24) |
106 | 0 | } Unexecuted instantiation: <u32 as arbitrary::Arbitrary>::arbitrary::<arbitrary::RingBuffer> Unexecuted instantiation: <u32 as arbitrary::Arbitrary>::arbitrary::<arbitrary::FiniteBuffer> |
107 | | } |
108 | | |
109 | | impl Arbitrary for i32 { |
110 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
111 | 0 | Ok(<u32 as Arbitrary>::arbitrary(u)? as Self) |
112 | 0 | } |
113 | | } |
114 | | |
115 | | impl Arbitrary for u64 { |
116 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
117 | 0 | let mut x = [0, 0, 0, 0, 0, 0, 0, 0]; |
118 | 0 | u.fill_buffer(&mut x)?; |
119 | 0 | Ok(Self::from(x[0]) |
120 | 0 | | Self::from(x[1]) << 8 |
121 | 0 | | Self::from(x[2]) << 16 |
122 | 0 | | Self::from(x[3]) << 24 |
123 | 0 | | Self::from(x[4]) << 32 |
124 | 0 | | Self::from(x[5]) << 40 |
125 | 0 | | Self::from(x[6]) << 48 |
126 | 0 | | Self::from(x[7]) << 56) |
127 | 0 | } Unexecuted instantiation: <u64 as arbitrary::Arbitrary>::arbitrary::<arbitrary::RingBuffer> Unexecuted instantiation: <u64 as arbitrary::Arbitrary>::arbitrary::<arbitrary::FiniteBuffer> |
128 | | } |
129 | | |
130 | | impl Arbitrary for i64 { |
131 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
132 | 0 | Ok(<u64 as Arbitrary>::arbitrary(u)? as Self) |
133 | 0 | } |
134 | | } |
135 | | |
136 | | impl Arbitrary for usize { |
137 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
138 | 0 | Ok(match ::std::mem::size_of::<Self>() { |
139 | 0 | 2 => <u16 as Arbitrary>::arbitrary(u)? as Self, |
140 | 0 | 4 => <u32 as Arbitrary>::arbitrary(u)? as Self, |
141 | 0 | 8 => <u64 as Arbitrary>::arbitrary(u)? as Self, |
142 | 0 | _ => unreachable!(), // welcome, 128 bit machine users |
143 | | }) |
144 | 0 | } Unexecuted instantiation: <usize as arbitrary::Arbitrary>::arbitrary::<arbitrary::RingBuffer> Unexecuted instantiation: <usize as arbitrary::Arbitrary>::arbitrary::<arbitrary::FiniteBuffer> |
145 | | } |
146 | | |
147 | | impl Arbitrary for isize { |
148 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
149 | 0 | Ok(<usize as Arbitrary>::arbitrary(u)? as Self) |
150 | 0 | } |
151 | | } |
152 | | |
153 | | impl Arbitrary for f32 { |
154 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
155 | 0 | Ok(Self::from_bits(<u32 as Arbitrary>::arbitrary(u)?)) |
156 | 0 | } |
157 | | } |
158 | | |
159 | | impl Arbitrary for f64 { |
160 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
161 | 0 | Ok(Self::from_bits(<u64 as Arbitrary>::arbitrary(u)?)) |
162 | 0 | } |
163 | | } |
164 | | |
165 | | impl Arbitrary for char { |
166 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
167 | | const CHAR_MASK: u32 = 0x001f_ffff; |
168 | 0 | let mut c = <u32 as Arbitrary>::arbitrary(u)? & CHAR_MASK; |
169 | | loop { |
170 | | // Cannot do rejection sampling which the rand crate does, because it may result in |
171 | | // infinite loop with unstructured data provided by a ring buffer. Instead we just pick |
172 | | // closest valid character which comes before the current one. |
173 | | // |
174 | | // Note, of course this does not result in unbiased data, but it is not really |
175 | | // necessary for either quickcheck or fuzzing. |
176 | 0 | if let Some(c) = ::std::char::from_u32(c) { |
177 | 0 | return Ok(c); |
178 | 0 | } else { |
179 | 0 | c -= 1; |
180 | 0 | } |
181 | | } |
182 | 0 | } |
183 | | } |
184 | | |
185 | | impl Arbitrary for AtomicBool { |
186 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
187 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
188 | 0 | } |
189 | | } |
190 | | |
191 | | impl Arbitrary for AtomicIsize { |
192 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
193 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
194 | 0 | } |
195 | | } |
196 | | |
197 | | impl Arbitrary for AtomicUsize { |
198 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
199 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
200 | 0 | } |
201 | | } |
202 | | |
203 | | impl Arbitrary for Duration { |
204 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
205 | 0 | Ok(Self::new( |
206 | 0 | Arbitrary::arbitrary(u)?, |
207 | 0 | <u32 as Arbitrary>::arbitrary(u)? % 1_000_000_000, |
208 | | )) |
209 | 0 | } |
210 | | } |
211 | | |
212 | | impl<A: Arbitrary> Arbitrary for Option<A> { |
213 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
214 | 0 | Ok(if Arbitrary::arbitrary(u)? { |
215 | 0 | Some(Arbitrary::arbitrary(u)?) |
216 | | } else { |
217 | 0 | None |
218 | | }) |
219 | 0 | } |
220 | | |
221 | 0 | fn shrink(&self) -> Box<dyn Iterator<Item = Self>> { |
222 | 0 | if let Some(ref a) = *self { |
223 | 0 | Box::new(iter::once(None).chain(a.shrink().map(Some))) |
224 | | } else { |
225 | 0 | Box::new(iter::empty()) |
226 | | } |
227 | 0 | } |
228 | | } |
229 | | |
230 | | impl<A: Arbitrary, B: Arbitrary> Arbitrary for Result<A, B> { |
231 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
232 | 0 | Ok(if Arbitrary::arbitrary(u)? { |
233 | 0 | Ok(Arbitrary::arbitrary(u)?) |
234 | | } else { |
235 | 0 | Err(Arbitrary::arbitrary(u)?) |
236 | | }) |
237 | 0 | } |
238 | | |
239 | 0 | fn shrink(&self) -> Box<dyn Iterator<Item = Self>> { |
240 | 0 | match *self { |
241 | 0 | Ok(ref a) => Box::new(a.shrink().map(Ok)), |
242 | 0 | Err(ref a) => Box::new(a.shrink().map(Err)), |
243 | | } |
244 | 0 | } |
245 | | } |
246 | | |
247 | | macro_rules! arbitrary_tuple { |
248 | | () => {}; |
249 | | ($x: ident $($xs: ident)*) => { |
250 | | arbitrary_tuple!($($xs)*); |
251 | | impl<$x: Arbitrary, $($xs: Arbitrary),*> Arbitrary for ($x, $($xs),*) { |
252 | 0 | fn arbitrary<_U: Unstructured + ?Sized>(u: &mut _U) -> Result<Self, _U::Error> { |
253 | 0 | Ok((Arbitrary::arbitrary(u)?, $($xs::arbitrary(u)?),*)) |
254 | 0 | } Unexecuted instantiation: <(u8, u8, u8, u8, u8, alloc::vec::Vec<u8>, alloc::vec::Vec<u8>) as arbitrary::Arbitrary>::arbitrary::<arbitrary::RingBuffer> Unexecuted instantiation: <(_,) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary::<_> |
255 | | // TODO: shrink |
256 | | } |
257 | | }; |
258 | | } |
259 | | arbitrary_tuple!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z); |
260 | | |
261 | | macro_rules! arbitrary_array { |
262 | | {$n:expr, $t:ident $($ts:ident)*} => { |
263 | | arbitrary_array!{($n - 1), $($ts)*} |
264 | | |
265 | | impl<T: Arbitrary> Arbitrary for [T; $n] { |
266 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<[T; $n], U::Error> { |
267 | 0 | Ok([Arbitrary::arbitrary(u)?, |
268 | 0 | $(<$ts as Arbitrary>::arbitrary(u)?),*]) |
269 | 0 | } Unexecuted instantiation: <[_; 1] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 2] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 3] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 4] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 5] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 6] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 7] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 8] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 9] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 10] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 11] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 12] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 13] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 14] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 15] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 16] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 17] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 18] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 19] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 20] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 21] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 22] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 23] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 24] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 25] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 26] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 27] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 28] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 29] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 30] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 31] as arbitrary::Arbitrary>::arbitrary::<_> Unexecuted instantiation: <[_; 32] as arbitrary::Arbitrary>::arbitrary::<_> |
270 | | // TODO: shrink |
271 | | } |
272 | | }; |
273 | | ($n: expr,) => {}; |
274 | | } |
275 | | |
276 | | arbitrary_array!{ 32, T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T T } |
277 | | |
278 | | impl<A: Arbitrary> Arbitrary for Vec<A> { |
279 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
280 | 0 | let size = u.container_size()?; |
281 | 0 | (0..size).map(|_| Arbitrary::arbitrary(u)).collect() Unexecuted instantiation: <alloc::vec::Vec<u8> as arbitrary::Arbitrary>::arbitrary::<arbitrary::RingBuffer>::{closure#0} Unexecuted instantiation: <alloc::vec::Vec<_> as arbitrary::Arbitrary>::arbitrary::<_>::{closure#0} |
282 | 0 | } Unexecuted instantiation: <alloc::vec::Vec<u8> as arbitrary::Arbitrary>::arbitrary::<arbitrary::RingBuffer> Unexecuted instantiation: <alloc::vec::Vec<_> as arbitrary::Arbitrary>::arbitrary::<_> |
283 | | } |
284 | | |
285 | | impl<K: Arbitrary + Ord, V: Arbitrary> Arbitrary for BTreeMap<K, V> { |
286 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
287 | 0 | let size = u.container_size()?; |
288 | 0 | (0..size).map(|_| Arbitrary::arbitrary(u)).collect() |
289 | 0 | } |
290 | | } |
291 | | |
292 | | impl<A: Arbitrary + Ord> Arbitrary for BTreeSet<A> { |
293 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
294 | 0 | let size = u.container_size()?; |
295 | 0 | (0..size).map(|_| Arbitrary::arbitrary(u)).collect() |
296 | 0 | } |
297 | | } |
298 | | |
299 | | impl<A: Arbitrary + Ord> Arbitrary for BinaryHeap<A> { |
300 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
301 | 0 | let size = u.container_size()?; |
302 | 0 | (0..size).map(|_| Arbitrary::arbitrary(u)).collect() |
303 | 0 | } |
304 | | } |
305 | | |
306 | | impl<K: Arbitrary + Eq + ::std::hash::Hash, V: Arbitrary> Arbitrary for HashMap<K, V> { |
307 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
308 | 0 | let size = u.container_size()?; |
309 | 0 | (0..size).map(|_| Arbitrary::arbitrary(u)).collect() |
310 | 0 | } |
311 | | } |
312 | | |
313 | | impl<A: Arbitrary + Eq + ::std::hash::Hash> Arbitrary for HashSet<A> { |
314 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
315 | 0 | let size = u.container_size()?; |
316 | 0 | (0..size).map(|_| Arbitrary::arbitrary(u)).collect() |
317 | 0 | } |
318 | | } |
319 | | |
320 | | impl<A: Arbitrary> Arbitrary for LinkedList<A> { |
321 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
322 | 0 | let size = u.container_size()?; |
323 | 0 | (0..size).map(|_| Arbitrary::arbitrary(u)).collect() |
324 | 0 | } |
325 | | } |
326 | | |
327 | | impl<A: Arbitrary> Arbitrary for VecDeque<A> { |
328 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
329 | 0 | let size = u.container_size()?; |
330 | 0 | (0..size).map(|_| Arbitrary::arbitrary(u)).collect() |
331 | 0 | } |
332 | | } |
333 | | |
334 | | impl<A> Arbitrary for Cow<'static, A> |
335 | | where |
336 | | A: ToOwned + ?Sized, |
337 | | <A as ToOwned>::Owned: Arbitrary, |
338 | | { |
339 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
340 | 0 | Arbitrary::arbitrary(u).map(Cow::Owned) |
341 | 0 | } |
342 | | } |
343 | | |
344 | | impl Arbitrary for String { |
345 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
346 | 0 | let size = u.container_size()?; |
347 | 0 | (0..size) |
348 | 0 | .map(|_| <char as Arbitrary>::arbitrary(u)) |
349 | 0 | .collect() |
350 | 0 | } |
351 | | } |
352 | | |
353 | | impl Arbitrary for CString { |
354 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
355 | 0 | <Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| { |
356 | 0 | x.retain(|&c| c != 0); |
357 | 0 | Self::new(x).unwrap() |
358 | 0 | }) |
359 | 0 | } |
360 | | } |
361 | | |
362 | | impl Arbitrary for OsString { |
363 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
364 | 0 | <String as Arbitrary>::arbitrary(u).map(From::from) |
365 | 0 | } |
366 | | } |
367 | | |
368 | | impl Arbitrary for PathBuf { |
369 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
370 | 0 | <OsString as Arbitrary>::arbitrary(u).map(From::from) |
371 | 0 | } |
372 | | } |
373 | | |
374 | | impl<A: Arbitrary> Arbitrary for Box<A> { |
375 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
376 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
377 | 0 | } |
378 | | } |
379 | | |
380 | | impl<A: Arbitrary> Arbitrary for Box<[A]> { |
381 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
382 | 0 | <Vec<A> as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_slice()) |
383 | 0 | } |
384 | | } |
385 | | |
386 | | impl Arbitrary for Box<str> { |
387 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
388 | 0 | <String as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_str()) |
389 | 0 | } |
390 | | } |
391 | | |
392 | | // impl Arbitrary for Box<CStr> { |
393 | | // fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
394 | | // <CString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_c_str()) |
395 | | // } |
396 | | // } |
397 | | |
398 | | // impl Arbitrary for Box<OsStr> { |
399 | | // fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
400 | | // <OsString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_osstr()) |
401 | | // |
402 | | // } |
403 | | // } |
404 | | |
405 | | impl<A: Arbitrary> Arbitrary for Arc<A> { |
406 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
407 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
408 | 0 | } |
409 | | } |
410 | | |
411 | | impl<A: Arbitrary> Arbitrary for Rc<A> { |
412 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
413 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
414 | 0 | } |
415 | | } |
416 | | |
417 | | impl<A: Arbitrary> Arbitrary for Cell<A> { |
418 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
419 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
420 | 0 | } |
421 | | } |
422 | | |
423 | | impl<A: Arbitrary> Arbitrary for RefCell<A> { |
424 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
425 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
426 | 0 | } |
427 | | } |
428 | | |
429 | | impl<A: Arbitrary> Arbitrary for UnsafeCell<A> { |
430 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
431 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
432 | 0 | } |
433 | | } |
434 | | |
435 | | impl<A: Arbitrary> Arbitrary for Mutex<A> { |
436 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
437 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
438 | 0 | } |
439 | | } |
440 | | |
441 | | impl<A: Arbitrary> Arbitrary for iter::Empty<A> { |
442 | 0 | fn arbitrary<U: Unstructured + ?Sized>(_: &mut U) -> Result<Self, U::Error> { |
443 | 0 | Ok(iter::empty()) |
444 | 0 | } |
445 | | } |
446 | | |
447 | | impl<A: Arbitrary> Arbitrary for ::std::marker::PhantomData<A> { |
448 | 0 | fn arbitrary<U: Unstructured + ?Sized>(_: &mut U) -> Result<Self, U::Error> { |
449 | 0 | Ok(::std::marker::PhantomData) |
450 | 0 | } |
451 | | } |
452 | | |
453 | | impl<A: Arbitrary> Arbitrary for ::std::num::Wrapping<A> { |
454 | 0 | fn arbitrary<U: Unstructured + ?Sized>(u: &mut U) -> Result<Self, U::Error> { |
455 | 0 | Arbitrary::arbitrary(u).map(::std::num::Wrapping) |
456 | 0 | } |
457 | | } |
458 | | |
459 | | /// An enumeration of buffer creation errors |
460 | | #[derive(Debug, Clone, Copy)] |
461 | | pub enum BufferError { |
462 | | /// The input buffer is empty, causing construction of some buffer types to |
463 | | /// fail |
464 | | EmptyInput, |
465 | | } |
466 | | |
467 | | /// A source of unstructured data with a finite size |
468 | | /// |
469 | | /// This buffer is a finite source of unstructured data. Once the data is |
470 | | /// exhausted it stays exhausted. |
471 | | pub struct FiniteBuffer<'a> { |
472 | | buffer: &'a [u8], |
473 | | offset: usize, |
474 | | max_len: usize, |
475 | | } |
476 | | |
477 | | impl<'a> FiniteBuffer<'a> { |
478 | | /// Create a new FiniteBuffer |
479 | | /// |
480 | | /// If the passed `buffer` is shorter than max_len the total number of bytes |
481 | | /// will be the bytes available in `buffer`. If `buffer` is longer than |
482 | | /// `max_len` the buffer will be trimmed. |
483 | 0 | pub fn new(buffer: &'a [u8], max_len: usize) -> Result<Self, BufferError> { |
484 | 0 | let buf: &'a [u8] = if buffer.len() > max_len { |
485 | 0 | &buffer[..max_len] |
486 | | } else { |
487 | | // This branch is hit if buffer is shorter than max_len. We might |
488 | | // choose to make this an error condition instead of, potentially, |
489 | | // surprising folks with less bytes. |
490 | 0 | buffer |
491 | | }; |
492 | | |
493 | 0 | Ok(FiniteBuffer { |
494 | 0 | buffer: buf, |
495 | 0 | offset: 0, |
496 | 0 | max_len: buf.len(), |
497 | 0 | }) |
498 | 0 | } |
499 | | } |
500 | | |
501 | | impl<'a> Unstructured for FiniteBuffer<'a> { |
502 | | type Error = (); |
503 | | |
504 | 0 | fn fill_buffer(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> { |
505 | 0 | if (self.max_len - self.offset) >= buffer.len() { |
506 | 0 | let max = self.offset + buffer.len(); |
507 | 0 | for (i, idx) in (self.offset..max).enumerate() { |
508 | 0 | buffer[i] = self.buffer[idx]; |
509 | 0 | } |
510 | 0 | self.offset = max; |
511 | 0 | Ok(()) |
512 | | } else { |
513 | 0 | Err(()) |
514 | | } |
515 | 0 | } |
516 | | |
517 | | // NOTE(blt) I'm not sure if this is the right definition. I don't |
518 | | // understand the purpose of container_size. |
519 | 0 | fn container_size(&mut self) -> Result<usize, Self::Error> { |
520 | 0 | <usize as Arbitrary>::arbitrary(self).map(|x| x % self.max_len) |
521 | 0 | } |
522 | | } |
523 | | |
524 | | /// A source of unstructured data which returns the same data over and over again |
525 | | /// |
526 | | /// This buffer acts as a ring buffer over the source of unstructured data, |
527 | | /// allowing for an infinite amount of not-very-random data. |
528 | | pub struct RingBuffer<'a> { |
529 | | buffer: &'a [u8], |
530 | | offset: usize, |
531 | | max_len: usize, |
532 | | } |
533 | | |
534 | | impl<'a> RingBuffer<'a> { |
535 | | /// Create a new RingBuffer |
536 | 0 | pub fn new(buffer: &'a [u8], max_len: usize) -> Result<Self, BufferError> { |
537 | 0 | if buffer.is_empty() { |
538 | 0 | return Err(BufferError::EmptyInput); |
539 | 0 | } |
540 | 0 | Ok(RingBuffer { |
541 | 0 | buffer, |
542 | 0 | offset: 0, |
543 | 0 | max_len, |
544 | 0 | }) |
545 | 0 | } |
546 | | } |
547 | | |
548 | | impl<'a> Unstructured for RingBuffer<'a> { |
549 | | type Error = (); |
550 | 0 | fn fill_buffer(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> { |
551 | 0 | let b = [&self.buffer[self.offset..], &self.buffer[..self.offset]]; |
552 | 0 | let it = ::std::iter::repeat(&b[..]).flat_map(|x| x).flat_map(|&x| x); |
553 | 0 | self.offset = (self.offset + buffer.len()) % self.buffer.len(); |
554 | 0 | for (d, f) in buffer.iter_mut().zip(it) { |
555 | 0 | *d = *f; |
556 | 0 | } |
557 | 0 | Ok(()) |
558 | 0 | } |
559 | | |
560 | 0 | fn container_size(&mut self) -> Result<usize, Self::Error> { |
561 | 0 | <usize as Arbitrary>::arbitrary(self).map(|x| x % self.max_len) |
562 | 0 | } |
563 | | } |
564 | | |
565 | | #[cfg(test)] |
566 | | mod test { |
567 | | use super::*; |
568 | | |
569 | | #[test] |
570 | | fn finite_buffer_fill_buffer() { |
571 | | let x = [1, 2, 3, 4]; |
572 | | let mut rb = FiniteBuffer::new(&x, 10).unwrap(); |
573 | | let mut z = [0; 2]; |
574 | | rb.fill_buffer(&mut z).unwrap(); |
575 | | assert_eq!(z, [1, 2]); |
576 | | rb.fill_buffer(&mut z).unwrap(); |
577 | | assert_eq!(z, [3, 4]); |
578 | | assert!(rb.fill_buffer(&mut z).is_err()); |
579 | | } |
580 | | |
581 | | #[test] |
582 | | fn ring_buffer_fill_buffer() { |
583 | | let x = [1, 2, 3, 4]; |
584 | | let mut rb = RingBuffer::new(&x, 2).unwrap(); |
585 | | let mut z = [0; 10]; |
586 | | rb.fill_buffer(&mut z).unwrap(); |
587 | | assert_eq!(z, [1, 2, 3, 4, 1, 2, 3, 4, 1, 2]); |
588 | | rb.fill_buffer(&mut z).unwrap(); |
589 | | assert_eq!(z, [3, 4, 1, 2, 3, 4, 1, 2, 3, 4]); |
590 | | } |
591 | | |
592 | | #[test] |
593 | | fn ring_buffer_container_size() { |
594 | | let x = [1, 2, 3, 4, 5]; |
595 | | let mut rb = RingBuffer::new(&x, 11).unwrap(); |
596 | | assert_eq!(rb.container_size().unwrap(), 9); |
597 | | assert_eq!(rb.container_size().unwrap(), 1); |
598 | | assert_eq!(rb.container_size().unwrap(), 2); |
599 | | assert_eq!(rb.container_size().unwrap(), 6); |
600 | | assert_eq!(rb.container_size().unwrap(), 1); |
601 | | } |
602 | | } |