/rust/registry/src/index.crates.io-1949cf8c6b5b557f/arbitrary-1.1.6/src/lib.rs
Line | Count | Source |
1 | | // Copyright © 2019 The Rust Fuzz Project Developers. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
4 | | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
5 | | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
6 | | // option. This file may not be copied, modified, or distributed |
7 | | // except according to those terms. |
8 | | |
9 | | //! The `Arbitrary` trait crate. |
10 | | //! |
11 | | //! This trait provides an [`Arbitrary`](./trait.Arbitrary.html) trait to |
12 | | //! produce well-typed, structured values, from raw, byte buffers. It is |
13 | | //! generally intended to be used with fuzzers like AFL or libFuzzer. See the |
14 | | //! [`Arbitrary`](./trait.Arbitrary.html) trait's documentation for details on |
15 | | //! automatically deriving, implementing, and/or using the trait. |
16 | | |
17 | | #![deny(bad_style)] |
18 | | #![deny(missing_docs)] |
19 | | #![deny(future_incompatible)] |
20 | | #![deny(nonstandard_style)] |
21 | | #![deny(rust_2018_compatibility)] |
22 | | #![deny(rust_2018_idioms)] |
23 | | #![deny(unused)] |
24 | | |
25 | | #[cfg(feature = "derive_arbitrary")] |
26 | | pub use derive_arbitrary::*; |
27 | | |
28 | | mod error; |
29 | | pub use error::*; |
30 | | |
31 | | pub mod unstructured; |
32 | | #[doc(inline)] |
33 | | pub use unstructured::Unstructured; |
34 | | |
35 | | pub mod size_hint; |
36 | | |
37 | | use core::array; |
38 | | use core::cell::{Cell, RefCell, UnsafeCell}; |
39 | | use core::iter; |
40 | | use core::mem; |
41 | | use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize}; |
42 | | use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize}; |
43 | | use core::ops::{Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive}; |
44 | | use core::str; |
45 | | use core::time::Duration; |
46 | | use std::borrow::{Cow, ToOwned}; |
47 | | use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque}; |
48 | | use std::ffi::{CString, OsString}; |
49 | | use std::hash::BuildHasher; |
50 | | use std::net::{Ipv4Addr, Ipv6Addr}; |
51 | | use std::ops::Bound; |
52 | | use std::path::PathBuf; |
53 | | use std::rc::Rc; |
54 | | use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize}; |
55 | | use std::sync::{Arc, Mutex}; |
56 | | |
57 | | /// Generate arbitrary structured values from raw, unstructured data. |
58 | | /// |
59 | | /// The `Arbitrary` trait allows you to generate valid structured values, like |
60 | | /// `HashMap`s, or ASTs, or `MyTomlConfig`, or any other data structure from |
61 | | /// raw, unstructured bytes provided by a fuzzer. |
62 | | /// |
63 | | /// # Deriving `Arbitrary` |
64 | | /// |
65 | | /// Automatically deriving the `Arbitrary` trait is the recommended way to |
66 | | /// implement `Arbitrary` for your types. |
67 | | /// |
68 | | /// Using the custom derive requires that you enable the `"derive"` cargo |
69 | | /// feature in your `Cargo.toml`: |
70 | | /// |
71 | | /// ```toml |
72 | | /// [dependencies] |
73 | | /// arbitrary = { version = "1", features = ["derive"] } |
74 | | /// ``` |
75 | | /// |
76 | | /// Then, you add the `#[derive(Arbitrary)]` annotation to your `struct` or |
77 | | /// `enum` type definition: |
78 | | /// |
79 | | /// ``` |
80 | | /// # #[cfg(feature = "derive")] mod foo { |
81 | | /// use arbitrary::Arbitrary; |
82 | | /// use std::collections::HashSet; |
83 | | /// |
84 | | /// #[derive(Arbitrary)] |
85 | | /// pub struct AddressBook { |
86 | | /// friends: HashSet<Friend>, |
87 | | /// } |
88 | | /// |
89 | | /// #[derive(Arbitrary, Hash, Eq, PartialEq)] |
90 | | /// pub enum Friend { |
91 | | /// Buddy { name: String }, |
92 | | /// Pal { age: usize }, |
93 | | /// } |
94 | | /// # } |
95 | | /// ``` |
96 | | /// |
97 | | /// Every member of the `struct` or `enum` must also implement `Arbitrary`. |
98 | | /// |
99 | | /// # Implementing `Arbitrary` By Hand |
100 | | /// |
101 | | /// Implementing `Arbitrary` mostly involves nested calls to other `Arbitrary` |
102 | | /// arbitrary implementations for each of your `struct` or `enum`'s members. But |
103 | | /// sometimes you need some amount of raw data, or you need to generate a |
104 | | /// variably-sized collection type, or something of that sort. The |
105 | | /// [`Unstructured`][crate::Unstructured] type helps you with these tasks. |
106 | | /// |
107 | | /// ``` |
108 | | /// # #[cfg(feature = "derive")] mod foo { |
109 | | /// # pub struct MyCollection<T> { _t: std::marker::PhantomData<T> } |
110 | | /// # impl<T> MyCollection<T> { |
111 | | /// # pub fn new() -> Self { MyCollection { _t: std::marker::PhantomData } } |
112 | | /// # pub fn insert(&mut self, element: T) {} |
113 | | /// # } |
114 | | /// use arbitrary::{Arbitrary, Result, Unstructured}; |
115 | | /// |
116 | | /// impl<'a, T> Arbitrary<'a> for MyCollection<T> |
117 | | /// where |
118 | | /// T: Arbitrary<'a>, |
119 | | /// { |
120 | | /// fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
121 | | /// // Get an iterator of arbitrary `T`s. |
122 | | /// let iter = u.arbitrary_iter::<T>()?; |
123 | | /// |
124 | | /// // And then create a collection! |
125 | | /// let mut my_collection = MyCollection::new(); |
126 | | /// for elem_result in iter { |
127 | | /// let elem = elem_result?; |
128 | | /// my_collection.insert(elem); |
129 | | /// } |
130 | | /// |
131 | | /// Ok(my_collection) |
132 | | /// } |
133 | | /// } |
134 | | /// # } |
135 | | /// ``` |
136 | | pub trait Arbitrary<'a>: Sized { |
137 | | /// Generate an arbitrary value of `Self` from the given unstructured data. |
138 | | /// |
139 | | /// Calling `Arbitrary::arbitrary` requires that you have some raw data, |
140 | | /// perhaps given to you by a fuzzer like AFL or libFuzzer. You wrap this |
141 | | /// raw data in an `Unstructured`, and then you can call `<MyType as |
142 | | /// Arbitrary>::arbitrary` to construct an arbitrary instance of `MyType` |
143 | | /// from that unstructured data. |
144 | | /// |
145 | | /// Implementations may return an error if there is not enough data to |
146 | | /// construct a full instance of `Self`, or they may fill out the rest of |
147 | | /// `Self` with dummy values. Using dummy values when the underlying data is |
148 | | /// exhausted can help avoid accidentally "defeating" some of the fuzzer's |
149 | | /// mutations to the underlying byte stream that might otherwise lead to |
150 | | /// interesting runtime behavior or new code coverage if only we had just a |
151 | | /// few more bytes. However, it also requires that implementations for |
152 | | /// recursive types (e.g. `struct Foo(Option<Box<Foo>>)`) avoid infinite |
153 | | /// recursion when the underlying data is exhausted. |
154 | | /// |
155 | | /// ``` |
156 | | /// # #[cfg(feature = "derive")] fn foo() { |
157 | | /// use arbitrary::{Arbitrary, Unstructured}; |
158 | | /// |
159 | | /// #[derive(Arbitrary)] |
160 | | /// pub struct MyType { |
161 | | /// // ... |
162 | | /// } |
163 | | /// |
164 | | /// // Get the raw data from the fuzzer or wherever else. |
165 | | /// # let get_raw_data_from_fuzzer = || &[]; |
166 | | /// let raw_data: &[u8] = get_raw_data_from_fuzzer(); |
167 | | /// |
168 | | /// // Wrap that raw data in an `Unstructured`. |
169 | | /// let mut unstructured = Unstructured::new(raw_data); |
170 | | /// |
171 | | /// // Generate an arbitrary instance of `MyType` and do stuff with it. |
172 | | /// if let Ok(value) = MyType::arbitrary(&mut unstructured) { |
173 | | /// # let do_stuff = |_| {}; |
174 | | /// do_stuff(value); |
175 | | /// } |
176 | | /// # } |
177 | | /// ``` |
178 | | /// |
179 | | /// See also the documentation for [`Unstructured`][crate::Unstructured]. |
180 | | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>; |
181 | | |
182 | | /// Generate an arbitrary value of `Self` from the entirety of the given |
183 | | /// unstructured data. |
184 | | /// |
185 | | /// This is similar to Arbitrary::arbitrary, however it assumes that it is |
186 | | /// the last consumer of the given data, and is thus able to consume it all |
187 | | /// if it needs. See also the documentation for |
188 | | /// [`Unstructured`][crate::Unstructured]. |
189 | 0 | fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<Self> { |
190 | 0 | Self::arbitrary(&mut u) |
191 | 0 | } |
192 | | |
193 | | /// Get a size hint for how many bytes out of an `Unstructured` this type |
194 | | /// needs to construct itself. |
195 | | /// |
196 | | /// This is useful for determining how many elements we should insert when |
197 | | /// creating an arbitrary collection. |
198 | | /// |
199 | | /// The return value is similar to |
200 | | /// [`Iterator::size_hint`][iterator-size-hint]: it returns a tuple where |
201 | | /// the first element is a lower bound on the number of bytes required, and |
202 | | /// the second element is an optional upper bound. |
203 | | /// |
204 | | /// The default implementation return `(0, None)` which is correct for any |
205 | | /// type, but not ultimately that useful. Using `#[derive(Arbitrary)]` will |
206 | | /// create a better implementation. If you are writing an `Arbitrary` |
207 | | /// implementation by hand, and your type can be part of a dynamically sized |
208 | | /// collection (such as `Vec`), you are strongly encouraged to override this |
209 | | /// default with a better implementation. The |
210 | | /// [`size_hint`][crate::size_hint] module will help with this task. |
211 | | /// |
212 | | /// ## Invariant |
213 | | /// |
214 | | /// It must be possible to construct every possible output using only inputs |
215 | | /// of lengths bounded by these parameters. This applies to both |
216 | | /// [`Arbitrary::arbitrary`] and [`Arbitrary::arbitrary_take_rest`]. |
217 | | /// |
218 | | /// This is trivially true for `(0, None)`. To restrict this further, it |
219 | | /// must be proven that all inputs that are now excluded produced redundant |
220 | | /// outputs which are still possible to produce using the reduced input |
221 | | /// space. |
222 | | /// |
223 | | /// ## The `depth` Parameter |
224 | | /// |
225 | | /// If you 100% know that the type you are implementing `Arbitrary` for is |
226 | | /// not a recursive type, or your implementation is not transitively calling |
227 | | /// any other `size_hint` methods, you can ignore the `depth` parameter. |
228 | | /// Note that if you are implementing `Arbitrary` for a generic type, you |
229 | | /// cannot guarantee the lack of type recursion! |
230 | | /// |
231 | | /// Otherwise, you need to use |
232 | | /// [`arbitrary::size_hint::recursion_guard(depth)`][crate::size_hint::recursion_guard] |
233 | | /// to prevent potential infinite recursion when calculating size hints for |
234 | | /// potentially recursive types: |
235 | | /// |
236 | | /// ``` |
237 | | /// use arbitrary::{Arbitrary, Unstructured, size_hint}; |
238 | | /// |
239 | | /// // This can potentially be a recursive type if `L` or `R` contain |
240 | | /// // something like `Box<Option<MyEither<L, R>>>`! |
241 | | /// enum MyEither<L, R> { |
242 | | /// Left(L), |
243 | | /// Right(R), |
244 | | /// } |
245 | | /// |
246 | | /// impl<'a, L, R> Arbitrary<'a> for MyEither<L, R> |
247 | | /// where |
248 | | /// L: Arbitrary<'a>, |
249 | | /// R: Arbitrary<'a>, |
250 | | /// { |
251 | | /// fn arbitrary(u: &mut Unstructured) -> arbitrary::Result<Self> { |
252 | | /// // ... |
253 | | /// # unimplemented!() |
254 | | /// } |
255 | | /// |
256 | | /// fn size_hint(depth: usize) -> (usize, Option<usize>) { |
257 | | /// // Protect against potential infinite recursion with |
258 | | /// // `recursion_guard`. |
259 | | /// size_hint::recursion_guard(depth, |depth| { |
260 | | /// // If we aren't too deep, then `recursion_guard` calls |
261 | | /// // this closure, which implements the natural size hint. |
262 | | /// // Don't forget to use the new `depth` in all nested |
263 | | /// // `size_hint` calls! We recommend shadowing the |
264 | | /// // parameter, like what is done here, so that you can't |
265 | | /// // accidentally use the wrong depth. |
266 | | /// size_hint::or( |
267 | | /// <L as Arbitrary>::size_hint(depth), |
268 | | /// <R as Arbitrary>::size_hint(depth), |
269 | | /// ) |
270 | | /// }) |
271 | | /// } |
272 | | /// } |
273 | | /// ``` |
274 | | /// |
275 | | /// [iterator-size-hint]: https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.size_hint |
276 | | #[inline] |
277 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
278 | 0 | let _ = depth; |
279 | 0 | (0, None) |
280 | 0 | } |
281 | | } |
282 | | |
283 | | impl<'a> Arbitrary<'a> for () { |
284 | 0 | fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> { |
285 | 0 | Ok(()) |
286 | 0 | } |
287 | | |
288 | | #[inline] |
289 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
290 | 0 | (0, Some(0)) |
291 | 0 | } |
292 | | } |
293 | | |
294 | | impl<'a> Arbitrary<'a> for bool { |
295 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
296 | 0 | Ok(<u8 as Arbitrary<'a>>::arbitrary(u)? & 1 == 1) |
297 | 0 | } |
298 | | |
299 | | #[inline] |
300 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
301 | 0 | <u8 as Arbitrary<'a>>::size_hint(depth) |
302 | 0 | } |
303 | | } |
304 | | |
305 | | macro_rules! impl_arbitrary_for_integers { |
306 | | ( $( $ty:ty: $unsigned:ty; )* ) => { |
307 | | $( |
308 | | impl<'a> Arbitrary<'a> for $ty { |
309 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
310 | 0 | let mut buf = [0; mem::size_of::<$ty>()]; |
311 | 0 | u.fill_buffer(&mut buf)?; |
312 | 0 | let mut x: $unsigned = 0; |
313 | 0 | for i in 0..mem::size_of::<$ty>() { |
314 | 0 | x |= buf[i] as $unsigned << (i * 8); |
315 | 0 | } |
316 | 0 | Ok(x as $ty) |
317 | 0 | } Unexecuted instantiation: <u8 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <u16 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <u32 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <u64 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <u128 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <usize as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <i8 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <i16 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <i32 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <i64 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <i128 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <isize as arbitrary::Arbitrary>::arbitrary |
318 | | |
319 | | #[inline] |
320 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
321 | 0 | let n = mem::size_of::<$ty>(); |
322 | 0 | (n, Some(n)) |
323 | 0 | } Unexecuted instantiation: <u16 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <u32 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <u64 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <u128 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <usize as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <i8 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <i16 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <i32 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <i64 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <i128 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <isize as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <u8 as arbitrary::Arbitrary>::size_hint |
324 | | |
325 | | } |
326 | | )* |
327 | | } |
328 | | } |
329 | | |
330 | | impl_arbitrary_for_integers! { |
331 | | u8: u8; |
332 | | u16: u16; |
333 | | u32: u32; |
334 | | u64: u64; |
335 | | u128: u128; |
336 | | usize: usize; |
337 | | i8: u8; |
338 | | i16: u16; |
339 | | i32: u32; |
340 | | i64: u64; |
341 | | i128: u128; |
342 | | isize: usize; |
343 | | } |
344 | | |
345 | | macro_rules! impl_arbitrary_for_floats { |
346 | | ( $( $ty:ident : $unsigned:ty; )* ) => { |
347 | | $( |
348 | | impl<'a> Arbitrary<'a> for $ty { |
349 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
350 | 0 | Ok(Self::from_bits(<$unsigned as Arbitrary<'a>>::arbitrary(u)?)) |
351 | 0 | } Unexecuted instantiation: <f32 as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <f64 as arbitrary::Arbitrary>::arbitrary |
352 | | |
353 | | #[inline] |
354 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
355 | 0 | <$unsigned as Arbitrary<'a>>::size_hint(depth) |
356 | 0 | } Unexecuted instantiation: <f32 as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <f64 as arbitrary::Arbitrary>::size_hint |
357 | | } |
358 | | )* |
359 | | } |
360 | | } |
361 | | |
362 | | impl_arbitrary_for_floats! { |
363 | | f32: u32; |
364 | | f64: u64; |
365 | | } |
366 | | |
367 | | impl<'a> Arbitrary<'a> for char { |
368 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
369 | | use std::char; |
370 | | // The highest unicode code point is 0x11_FFFF |
371 | | const CHAR_END: u32 = 0x11_0000; |
372 | | // The size of the surrogate blocks |
373 | | const SURROGATES_START: u32 = 0xD800; |
374 | 0 | let mut c = <u32 as Arbitrary<'a>>::arbitrary(u)? % CHAR_END; |
375 | 0 | if let Some(c) = char::from_u32(c) { |
376 | 0 | Ok(c) |
377 | | } else { |
378 | | // We found a surrogate, wrap and try again |
379 | 0 | c -= SURROGATES_START; |
380 | 0 | Ok(char::from_u32(c) |
381 | 0 | .expect("Generated character should be valid! This is a bug in arbitrary-rs")) |
382 | | } |
383 | 0 | } |
384 | | |
385 | | #[inline] |
386 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
387 | 0 | <u32 as Arbitrary<'a>>::size_hint(depth) |
388 | 0 | } |
389 | | } |
390 | | |
391 | | impl<'a> Arbitrary<'a> for AtomicBool { |
392 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
393 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
394 | 0 | } |
395 | | |
396 | | #[inline] |
397 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
398 | 0 | <bool as Arbitrary<'a>>::size_hint(depth) |
399 | 0 | } |
400 | | } |
401 | | |
402 | | impl<'a> Arbitrary<'a> for AtomicIsize { |
403 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
404 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
405 | 0 | } |
406 | | |
407 | | #[inline] |
408 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
409 | 0 | <isize as Arbitrary<'a>>::size_hint(depth) |
410 | 0 | } |
411 | | } |
412 | | |
413 | | impl<'a> Arbitrary<'a> for AtomicUsize { |
414 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
415 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
416 | 0 | } |
417 | | |
418 | | #[inline] |
419 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
420 | 0 | <usize as Arbitrary<'a>>::size_hint(depth) |
421 | 0 | } |
422 | | } |
423 | | |
424 | | macro_rules! impl_range { |
425 | | ( |
426 | | $range:ty, |
427 | | $value_closure:expr, |
428 | | $value_ty:ty, |
429 | | $fun:ident($fun_closure:expr), |
430 | | $size_hint_closure:expr |
431 | | ) => { |
432 | | impl<'a, A> Arbitrary<'a> for $range |
433 | | where |
434 | | A: Arbitrary<'a> + Clone + PartialOrd, |
435 | | { |
436 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
437 | 0 | let value: $value_ty = Arbitrary::arbitrary(u)?; |
438 | 0 | Ok($fun(value, $fun_closure)) |
439 | 0 | } Unexecuted instantiation: <core::ops::range::Range<_> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::ops::range::RangeFrom<_> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::ops::range::RangeInclusive<_> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::ops::range::RangeTo<_> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::ops::range::RangeToInclusive<_> as arbitrary::Arbitrary>::arbitrary |
440 | | |
441 | | #[inline] |
442 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
443 | 0 | $size_hint_closure(depth) |
444 | 0 | } Unexecuted instantiation: <core::ops::range::Range<_> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::ops::range::RangeFrom<_> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::ops::range::RangeInclusive<_> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::ops::range::RangeTo<_> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::ops::range::RangeToInclusive<_> as arbitrary::Arbitrary>::size_hint |
445 | | } |
446 | | }; |
447 | | } |
448 | | |
449 | | impl_range!( |
450 | | Range<A>, |
451 | | |r: &Range<A>| (r.start.clone(), r.end.clone()), |
452 | | (A, A), |
453 | 0 | bounded_range(|(a, b)| a..b), |
454 | 0 | |depth| crate::size_hint::and( |
455 | 0 | <A as Arbitrary>::size_hint(depth), |
456 | 0 | <A as Arbitrary>::size_hint(depth) |
457 | | ) |
458 | | ); |
459 | | impl_range!( |
460 | | RangeFrom<A>, |
461 | | |r: &RangeFrom<A>| r.start.clone(), |
462 | | A, |
463 | 0 | unbounded_range(|a| a..), |
464 | 0 | |depth| <A as Arbitrary>::size_hint(depth) |
465 | | ); |
466 | | impl_range!( |
467 | | RangeInclusive<A>, |
468 | | |r: &RangeInclusive<A>| (r.start().clone(), r.end().clone()), |
469 | | (A, A), |
470 | 0 | bounded_range(|(a, b)| a..=b), |
471 | 0 | |depth| crate::size_hint::and( |
472 | 0 | <A as Arbitrary>::size_hint(depth), |
473 | 0 | <A as Arbitrary>::size_hint(depth) |
474 | | ) |
475 | | ); |
476 | | impl_range!( |
477 | | RangeTo<A>, |
478 | | |r: &RangeTo<A>| r.end.clone(), |
479 | | A, |
480 | 0 | unbounded_range(|b| ..b), |
481 | 0 | |depth| <A as Arbitrary>::size_hint(depth) |
482 | | ); |
483 | | impl_range!( |
484 | | RangeToInclusive<A>, |
485 | | |r: &RangeToInclusive<A>| r.end.clone(), |
486 | | A, |
487 | 0 | unbounded_range(|b| ..=b), |
488 | 0 | |depth| <A as Arbitrary>::size_hint(depth) |
489 | | ); |
490 | | |
491 | 0 | pub(crate) fn bounded_range<CB, I, R>(bounds: (I, I), cb: CB) -> R |
492 | 0 | where |
493 | 0 | CB: Fn((I, I)) -> R, |
494 | 0 | I: PartialOrd, |
495 | 0 | R: RangeBounds<I>, |
496 | | { |
497 | 0 | let (mut start, mut end) = bounds; |
498 | 0 | if start > end { |
499 | 0 | mem::swap(&mut start, &mut end); |
500 | 0 | } |
501 | 0 | cb((start, end)) |
502 | 0 | } |
503 | | |
504 | 0 | pub(crate) fn unbounded_range<CB, I, R>(bound: I, cb: CB) -> R |
505 | 0 | where |
506 | 0 | CB: Fn(I) -> R, |
507 | 0 | R: RangeBounds<I>, |
508 | | { |
509 | 0 | cb(bound) |
510 | 0 | } |
511 | | |
512 | | impl<'a> Arbitrary<'a> for Duration { |
513 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
514 | 0 | Ok(Self::new( |
515 | 0 | <u64 as Arbitrary>::arbitrary(u)?, |
516 | 0 | u.int_in_range(0..=999_999_999)?, |
517 | | )) |
518 | 0 | } |
519 | | |
520 | | #[inline] |
521 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
522 | 0 | crate::size_hint::and( |
523 | 0 | <u64 as Arbitrary>::size_hint(depth), |
524 | 0 | <u32 as Arbitrary>::size_hint(depth), |
525 | | ) |
526 | 0 | } |
527 | | } |
528 | | |
529 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Option<A> { |
530 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
531 | 0 | Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? { |
532 | 0 | Some(Arbitrary::arbitrary(u)?) |
533 | | } else { |
534 | 0 | None |
535 | | }) |
536 | 0 | } |
537 | | |
538 | | #[inline] |
539 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
540 | 0 | crate::size_hint::and( |
541 | 0 | <bool as Arbitrary>::size_hint(depth), |
542 | 0 | crate::size_hint::or((0, Some(0)), <A as Arbitrary>::size_hint(depth)), |
543 | | ) |
544 | 0 | } |
545 | | } |
546 | | |
547 | | impl<'a, A: Arbitrary<'a>, B: Arbitrary<'a>> Arbitrary<'a> for std::result::Result<A, B> { |
548 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
549 | 0 | Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? { |
550 | 0 | Ok(<A as Arbitrary>::arbitrary(u)?) |
551 | | } else { |
552 | 0 | Err(<B as Arbitrary>::arbitrary(u)?) |
553 | | }) |
554 | 0 | } |
555 | | |
556 | | #[inline] |
557 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
558 | 0 | crate::size_hint::and( |
559 | 0 | <bool as Arbitrary>::size_hint(depth), |
560 | 0 | crate::size_hint::or( |
561 | 0 | <A as Arbitrary>::size_hint(depth), |
562 | 0 | <B as Arbitrary>::size_hint(depth), |
563 | | ), |
564 | | ) |
565 | 0 | } |
566 | | } |
567 | | |
568 | | macro_rules! arbitrary_tuple { |
569 | | () => {}; |
570 | | ($last: ident $($xs: ident)*) => { |
571 | | arbitrary_tuple!($($xs)*); |
572 | | |
573 | | impl<'a, $($xs: Arbitrary<'a>,)* $last: Arbitrary<'a>> Arbitrary<'a> for ($($xs,)* $last,) { |
574 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
575 | 0 | Ok(($($xs::arbitrary(u)?,)* Arbitrary::arbitrary(u)?,)) |
576 | 0 | } 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 |
577 | | |
578 | | #[allow(unused_mut, non_snake_case)] |
579 | 0 | fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<Self> { |
580 | 0 | $(let $xs = $xs::arbitrary(&mut u)?;)* |
581 | 0 | let $last = $last::arbitrary_take_rest(u)?; |
582 | 0 | Ok(($($xs,)* $last,)) |
583 | 0 | } Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_,) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::arbitrary_take_rest |
584 | | |
585 | | #[inline] |
586 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
587 | 0 | crate::size_hint::and_all(&[ |
588 | 0 | <$last as Arbitrary>::size_hint(depth), |
589 | 0 | $( <$xs as Arbitrary>::size_hint(depth) ),* |
590 | 0 | ]) |
591 | 0 | } Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_,) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as arbitrary::Arbitrary>::size_hint |
592 | | } |
593 | | }; |
594 | | } |
595 | | 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); |
596 | | |
597 | | // Helper to safely create arrays since the standard library doesn't |
598 | | // provide one yet. Shouldn't be necessary in the future. |
599 | | struct ArrayGuard<T, const N: usize> { |
600 | | dst: *mut T, |
601 | | initialized: usize, |
602 | | } |
603 | | |
604 | | impl<T, const N: usize> Drop for ArrayGuard<T, N> { |
605 | 0 | fn drop(&mut self) { |
606 | 0 | debug_assert!(self.initialized <= N); |
607 | 0 | let initialized_part = core::ptr::slice_from_raw_parts_mut(self.dst, self.initialized); |
608 | 0 | unsafe { |
609 | 0 | core::ptr::drop_in_place(initialized_part); |
610 | 0 | } |
611 | 0 | } |
612 | | } |
613 | | |
614 | 0 | fn try_create_array<F, T, const N: usize>(mut cb: F) -> Result<[T; N]> |
615 | 0 | where |
616 | 0 | F: FnMut(usize) -> Result<T>, |
617 | | { |
618 | 0 | let mut array: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninit(); |
619 | 0 | let array_ptr = array.as_mut_ptr(); |
620 | 0 | let dst = array_ptr as _; |
621 | 0 | let mut guard: ArrayGuard<T, N> = ArrayGuard { |
622 | 0 | dst, |
623 | 0 | initialized: 0, |
624 | 0 | }; |
625 | | unsafe { |
626 | 0 | for (idx, value_ptr) in (&mut *array.as_mut_ptr()).iter_mut().enumerate() { |
627 | 0 | core::ptr::write(value_ptr, cb(idx)?); |
628 | 0 | guard.initialized += 1; |
629 | | } |
630 | 0 | mem::forget(guard); |
631 | 0 | Ok(array.assume_init()) |
632 | | } |
633 | 0 | } |
634 | | |
635 | | impl<'a, T, const N: usize> Arbitrary<'a> for [T; N] |
636 | | where |
637 | | T: Arbitrary<'a>, |
638 | | { |
639 | | #[inline] |
640 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
641 | 0 | try_create_array(|_| <T as Arbitrary<'a>>::arbitrary(u)) |
642 | 0 | } |
643 | | |
644 | | #[inline] |
645 | 0 | fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<Self> { |
646 | 0 | let mut array = Self::arbitrary(&mut u)?; |
647 | 0 | if let Some(last) = array.last_mut() { |
648 | 0 | *last = Arbitrary::arbitrary_take_rest(u)?; |
649 | 0 | } |
650 | 0 | Ok(array) |
651 | 0 | } |
652 | | |
653 | | #[inline] |
654 | 0 | fn size_hint(d: usize) -> (usize, Option<usize>) { |
655 | 0 | crate::size_hint::and_all(&array::from_fn::<_, N, _>(|_| { |
656 | 0 | <T as Arbitrary>::size_hint(d) |
657 | 0 | })) |
658 | 0 | } |
659 | | } |
660 | | |
661 | | impl<'a> Arbitrary<'a> for &'a [u8] { |
662 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
663 | 0 | let len = u.arbitrary_len::<u8>()?; |
664 | 0 | u.bytes(len) |
665 | 0 | } |
666 | | |
667 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
668 | 0 | Ok(u.take_rest()) |
669 | 0 | } |
670 | | |
671 | | #[inline] |
672 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
673 | 0 | (0, None) |
674 | 0 | } |
675 | | } |
676 | | |
677 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> { |
678 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
679 | 0 | u.arbitrary_iter()?.collect() |
680 | 0 | } |
681 | | |
682 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
683 | 0 | u.arbitrary_take_rest_iter()?.collect() |
684 | 0 | } |
685 | | |
686 | | #[inline] |
687 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
688 | 0 | (0, None) |
689 | 0 | } |
690 | | } |
691 | | |
692 | | impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K, V> { |
693 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
694 | 0 | u.arbitrary_iter()?.collect() |
695 | 0 | } |
696 | | |
697 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
698 | 0 | u.arbitrary_take_rest_iter()?.collect() |
699 | 0 | } |
700 | | |
701 | | #[inline] |
702 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
703 | 0 | (0, None) |
704 | 0 | } |
705 | | } |
706 | | |
707 | | impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BTreeSet<A> { |
708 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
709 | 0 | u.arbitrary_iter()?.collect() |
710 | 0 | } |
711 | | |
712 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
713 | 0 | u.arbitrary_take_rest_iter()?.collect() |
714 | 0 | } |
715 | | |
716 | | #[inline] |
717 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
718 | 0 | (0, None) |
719 | 0 | } |
720 | | } |
721 | | |
722 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Bound<A> { |
723 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
724 | 0 | match u.int_in_range::<u8>(0..=2)? { |
725 | 0 | 0 => Ok(Bound::Included(A::arbitrary(u)?)), |
726 | 0 | 1 => Ok(Bound::Excluded(A::arbitrary(u)?)), |
727 | 0 | 2 => Ok(Bound::Unbounded), |
728 | 0 | _ => unreachable!(), |
729 | | } |
730 | 0 | } |
731 | | |
732 | | #[inline] |
733 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
734 | 0 | size_hint::or( |
735 | 0 | size_hint::and((1, Some(1)), A::size_hint(depth)), |
736 | 0 | (1, Some(1)), |
737 | | ) |
738 | 0 | } |
739 | | } |
740 | | |
741 | | impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> { |
742 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
743 | 0 | u.arbitrary_iter()?.collect() |
744 | 0 | } |
745 | | |
746 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
747 | 0 | u.arbitrary_take_rest_iter()?.collect() |
748 | 0 | } |
749 | | |
750 | | #[inline] |
751 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
752 | 0 | (0, None) |
753 | 0 | } |
754 | | } |
755 | | |
756 | | impl<'a, K: Arbitrary<'a> + Eq + ::std::hash::Hash, V: Arbitrary<'a>, S: BuildHasher + Default> |
757 | | Arbitrary<'a> for HashMap<K, V, S> |
758 | | { |
759 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
760 | 0 | u.arbitrary_iter()?.collect() |
761 | 0 | } |
762 | | |
763 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
764 | 0 | u.arbitrary_take_rest_iter()?.collect() |
765 | 0 | } |
766 | | |
767 | | #[inline] |
768 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
769 | 0 | (0, None) |
770 | 0 | } |
771 | | } |
772 | | |
773 | | impl<'a, A: Arbitrary<'a> + Eq + ::std::hash::Hash, S: BuildHasher + Default> Arbitrary<'a> |
774 | | for HashSet<A, S> |
775 | | { |
776 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
777 | 0 | u.arbitrary_iter()?.collect() |
778 | 0 | } |
779 | | |
780 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
781 | 0 | u.arbitrary_take_rest_iter()?.collect() |
782 | 0 | } |
783 | | |
784 | | #[inline] |
785 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
786 | 0 | (0, None) |
787 | 0 | } |
788 | | } |
789 | | |
790 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> { |
791 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
792 | 0 | u.arbitrary_iter()?.collect() |
793 | 0 | } |
794 | | |
795 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
796 | 0 | u.arbitrary_take_rest_iter()?.collect() |
797 | 0 | } |
798 | | |
799 | | #[inline] |
800 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
801 | 0 | (0, None) |
802 | 0 | } |
803 | | } |
804 | | |
805 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> { |
806 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
807 | 0 | u.arbitrary_iter()?.collect() |
808 | 0 | } |
809 | | |
810 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
811 | 0 | u.arbitrary_take_rest_iter()?.collect() |
812 | 0 | } |
813 | | |
814 | | #[inline] |
815 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
816 | 0 | (0, None) |
817 | 0 | } |
818 | | } |
819 | | |
820 | | impl<'a, A> Arbitrary<'a> for Cow<'a, A> |
821 | | where |
822 | | A: ToOwned + ?Sized, |
823 | | <A as ToOwned>::Owned: Arbitrary<'a>, |
824 | | { |
825 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
826 | 0 | Arbitrary::arbitrary(u).map(Cow::Owned) |
827 | 0 | } |
828 | | |
829 | | #[inline] |
830 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
831 | 0 | crate::size_hint::recursion_guard(depth, |depth| { |
832 | 0 | <<A as ToOwned>::Owned as Arbitrary>::size_hint(depth) |
833 | 0 | }) |
834 | 0 | } |
835 | | } |
836 | | |
837 | | impl<'a> Arbitrary<'a> for &'a str { |
838 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
839 | 0 | let size = u.arbitrary_len::<u8>()?; |
840 | 0 | match str::from_utf8(&u.peek_bytes(size).unwrap()) { |
841 | 0 | Ok(s) => { |
842 | 0 | u.bytes(size).unwrap(); |
843 | 0 | Ok(s) |
844 | | } |
845 | 0 | Err(e) => { |
846 | 0 | let i = e.valid_up_to(); |
847 | 0 | let valid = u.bytes(i).unwrap(); |
848 | 0 | let s = unsafe { |
849 | 0 | debug_assert!(str::from_utf8(valid).is_ok()); |
850 | 0 | str::from_utf8_unchecked(valid) |
851 | | }; |
852 | 0 | Ok(s) |
853 | | } |
854 | | } |
855 | 0 | } |
856 | | |
857 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
858 | 0 | let bytes = u.take_rest(); |
859 | 0 | str::from_utf8(bytes) |
860 | 0 | .map_err(|_| Error::IncorrectFormat) |
861 | 0 | .map(Into::into) |
862 | 0 | } |
863 | | |
864 | | #[inline] |
865 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
866 | 0 | (0, None) |
867 | 0 | } |
868 | | } |
869 | | |
870 | | impl<'a> Arbitrary<'a> for String { |
871 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
872 | 0 | <&str as Arbitrary>::arbitrary(u).map(Into::into) |
873 | 0 | } |
874 | | |
875 | 0 | fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
876 | 0 | <&str as Arbitrary>::arbitrary_take_rest(u).map(Into::into) |
877 | 0 | } |
878 | | |
879 | | #[inline] |
880 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
881 | 0 | <&str as Arbitrary>::size_hint(depth) |
882 | 0 | } |
883 | | } |
884 | | |
885 | | impl<'a> Arbitrary<'a> for CString { |
886 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
887 | 0 | <Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| { |
888 | 0 | x.retain(|&c| c != 0); |
889 | 0 | Self::new(x).unwrap() |
890 | 0 | }) |
891 | 0 | } |
892 | | |
893 | | #[inline] |
894 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
895 | 0 | <Vec<u8> as Arbitrary>::size_hint(depth) |
896 | 0 | } |
897 | | } |
898 | | |
899 | | impl<'a> Arbitrary<'a> for OsString { |
900 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
901 | 0 | <String as Arbitrary>::arbitrary(u).map(From::from) |
902 | 0 | } |
903 | | |
904 | | #[inline] |
905 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
906 | 0 | <String as Arbitrary>::size_hint(depth) |
907 | 0 | } |
908 | | } |
909 | | |
910 | | impl<'a> Arbitrary<'a> for PathBuf { |
911 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
912 | 0 | <OsString as Arbitrary>::arbitrary(u).map(From::from) |
913 | 0 | } |
914 | | |
915 | | #[inline] |
916 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
917 | 0 | <OsString as Arbitrary>::size_hint(depth) |
918 | 0 | } |
919 | | } |
920 | | |
921 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> { |
922 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
923 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
924 | 0 | } |
925 | | |
926 | | #[inline] |
927 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
928 | 0 | crate::size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint) |
929 | 0 | } |
930 | | } |
931 | | |
932 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> { |
933 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
934 | 0 | <Vec<A> as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_slice()) |
935 | 0 | } |
936 | | |
937 | | #[inline] |
938 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
939 | 0 | <Vec<A> as Arbitrary>::size_hint(depth) |
940 | 0 | } |
941 | | } |
942 | | |
943 | | impl<'a> Arbitrary<'a> for Box<str> { |
944 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
945 | 0 | <String as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_str()) |
946 | 0 | } |
947 | | |
948 | | #[inline] |
949 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
950 | 0 | <String as Arbitrary>::size_hint(depth) |
951 | 0 | } |
952 | | } |
953 | | |
954 | | // impl Arbitrary for Box<CStr> { |
955 | | // fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> { |
956 | | // <CString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_c_str()) |
957 | | // } |
958 | | // } |
959 | | |
960 | | // impl Arbitrary for Box<OsStr> { |
961 | | // fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> { |
962 | | // <OsString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_osstr()) |
963 | | // |
964 | | // } |
965 | | // } |
966 | | |
967 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> { |
968 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
969 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
970 | 0 | } |
971 | | |
972 | | #[inline] |
973 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
974 | 0 | crate::size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint) |
975 | 0 | } |
976 | | } |
977 | | |
978 | | impl<'a> Arbitrary<'a> for Arc<str> { |
979 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
980 | 0 | <&str as Arbitrary>::arbitrary(u).map(Into::into) |
981 | 0 | } |
982 | | |
983 | | #[inline] |
984 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
985 | 0 | <&str as Arbitrary>::size_hint(depth) |
986 | 0 | } |
987 | | } |
988 | | |
989 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> { |
990 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
991 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
992 | 0 | } |
993 | | |
994 | | #[inline] |
995 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
996 | 0 | crate::size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint) |
997 | 0 | } |
998 | | } |
999 | | |
1000 | | impl<'a> Arbitrary<'a> for Rc<str> { |
1001 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
1002 | 0 | <&str as Arbitrary>::arbitrary(u).map(Into::into) |
1003 | 0 | } |
1004 | | |
1005 | | #[inline] |
1006 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
1007 | 0 | <&str as Arbitrary>::size_hint(depth) |
1008 | 0 | } |
1009 | | } |
1010 | | |
1011 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Cell<A> { |
1012 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
1013 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
1014 | 0 | } |
1015 | | |
1016 | | #[inline] |
1017 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
1018 | 0 | <A as Arbitrary<'a>>::size_hint(depth) |
1019 | 0 | } |
1020 | | } |
1021 | | |
1022 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for RefCell<A> { |
1023 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
1024 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
1025 | 0 | } |
1026 | | |
1027 | | #[inline] |
1028 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
1029 | 0 | <A as Arbitrary<'a>>::size_hint(depth) |
1030 | 0 | } |
1031 | | } |
1032 | | |
1033 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for UnsafeCell<A> { |
1034 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
1035 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
1036 | 0 | } |
1037 | | |
1038 | | #[inline] |
1039 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
1040 | 0 | <A as Arbitrary<'a>>::size_hint(depth) |
1041 | 0 | } |
1042 | | } |
1043 | | |
1044 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Mutex<A> { |
1045 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
1046 | 0 | Arbitrary::arbitrary(u).map(Self::new) |
1047 | 0 | } |
1048 | | |
1049 | | #[inline] |
1050 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
1051 | 0 | <A as Arbitrary<'a>>::size_hint(depth) |
1052 | 0 | } |
1053 | | } |
1054 | | |
1055 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for iter::Empty<A> { |
1056 | 0 | fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> { |
1057 | 0 | Ok(iter::empty()) |
1058 | 0 | } |
1059 | | |
1060 | | #[inline] |
1061 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
1062 | 0 | (0, Some(0)) |
1063 | 0 | } |
1064 | | } |
1065 | | |
1066 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::marker::PhantomData<A> { |
1067 | 0 | fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> { |
1068 | 0 | Ok(::std::marker::PhantomData) |
1069 | 0 | } |
1070 | | |
1071 | | #[inline] |
1072 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
1073 | 0 | (0, Some(0)) |
1074 | 0 | } |
1075 | | } |
1076 | | |
1077 | | impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::num::Wrapping<A> { |
1078 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
1079 | 0 | Arbitrary::arbitrary(u).map(::std::num::Wrapping) |
1080 | 0 | } |
1081 | | |
1082 | | #[inline] |
1083 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
1084 | 0 | <A as Arbitrary<'a>>::size_hint(depth) |
1085 | 0 | } |
1086 | | } |
1087 | | |
1088 | | macro_rules! implement_nonzero_int { |
1089 | | ($nonzero:ty, $int:ty) => { |
1090 | | impl<'a> Arbitrary<'a> for $nonzero { |
1091 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
1092 | 0 | match Self::new(<$int as Arbitrary<'a>>::arbitrary(u)?) { |
1093 | 0 | Some(n) => Ok(n), |
1094 | 0 | None => Err(Error::IncorrectFormat), |
1095 | | } |
1096 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<i8> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<i16> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<i32> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<i64> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<i128> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<isize> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as arbitrary::Arbitrary>::arbitrary Unexecuted instantiation: <core::num::nonzero::NonZero<usize> as arbitrary::Arbitrary>::arbitrary |
1097 | | |
1098 | | #[inline] |
1099 | 0 | fn size_hint(depth: usize) -> (usize, Option<usize>) { |
1100 | 0 | <$int as Arbitrary<'a>>::size_hint(depth) |
1101 | 0 | } Unexecuted instantiation: <core::num::nonzero::NonZero<i8> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<i16> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<i32> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<i64> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<i128> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<isize> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<u8> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<u16> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<u32> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<u64> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<u128> as arbitrary::Arbitrary>::size_hint Unexecuted instantiation: <core::num::nonzero::NonZero<usize> as arbitrary::Arbitrary>::size_hint |
1102 | | } |
1103 | | }; |
1104 | | } |
1105 | | |
1106 | | implement_nonzero_int! { NonZeroI8, i8 } |
1107 | | implement_nonzero_int! { NonZeroI16, i16 } |
1108 | | implement_nonzero_int! { NonZeroI32, i32 } |
1109 | | implement_nonzero_int! { NonZeroI64, i64 } |
1110 | | implement_nonzero_int! { NonZeroI128, i128 } |
1111 | | implement_nonzero_int! { NonZeroIsize, isize } |
1112 | | implement_nonzero_int! { NonZeroU8, u8 } |
1113 | | implement_nonzero_int! { NonZeroU16, u16 } |
1114 | | implement_nonzero_int! { NonZeroU32, u32 } |
1115 | | implement_nonzero_int! { NonZeroU64, u64 } |
1116 | | implement_nonzero_int! { NonZeroU128, u128 } |
1117 | | implement_nonzero_int! { NonZeroUsize, usize } |
1118 | | |
1119 | | impl<'a> Arbitrary<'a> for Ipv4Addr { |
1120 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
1121 | 0 | Ok(Ipv4Addr::from(u32::arbitrary(u)?)) |
1122 | 0 | } |
1123 | | |
1124 | | #[inline] |
1125 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
1126 | 0 | (4, Some(4)) |
1127 | 0 | } |
1128 | | } |
1129 | | |
1130 | | impl<'a> Arbitrary<'a> for Ipv6Addr { |
1131 | 0 | fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
1132 | 0 | Ok(Ipv6Addr::from(u128::arbitrary(u)?)) |
1133 | 0 | } |
1134 | | |
1135 | | #[inline] |
1136 | 0 | fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
1137 | 0 | (16, Some(16)) |
1138 | 0 | } |
1139 | | } |
1140 | | |
1141 | | #[cfg(test)] |
1142 | | mod test { |
1143 | | use super::*; |
1144 | | |
1145 | | /// Generates an arbitrary `T`, and checks that the result is consistent with the |
1146 | | /// `size_hint()` reported by `T`. |
1147 | | fn checked_arbitrary<'a, T: Arbitrary<'a>>(u: &mut Unstructured<'a>) -> Result<T> { |
1148 | | let (min, max) = T::size_hint(0); |
1149 | | |
1150 | | let len_before = u.len(); |
1151 | | let result = T::arbitrary(u); |
1152 | | |
1153 | | let consumed = len_before - u.len(); |
1154 | | |
1155 | | if let Some(max) = max { |
1156 | | assert!( |
1157 | | consumed <= max, |
1158 | | "incorrect maximum size: indicated {}, actually consumed {}", |
1159 | | max, |
1160 | | consumed |
1161 | | ); |
1162 | | } |
1163 | | |
1164 | | if result.is_ok() { |
1165 | | assert!( |
1166 | | consumed >= min, |
1167 | | "incorrect minimum size: indicated {}, actually consumed {}", |
1168 | | min, |
1169 | | consumed |
1170 | | ); |
1171 | | } |
1172 | | |
1173 | | result |
1174 | | } |
1175 | | |
1176 | | /// Like `checked_arbitrary()`, but calls `arbitrary_take_rest()` instead of `arbitrary()`. |
1177 | | fn checked_arbitrary_take_rest<'a, T: Arbitrary<'a>>(u: Unstructured<'a>) -> Result<T> { |
1178 | | let (min, _) = T::size_hint(0); |
1179 | | |
1180 | | let len_before = u.len(); |
1181 | | let result = T::arbitrary_take_rest(u); |
1182 | | |
1183 | | if result.is_ok() { |
1184 | | assert!( |
1185 | | len_before >= min, |
1186 | | "incorrect minimum size: indicated {}, worked with {}", |
1187 | | min, |
1188 | | len_before |
1189 | | ); |
1190 | | } |
1191 | | |
1192 | | result |
1193 | | } |
1194 | | |
1195 | | #[test] |
1196 | | fn finite_buffer_fill_buffer() { |
1197 | | let x = [1, 2, 3, 4]; |
1198 | | let mut rb = Unstructured::new(&x); |
1199 | | let mut z = [0; 2]; |
1200 | | rb.fill_buffer(&mut z).unwrap(); |
1201 | | assert_eq!(z, [1, 2]); |
1202 | | rb.fill_buffer(&mut z).unwrap(); |
1203 | | assert_eq!(z, [3, 4]); |
1204 | | rb.fill_buffer(&mut z).unwrap(); |
1205 | | assert_eq!(z, [0, 0]); |
1206 | | } |
1207 | | |
1208 | | #[test] |
1209 | | fn arbitrary_for_integers() { |
1210 | | let x = [1, 2, 3, 4]; |
1211 | | let mut buf = Unstructured::new(&x); |
1212 | | let expected = 1 | (2 << 8) | (3 << 16) | (4 << 24); |
1213 | | let actual = checked_arbitrary::<i32>(&mut buf).unwrap(); |
1214 | | assert_eq!(expected, actual); |
1215 | | } |
1216 | | |
1217 | | #[test] |
1218 | | fn arbitrary_for_bytes() { |
1219 | | let x = [1, 2, 3, 4, 4]; |
1220 | | let mut buf = Unstructured::new(&x); |
1221 | | let expected = &[1, 2, 3, 4]; |
1222 | | let actual = checked_arbitrary::<&[u8]>(&mut buf).unwrap(); |
1223 | | assert_eq!(expected, actual); |
1224 | | } |
1225 | | |
1226 | | #[test] |
1227 | | fn arbitrary_take_rest_for_bytes() { |
1228 | | let x = [1, 2, 3, 4]; |
1229 | | let buf = Unstructured::new(&x); |
1230 | | let expected = &[1, 2, 3, 4]; |
1231 | | let actual = checked_arbitrary_take_rest::<&[u8]>(buf).unwrap(); |
1232 | | assert_eq!(expected, actual); |
1233 | | } |
1234 | | |
1235 | | #[test] |
1236 | | fn arbitrary_collection() { |
1237 | | let x = [ |
1238 | | 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 12, |
1239 | | ]; |
1240 | | assert_eq!( |
1241 | | checked_arbitrary::<&[u8]>(&mut Unstructured::new(&x)).unwrap(), |
1242 | | &[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3] |
1243 | | ); |
1244 | | assert_eq!( |
1245 | | checked_arbitrary::<Vec<u8>>(&mut Unstructured::new(&x)).unwrap(), |
1246 | | &[2, 4, 6, 8, 1] |
1247 | | ); |
1248 | | assert_eq!( |
1249 | | checked_arbitrary::<Vec<u32>>(&mut Unstructured::new(&x)).unwrap(), |
1250 | | &[84148994] |
1251 | | ); |
1252 | | assert_eq!( |
1253 | | checked_arbitrary::<String>(&mut Unstructured::new(&x)).unwrap(), |
1254 | | "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03" |
1255 | | ); |
1256 | | } |
1257 | | |
1258 | | #[test] |
1259 | | fn arbitrary_take_rest() { |
1260 | | let x = [1, 2, 3, 4]; |
1261 | | assert_eq!( |
1262 | | checked_arbitrary_take_rest::<&[u8]>(Unstructured::new(&x)).unwrap(), |
1263 | | &[1, 2, 3, 4] |
1264 | | ); |
1265 | | assert_eq!( |
1266 | | checked_arbitrary_take_rest::<Vec<u8>>(Unstructured::new(&x)).unwrap(), |
1267 | | &[1, 2, 3, 4] |
1268 | | ); |
1269 | | assert_eq!( |
1270 | | checked_arbitrary_take_rest::<Vec<u32>>(Unstructured::new(&x)).unwrap(), |
1271 | | &[0x4030201] |
1272 | | ); |
1273 | | assert_eq!( |
1274 | | checked_arbitrary_take_rest::<String>(Unstructured::new(&x)).unwrap(), |
1275 | | "\x01\x02\x03\x04" |
1276 | | ); |
1277 | | |
1278 | | assert_eq!( |
1279 | | checked_arbitrary_take_rest::<&[u8]>(Unstructured::new(&[])).unwrap(), |
1280 | | &[] |
1281 | | ); |
1282 | | assert_eq!( |
1283 | | checked_arbitrary_take_rest::<Vec<u8>>(Unstructured::new(&[])).unwrap(), |
1284 | | &[] |
1285 | | ); |
1286 | | } |
1287 | | |
1288 | | #[test] |
1289 | | fn size_hint_for_tuples() { |
1290 | | assert_eq!( |
1291 | | (7, Some(7)), |
1292 | | <(bool, u16, i32) as Arbitrary<'_>>::size_hint(0) |
1293 | | ); |
1294 | | assert_eq!((1, None), <(u8, Vec<u8>) as Arbitrary>::size_hint(0)); |
1295 | | } |
1296 | | } |