Coverage Report

Created: 2025-05-08 06:13

/rust/registry/src/index.crates.io-6f17d22bba15001f/arbitrary-1.0.0/src/lib.rs
Line
Count
Source (jump to first uncovered line)
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::cell::{Cell, RefCell, UnsafeCell};
38
use core::iter;
39
use core::mem;
40
use core::ops::{Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive};
41
use core::str;
42
use core::time::Duration;
43
use std::borrow::{Cow, ToOwned};
44
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
45
use std::ffi::{CString, OsString};
46
use std::path::PathBuf;
47
use std::rc::Rc;
48
use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize};
49
use std::sync::{Arc, Mutex};
50
51
/// Generate arbitrary structured values from raw, unstructured data.
52
///
53
/// The `Arbitrary` trait allows you to generate valid structured values, like
54
/// `HashMap`s, or ASTs, or `MyTomlConfig`, or any other data structure from
55
/// raw, unstructured bytes provided by a fuzzer.
56
///
57
/// # Deriving `Arbitrary`
58
///
59
/// Automatically deriving the `Arbitrary` trait is the recommended way to
60
/// implement `Arbitrary` for your types.
61
///
62
/// Using the custom derive requires that you enable the `"derive"` cargo
63
/// feature in your `Cargo.toml`:
64
///
65
/// ```toml
66
/// [dependencies]
67
/// arbitrary = { version = "1", features = ["derive"] }
68
/// ```
69
///
70
/// Then, you add the `#[derive(Arbitrary)]` annotation to your `struct` or
71
/// `enum` type definition:
72
///
73
/// ```
74
/// # #[cfg(feature = "derive")] mod foo {
75
/// use arbitrary::Arbitrary;
76
/// use std::collections::HashSet;
77
///
78
/// #[derive(Arbitrary)]
79
/// pub struct AddressBook {
80
///     friends: HashSet<Friend>,
81
/// }
82
///
83
/// #[derive(Arbitrary, Hash, Eq, PartialEq)]
84
/// pub enum Friend {
85
///     Buddy { name: String },
86
///     Pal { age: usize },
87
/// }
88
/// # }
89
/// ```
90
///
91
/// Every member of the `struct` or `enum` must also implement `Arbitrary`.
92
///
93
/// # Implementing `Arbitrary` By Hand
94
///
95
/// Implementing `Arbitrary` mostly involves nested calls to other `Arbitrary`
96
/// arbitrary implementations for each of your `struct` or `enum`'s members. But
97
/// sometimes you need some amount of raw data, or you need to generate a
98
/// variably-sized collection type, or you something of that sort. The
99
/// [`Unstructured`][crate::Unstructured] type helps you with these tasks.
100
///
101
/// ```
102
/// # #[cfg(feature = "derive")] mod foo {
103
/// # pub struct MyCollection<T> { _t: std::marker::PhantomData<T> }
104
/// # impl<T> MyCollection<T> {
105
/// #     pub fn new() -> Self { MyCollection { _t: std::marker::PhantomData } }
106
/// #     pub fn insert(&mut self, element: T) {}
107
/// # }
108
/// use arbitrary::{Arbitrary, Result, Unstructured};
109
///
110
/// impl<'a, T> Arbitrary<'a> for MyCollection<T>
111
/// where
112
///     T: Arbitrary<'a>,
113
/// {
114
///     fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
115
///         // Get an iterator of arbitrary `T`s.
116
///         let iter = u.arbitrary_iter::<T>()?;
117
///
118
///         // And then create a collection!
119
///         let mut my_collection = MyCollection::new();
120
///         for elem_result in iter {
121
///             let elem = elem_result?;
122
///             my_collection.insert(elem);
123
///         }
124
///
125
///         Ok(my_collection)
126
///     }
127
/// }
128
/// # }
129
/// ```
130
pub trait Arbitrary<'a>: Sized {
131
    /// Generate an arbitrary value of `Self` from the given unstructured data.
132
    ///
133
    /// Calling `Arbitrary::arbitrary` requires that you have some raw data,
134
    /// perhaps given to you by a fuzzer like AFL or libFuzzer. You wrap this
135
    /// raw data in an `Unstructured`, and then you can call `<MyType as
136
    /// Arbitrary>::arbitrary` to construct an arbitrary instance of `MyType`
137
    /// from that unstuctured data.
138
    ///
139
    /// Implementation may return an error if there is not enough data to
140
    /// construct a full instance of `Self`. This is generally OK: it is better
141
    /// to exit early and get the fuzzer to provide more input data, than it is
142
    /// to generate default values in place of the missing data, which would
143
    /// bias the distribution of generated values, and ultimately make fuzzing
144
    /// less efficient.
145
    ///
146
    /// ```
147
    /// # #[cfg(feature = "derive")] fn foo() {
148
    /// use arbitrary::{Arbitrary, Unstructured};
149
    ///
150
    /// #[derive(Arbitrary)]
151
    /// pub struct MyType {
152
    ///     // ...
153
    /// }
154
    ///
155
    /// // Get the raw data from the fuzzer or wherever else.
156
    /// # let get_raw_data_from_fuzzer = || &[];
157
    /// let raw_data: &[u8] = get_raw_data_from_fuzzer();
158
    ///
159
    /// // Wrap that raw data in an `Unstructured`.
160
    /// let mut unstructured = Unstructured::new(raw_data);
161
    ///
162
    /// // Generate an arbitrary instance of `MyType` and do stuff with it.
163
    /// if let Ok(value) = MyType::arbitrary(&mut unstructured) {
164
    /// #   let do_stuff = |_| {};
165
    ///     do_stuff(value);
166
    /// }
167
    /// # }
168
    /// ```
169
    ///
170
    /// See also the documentation for [`Unstructured`][crate::Unstructured].
171
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>;
172
173
    /// Generate an arbitrary value of `Self` from the entirety of the given unstructured data.
174
    ///
175
    /// This is similar to Arbitrary::arbitrary, however it assumes that it is the
176
    /// last consumer of the given data, and is thus able to consume it all if it needs.
177
    /// See also the documentation for [`Unstructured`][crate::Unstructured].
178
0
    fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<Self> {
179
0
        Self::arbitrary(&mut u)
180
0
    }
181
182
    /// Get a size hint for how many bytes out of an `Unstructured` this type
183
    /// needs to construct itself.
184
    ///
185
    /// This is useful for determining how many elements we should insert when
186
    /// creating an arbitrary collection.
187
    ///
188
    /// The return value is similar to
189
    /// [`Iterator::size_hint`][iterator-size-hint]: it returns a tuple where
190
    /// the first element is a lower bound on the number of bytes required, and
191
    /// the second element is an optional upper bound.
192
    ///
193
    /// The default implementation return `(0, None)` which is correct for any
194
    /// type, but not ultimately that useful. Using `#[derive(Arbitrary)]` will
195
    /// create a better implementation. If you are writing an `Arbitrary`
196
    /// implementation by hand, and your type can be part of a dynamically sized
197
    /// collection (such as `Vec`), you are strongly encouraged to override this
198
    /// default with a better implementation. The
199
    /// [`size_hint`][crate::size_hint] module will help with this task.
200
    ///
201
    /// ## The `depth` Parameter
202
    ///
203
    /// If you 100% know that the type you are implementing `Arbitrary` for is
204
    /// not a recursive type, or your implementation is not transitively calling
205
    /// any other `size_hint` methods, you can ignore the `depth` parameter.
206
    /// Note that if you are implementing `Arbitrary` for a generic type, you
207
    /// cannot guarantee the lack of type recrusion!
208
    ///
209
    /// Otherwise, you need to use
210
    /// [`arbitrary::size_hint::recursion_guard(depth)`][crate::size_hint::recursion_guard]
211
    /// to prevent potential infinite recursion when calculating size hints for
212
    /// potentially recursive types:
213
    ///
214
    /// ```
215
    /// use arbitrary::{Arbitrary, Unstructured, size_hint};
216
    ///
217
    /// // This can potentially be a recursive type if `L` or `R` contain
218
    /// // something like `Box<Option<MyEither<L, R>>>`!
219
    /// enum MyEither<L, R> {
220
    ///     Left(L),
221
    ///     Right(R),
222
    /// }
223
    ///
224
    /// impl<'a, L, R> Arbitrary<'a> for MyEither<L, R>
225
    /// where
226
    ///     L: Arbitrary<'a>,
227
    ///     R: Arbitrary<'a>,
228
    /// {
229
    ///     fn arbitrary(u: &mut Unstructured) -> arbitrary::Result<Self> {
230
    ///         // ...
231
    /// #       unimplemented!()
232
    ///     }
233
    ///
234
    ///     fn size_hint(depth: usize) -> (usize, Option<usize>) {
235
    ///         // Protect against potential infinite recursion with
236
    ///         // `recursion_guard`.
237
    ///         size_hint::recursion_guard(depth, |depth| {
238
    ///             // If we aren't too deep, then `recursion_guard` calls
239
    ///             // this closure, which implements the natural size hint.
240
    ///             // Don't forget to use the new `depth` in all nested
241
    ///             // `size_hint` calls! We recommend shadowing the
242
    ///             // parameter, like what is done here, so that you can't
243
    ///             // accidentally use the wrong depth.
244
    ///             size_hint::or(
245
    ///                 <L as Arbitrary>::size_hint(depth),
246
    ///                 <R as Arbitrary>::size_hint(depth),
247
    ///             )
248
    ///         })
249
    ///     }
250
    /// }
251
    /// ```
252
    ///
253
    /// [iterator-size-hint]: https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.size_hint
254
    #[inline]
255
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
256
0
        let _ = depth;
257
0
        (0, None)
258
0
    }
259
}
260
261
impl<'a> Arbitrary<'a> for () {
262
0
    fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> {
263
0
        Ok(())
264
0
    }
265
266
    #[inline]
267
0
    fn size_hint(_depth: usize) -> (usize, Option<usize>) {
268
0
        (0, Some(0))
269
0
    }
270
}
271
272
impl<'a> Arbitrary<'a> for bool {
273
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
274
0
        Ok(<u8 as Arbitrary<'a>>::arbitrary(u)? & 1 == 1)
275
0
    }
276
277
    #[inline]
278
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
279
0
        <u8 as Arbitrary<'a>>::size_hint(depth)
280
0
    }
281
}
282
283
macro_rules! impl_arbitrary_for_integers {
284
    ( $( $ty:ty: $unsigned:ty; )* ) => {
285
        $(
286
            impl<'a> Arbitrary<'a> for $ty {
287
0
                fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
288
0
                    let mut buf = [0; mem::size_of::<$ty>()];
289
0
                    u.fill_buffer(&mut buf)?;
290
0
                    let mut x: $unsigned = 0;
291
0
                    for i in 0..mem::size_of::<$ty>() {
292
0
                        x |= buf[i] as $unsigned << (i * 8);
293
0
                    }
294
0
                    Ok(x as $ty)
295
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
296
297
                #[inline]
298
7.51k
                fn size_hint(_depth: usize) -> (usize, Option<usize>) {
299
7.51k
                    let n = mem::size_of::<$ty>();
300
7.51k
                    (n, Some(n))
301
7.51k
                }
<usize as arbitrary::Arbitrary>::size_hint
Line
Count
Source
298
7.51k
                fn size_hint(_depth: usize) -> (usize, Option<usize>) {
299
7.51k
                    let n = mem::size_of::<$ty>();
300
7.51k
                    (n, Some(n))
301
7.51k
                }
Unexecuted instantiation: <u8 as arbitrary::Arbitrary>::size_hint
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
302
303
            }
304
        )*
305
    }
306
}
307
308
impl_arbitrary_for_integers! {
309
    u8: u8;
310
    u16: u16;
311
    u32: u32;
312
    u64: u64;
313
    u128: u128;
314
    usize: usize;
315
    i8: u8;
316
    i16: u16;
317
    i32: u32;
318
    i64: u64;
319
    i128: u128;
320
    isize: usize;
321
}
322
323
macro_rules! impl_arbitrary_for_floats {
324
    ( $( $ty:ident : $unsigned:ty; )* ) => {
325
        $(
326
            impl<'a> Arbitrary<'a> for $ty {
327
0
                fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
328
0
                    Ok(Self::from_bits(<$unsigned as Arbitrary<'a>>::arbitrary(u)?))
329
0
                }
Unexecuted instantiation: <f32 as arbitrary::Arbitrary>::arbitrary
Unexecuted instantiation: <f64 as arbitrary::Arbitrary>::arbitrary
330
331
                #[inline]
332
0
                fn size_hint(depth: usize) -> (usize, Option<usize>) {
333
0
                    <$unsigned as Arbitrary<'a>>::size_hint(depth)
334
0
                }
Unexecuted instantiation: <f32 as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <f64 as arbitrary::Arbitrary>::size_hint
335
            }
336
        )*
337
    }
338
}
339
340
impl_arbitrary_for_floats! {
341
    f32: u32;
342
    f64: u64;
343
}
344
345
impl<'a> Arbitrary<'a> for char {
346
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
347
        use std::char;
348
        const CHAR_END: u32 = 0x0011_000;
349
        // The size of the surrogate blocks
350
        const SURROGATES_START: u32 = 0xD800;
351
0
        let mut c = <u32 as Arbitrary<'a>>::arbitrary(u)? % CHAR_END;
352
0
        if let Some(c) = char::from_u32(c) {
353
0
            return Ok(c);
354
        } else {
355
            // We found a surrogate, wrap and try again
356
0
            c -= SURROGATES_START;
357
0
            Ok(char::from_u32(c)
358
0
                .expect("Generated character should be valid! This is a bug in arbitrary-rs"))
359
        }
360
0
    }
361
362
    #[inline]
363
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
364
0
        <u32 as Arbitrary<'a>>::size_hint(depth)
365
0
    }
366
}
367
368
impl<'a> Arbitrary<'a> for AtomicBool {
369
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
370
0
        Arbitrary::arbitrary(u).map(Self::new)
371
0
    }
372
373
    #[inline]
374
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
375
0
        <bool as Arbitrary<'a>>::size_hint(depth)
376
0
    }
377
}
378
379
impl<'a> Arbitrary<'a> for AtomicIsize {
380
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
381
0
        Arbitrary::arbitrary(u).map(Self::new)
382
0
    }
383
384
    #[inline]
385
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
386
0
        <isize as Arbitrary<'a>>::size_hint(depth)
387
0
    }
388
}
389
390
impl<'a> Arbitrary<'a> for AtomicUsize {
391
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
392
0
        Arbitrary::arbitrary(u).map(Self::new)
393
0
    }
394
395
    #[inline]
396
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
397
0
        <usize as Arbitrary<'a>>::size_hint(depth)
398
0
    }
399
}
400
401
macro_rules! impl_range {
402
    (
403
        $range:ty,
404
        $value_closure:expr,
405
        $value_ty:ty,
406
        $fun:ident($fun_closure:expr),
407
        $size_hint_closure:expr
408
    ) => {
409
        impl<'a, A> Arbitrary<'a> for $range
410
        where
411
            A: Arbitrary<'a> + Clone + PartialOrd,
412
        {
413
0
            fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
414
0
                let value: $value_ty = Arbitrary::arbitrary(u)?;
415
0
                Ok($fun(value, $fun_closure))
416
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
417
418
            #[inline]
419
0
            fn size_hint(depth: usize) -> (usize, Option<usize>) {
420
0
                $size_hint_closure(depth)
421
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
422
        }
423
    };
424
}
425
426
impl_range!(
427
    Range<A>,
428
    |r: &Range<A>| (r.start.clone(), r.end.clone()),
429
    (A, A),
430
0
    bounded_range(|(a, b)| a..b),
431
0
    |depth| crate::size_hint::and(
432
0
        <A as Arbitrary>::size_hint(depth),
433
0
        <A as Arbitrary>::size_hint(depth)
434
0
    )
435
);
436
impl_range!(
437
    RangeFrom<A>,
438
    |r: &RangeFrom<A>| r.start.clone(),
439
    A,
440
0
    unbounded_range(|a| a..),
441
0
    |depth| <A as Arbitrary>::size_hint(depth)
442
);
443
impl_range!(
444
    RangeInclusive<A>,
445
    |r: &RangeInclusive<A>| (r.start().clone(), r.end().clone()),
446
    (A, A),
447
0
    bounded_range(|(a, b)| a..=b),
448
0
    |depth| crate::size_hint::and(
449
0
        <A as Arbitrary>::size_hint(depth),
450
0
        <A as Arbitrary>::size_hint(depth)
451
0
    )
452
);
453
impl_range!(
454
    RangeTo<A>,
455
    |r: &RangeTo<A>| r.end.clone(),
456
    A,
457
0
    unbounded_range(|b| ..b),
458
0
    |depth| <A as Arbitrary>::size_hint(depth)
459
);
460
impl_range!(
461
    RangeToInclusive<A>,
462
    |r: &RangeToInclusive<A>| r.end.clone(),
463
    A,
464
0
    unbounded_range(|b| ..=b),
465
0
    |depth| <A as Arbitrary>::size_hint(depth)
466
);
467
468
0
pub(crate) fn bounded_range<CB, I, R>(bounds: (I, I), cb: CB) -> R
469
0
where
470
0
    CB: Fn((I, I)) -> R,
471
0
    I: PartialOrd,
472
0
    R: RangeBounds<I>,
473
0
{
474
0
    let (mut start, mut end) = bounds;
475
0
    if start > end {
476
0
        mem::swap(&mut start, &mut end);
477
0
    }
478
0
    cb((start, end))
479
0
}
480
481
0
pub(crate) fn unbounded_range<CB, I, R>(bound: I, cb: CB) -> R
482
0
where
483
0
    CB: Fn(I) -> R,
484
0
    R: RangeBounds<I>,
485
0
{
486
0
    cb(bound)
487
0
}
488
489
impl<'a> Arbitrary<'a> for Duration {
490
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
491
0
        Ok(Self::new(
492
0
            <u64 as Arbitrary>::arbitrary(u)?,
493
0
            u.int_in_range(0..=999_999_999)?,
494
        ))
495
0
    }
496
497
    #[inline]
498
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
499
0
        crate::size_hint::and(
500
0
            <u64 as Arbitrary>::size_hint(depth),
501
0
            <u32 as Arbitrary>::size_hint(depth),
502
0
        )
503
0
    }
504
}
505
506
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Option<A> {
507
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
508
0
        Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? {
509
0
            Some(Arbitrary::arbitrary(u)?)
510
        } else {
511
0
            None
512
        })
513
0
    }
514
515
    #[inline]
516
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
517
0
        crate::size_hint::and(
518
0
            <bool as Arbitrary>::size_hint(depth),
519
0
            crate::size_hint::or((0, Some(0)), <A as Arbitrary>::size_hint(depth)),
520
0
        )
521
0
    }
522
}
523
524
impl<'a, A: Arbitrary<'a>, B: Arbitrary<'a>> Arbitrary<'a> for std::result::Result<A, B> {
525
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
526
0
        Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? {
527
0
            Ok(<A as Arbitrary>::arbitrary(u)?)
528
        } else {
529
0
            Err(<B as Arbitrary>::arbitrary(u)?)
530
        })
531
0
    }
532
533
    #[inline]
534
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
535
0
        crate::size_hint::and(
536
0
            <bool as Arbitrary>::size_hint(depth),
537
0
            crate::size_hint::or(
538
0
                <A as Arbitrary>::size_hint(depth),
539
0
                <B as Arbitrary>::size_hint(depth),
540
0
            ),
541
0
        )
542
0
    }
543
}
544
545
macro_rules! arbitrary_tuple {
546
    () => {};
547
    ($last: ident $($xs: ident)*) => {
548
        arbitrary_tuple!($($xs)*);
549
550
        impl<'a, $($xs: Arbitrary<'a>,)* $last: Arbitrary<'a>> Arbitrary<'a> for ($($xs,)* $last,) {
551
0
            fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
552
0
                Ok(($($xs::arbitrary(u)?,)* Arbitrary::arbitrary(u)?,))
553
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
554
555
            #[allow(unused_mut, non_snake_case)]
556
0
            fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<Self> {
557
0
                $(let $xs = $xs::arbitrary(&mut u)?;)*
558
0
                let $last = $last::arbitrary_take_rest(u)?;
559
0
                Ok(($($xs,)* $last,))
560
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
561
562
            #[inline]
563
0
            fn size_hint(depth: usize) -> (usize, Option<usize>) {
564
0
                crate::size_hint::and_all(&[
565
0
                    <$last as Arbitrary>::size_hint(depth),
566
0
                    $( <$xs as Arbitrary>::size_hint(depth) ),*
567
0
                ])
568
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
569
        }
570
    };
571
}
572
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);
573
574
macro_rules! arbitrary_array {
575
    {$n:expr, ($t:ident, $a:ident) $(($ts:ident, $as:ident))*} => {
576
        arbitrary_array!{($n - 1), $(($ts, $as))*}
577
578
        impl<'a, T: Arbitrary<'a>> Arbitrary<'a> for [T; $n] {
579
0
            fn arbitrary(u: &mut Unstructured<'a>) -> Result<[T; $n]> {
580
0
                Ok([
581
0
                    Arbitrary::arbitrary(u)?,
582
0
                    $(<$ts as Arbitrary>::arbitrary(u)?),*
583
                ])
584
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
585
586
            #[allow(unused_mut)]
587
0
            fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<[T; $n]> {
588
0
                $(let $as = $ts::arbitrary(&mut u)?;)*
589
0
                let last = Arbitrary::arbitrary_take_rest(u)?;
590
591
0
                Ok([
592
0
                    $($as,)* last
593
0
                ])
594
0
            }
Unexecuted instantiation: <[_; 1] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 2] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 3] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 4] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 5] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 6] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 7] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 8] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 9] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 10] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 11] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 12] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 13] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 14] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 15] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 16] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 17] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 18] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 19] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 20] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 21] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 22] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 23] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 24] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 25] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 26] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 27] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 28] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 29] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 30] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 31] as arbitrary::Arbitrary>::arbitrary_take_rest
Unexecuted instantiation: <[_; 32] as arbitrary::Arbitrary>::arbitrary_take_rest
595
596
            #[inline]
597
0
            fn size_hint(depth: usize) -> (usize, Option<usize>) {
598
0
                crate::size_hint::and_all(&[
599
0
                    <$t as Arbitrary>::size_hint(depth),
600
0
                    $( <$ts as Arbitrary>::size_hint(depth) ),*
601
0
                ])
602
0
            }
Unexecuted instantiation: <[_; 1] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 2] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 3] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 4] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 5] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 6] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 7] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 8] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 9] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 10] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 11] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 12] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 13] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 14] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 15] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 16] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 17] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 18] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 19] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 20] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 21] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 22] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 23] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 24] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 25] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 26] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 27] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 28] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 29] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 30] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 31] as arbitrary::Arbitrary>::size_hint
Unexecuted instantiation: <[_; 32] as arbitrary::Arbitrary>::size_hint
603
        }
604
    };
605
    ($n: expr,) => {};
606
}
607
608
impl<'a, T: Arbitrary<'a>> Arbitrary<'a> for [T; 0] {
609
0
    fn arbitrary(_: &mut Unstructured<'a>) -> Result<[T; 0]> {
610
0
        Ok([])
611
0
    }
612
613
0
    fn arbitrary_take_rest(_: Unstructured<'a>) -> Result<[T; 0]> {
614
0
        Ok([])
615
0
    }
616
617
    #[inline]
618
0
    fn size_hint(_: usize) -> (usize, Option<usize>) {
619
0
        crate::size_hint::and_all(&[])
620
0
    }
621
}
622
623
arbitrary_array! { 32, (T, a) (T, b) (T, c) (T, d) (T, e) (T, f) (T, g) (T, h)
624
(T, i) (T, j) (T, k) (T, l) (T, m) (T, n) (T, o) (T, p)
625
(T, q) (T, r) (T, s) (T, u) (T, v) (T, w) (T, x) (T, y)
626
(T, z) (T, aa) (T, ab) (T, ac) (T, ad) (T, ae) (T, af)
627
(T, ag) }
628
629
impl<'a> Arbitrary<'a> for &'a [u8] {
630
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
631
0
        let len = u.arbitrary_len::<u8>()?;
632
0
        u.bytes(len)
633
0
    }
634
635
0
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
636
0
        Ok(u.take_rest())
637
0
    }
638
639
    #[inline]
640
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
641
0
        <usize as Arbitrary>::size_hint(depth)
642
0
    }
643
}
644
645
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> {
646
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
647
0
        u.arbitrary_iter()?.collect()
648
0
    }
649
650
0
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
651
0
        u.arbitrary_take_rest_iter()?.collect()
652
0
    }
653
654
    #[inline]
655
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
656
0
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
657
0
    }
658
}
659
660
impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K, V> {
661
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
662
0
        u.arbitrary_iter()?.collect()
663
0
    }
664
665
0
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
666
0
        u.arbitrary_take_rest_iter()?.collect()
667
0
    }
668
669
    #[inline]
670
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
671
0
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
672
0
    }
673
}
674
675
impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BTreeSet<A> {
676
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
677
0
        u.arbitrary_iter()?.collect()
678
0
    }
679
680
0
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
681
0
        u.arbitrary_take_rest_iter()?.collect()
682
0
    }
683
684
    #[inline]
685
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
686
0
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
687
0
    }
688
}
689
690
impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> {
691
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
692
0
        u.arbitrary_iter()?.collect()
693
0
    }
694
695
0
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
696
0
        u.arbitrary_take_rest_iter()?.collect()
697
0
    }
698
699
    #[inline]
700
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
701
0
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
702
0
    }
703
}
704
705
impl<'a, K: Arbitrary<'a> + Eq + ::std::hash::Hash, V: Arbitrary<'a>> Arbitrary<'a>
706
    for HashMap<K, V>
707
{
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
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
719
0
    }
720
}
721
722
impl<'a, A: Arbitrary<'a> + Eq + ::std::hash::Hash> Arbitrary<'a> for HashSet<A> {
723
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
724
0
        u.arbitrary_iter()?.collect()
725
0
    }
726
727
0
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
728
0
        u.arbitrary_take_rest_iter()?.collect()
729
0
    }
730
731
    #[inline]
732
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
733
0
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
734
0
    }
735
}
736
737
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> {
738
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
739
0
        u.arbitrary_iter()?.collect()
740
0
    }
741
742
0
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
743
0
        u.arbitrary_take_rest_iter()?.collect()
744
0
    }
745
746
    #[inline]
747
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
748
0
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
749
0
    }
750
}
751
752
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> {
753
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
754
0
        u.arbitrary_iter()?.collect()
755
0
    }
756
757
0
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
758
0
        u.arbitrary_take_rest_iter()?.collect()
759
0
    }
760
761
    #[inline]
762
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
763
0
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
764
0
    }
765
}
766
767
impl<'a, A> Arbitrary<'a> for Cow<'a, A>
768
where
769
    A: ToOwned + ?Sized,
770
    <A as ToOwned>::Owned: Arbitrary<'a>,
771
{
772
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
773
0
        Arbitrary::arbitrary(u).map(Cow::Owned)
774
0
    }
775
776
    #[inline]
777
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
778
0
        crate::size_hint::recursion_guard(depth, |depth| {
779
0
            <<A as ToOwned>::Owned as Arbitrary>::size_hint(depth)
780
0
        })
781
0
    }
782
}
783
784
impl<'a> Arbitrary<'a> for &'a str {
785
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
786
0
        let size = u.arbitrary_len::<u8>()?;
787
0
        match str::from_utf8(&u.peek_bytes(size).unwrap()) {
788
0
            Ok(s) => {
789
0
                u.bytes(size).unwrap();
790
0
                Ok(s)
791
            }
792
0
            Err(e) => {
793
0
                let i = e.valid_up_to();
794
0
                let valid = u.bytes(i).unwrap();
795
0
                let s = unsafe {
796
0
                    debug_assert!(str::from_utf8(valid).is_ok());
797
0
                    str::from_utf8_unchecked(valid)
798
0
                };
799
0
                Ok(s)
800
            }
801
        }
802
0
    }
803
804
7.50k
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
805
7.50k
        let bytes = u.take_rest();
806
7.50k
        str::from_utf8(bytes)
807
7.50k
            .map_err(|_| Error::IncorrectFormat)
808
7.50k
            .map(Into::into)
809
7.50k
    }
810
811
    #[inline]
812
7.51k
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
813
7.51k
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
814
7.51k
    }
<&str as arbitrary::Arbitrary>::size_hint
Line
Count
Source
812
7.51k
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
813
7.51k
        crate::size_hint::and(<usize as Arbitrary>::size_hint(depth), (0, None))
814
7.51k
    }
Unexecuted instantiation: <&str as arbitrary::Arbitrary>::size_hint
815
}
816
817
impl<'a> Arbitrary<'a> for String {
818
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
819
0
        <&str as Arbitrary>::arbitrary(u).map(Into::into)
820
0
    }
821
822
0
    fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> {
823
0
        <&str as Arbitrary>::arbitrary_take_rest(u).map(Into::into)
824
0
    }
825
826
    #[inline]
827
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
828
0
        <&str as Arbitrary>::size_hint(depth)
829
0
    }
830
}
831
832
impl<'a> Arbitrary<'a> for CString {
833
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
834
0
        <Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| {
835
0
            x.retain(|&c| c != 0);
836
0
            Self::new(x).unwrap()
837
0
        })
838
0
    }
839
840
    #[inline]
841
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
842
0
        <Vec<u8> as Arbitrary>::size_hint(depth)
843
0
    }
844
}
845
846
impl<'a> Arbitrary<'a> for OsString {
847
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
848
0
        <String as Arbitrary>::arbitrary(u).map(From::from)
849
0
    }
850
851
    #[inline]
852
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
853
0
        <String as Arbitrary>::size_hint(depth)
854
0
    }
855
}
856
857
impl<'a> Arbitrary<'a> for PathBuf {
858
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
859
0
        <OsString as Arbitrary>::arbitrary(u).map(From::from)
860
0
    }
861
862
    #[inline]
863
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
864
0
        <OsString as Arbitrary>::size_hint(depth)
865
0
    }
866
}
867
868
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {
869
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
870
0
        Arbitrary::arbitrary(u).map(Self::new)
871
0
    }
872
873
    #[inline]
874
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
875
0
        crate::size_hint::recursion_guard(depth, |depth| <A as Arbitrary>::size_hint(depth))
876
0
    }
877
}
878
879
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
880
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
881
0
        <Vec<A> as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_slice())
882
0
    }
883
884
    #[inline]
885
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
886
0
        <Vec<A> as Arbitrary>::size_hint(depth)
887
0
    }
888
}
889
890
impl<'a> Arbitrary<'a> for Box<str> {
891
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
892
0
        <String as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_str())
893
0
    }
894
895
    #[inline]
896
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
897
0
        <String as Arbitrary>::size_hint(depth)
898
0
    }
899
}
900
901
// impl Arbitrary for Box<CStr> {
902
//     fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> {
903
//         <CString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_c_str())
904
//     }
905
// }
906
907
// impl Arbitrary for Box<OsStr> {
908
//     fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> {
909
//         <OsString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_osstr())
910
//
911
//     }
912
// }
913
914
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
915
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
916
0
        Arbitrary::arbitrary(u).map(Self::new)
917
0
    }
918
919
    #[inline]
920
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
921
0
        crate::size_hint::recursion_guard(depth, |depth| <A as Arbitrary>::size_hint(depth))
922
0
    }
923
}
924
925
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> {
926
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
927
0
        Arbitrary::arbitrary(u).map(Self::new)
928
0
    }
929
930
    #[inline]
931
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
932
0
        crate::size_hint::recursion_guard(depth, |depth| <A as Arbitrary>::size_hint(depth))
933
0
    }
934
}
935
936
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Cell<A> {
937
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
938
0
        Arbitrary::arbitrary(u).map(Self::new)
939
0
    }
940
941
    #[inline]
942
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
943
0
        <A as Arbitrary<'a>>::size_hint(depth)
944
0
    }
945
}
946
947
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for RefCell<A> {
948
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
949
0
        Arbitrary::arbitrary(u).map(Self::new)
950
0
    }
951
952
    #[inline]
953
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
954
0
        <A as Arbitrary<'a>>::size_hint(depth)
955
0
    }
956
}
957
958
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for UnsafeCell<A> {
959
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
960
0
        Arbitrary::arbitrary(u).map(Self::new)
961
0
    }
962
963
    #[inline]
964
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
965
0
        <A as Arbitrary<'a>>::size_hint(depth)
966
0
    }
967
}
968
969
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Mutex<A> {
970
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
971
0
        Arbitrary::arbitrary(u).map(Self::new)
972
0
    }
973
974
    #[inline]
975
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
976
0
        <A as Arbitrary<'a>>::size_hint(depth)
977
0
    }
978
}
979
980
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for iter::Empty<A> {
981
0
    fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> {
982
0
        Ok(iter::empty())
983
0
    }
984
985
    #[inline]
986
0
    fn size_hint(_depth: usize) -> (usize, Option<usize>) {
987
0
        (0, Some(0))
988
0
    }
989
}
990
991
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::marker::PhantomData<A> {
992
0
    fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> {
993
0
        Ok(::std::marker::PhantomData)
994
0
    }
995
996
    #[inline]
997
0
    fn size_hint(_depth: usize) -> (usize, Option<usize>) {
998
0
        (0, Some(0))
999
0
    }
1000
}
1001
1002
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::num::Wrapping<A> {
1003
0
    fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
1004
0
        Arbitrary::arbitrary(u).map(::std::num::Wrapping)
1005
0
    }
1006
1007
    #[inline]
1008
0
    fn size_hint(depth: usize) -> (usize, Option<usize>) {
1009
0
        <A as Arbitrary<'a>>::size_hint(depth)
1010
0
    }
1011
}
1012
1013
#[cfg(test)]
1014
mod test {
1015
    use super::*;
1016
1017
    #[test]
1018
    fn finite_buffer_fill_buffer() {
1019
        let x = [1, 2, 3, 4];
1020
        let mut rb = Unstructured::new(&x);
1021
        let mut z = [0; 2];
1022
        rb.fill_buffer(&mut z).unwrap();
1023
        assert_eq!(z, [1, 2]);
1024
        rb.fill_buffer(&mut z).unwrap();
1025
        assert_eq!(z, [3, 4]);
1026
        rb.fill_buffer(&mut z).unwrap();
1027
        assert_eq!(z, [0, 0]);
1028
    }
1029
1030
    #[test]
1031
    fn arbitrary_for_integers() {
1032
        let x = [1, 2, 3, 4];
1033
        let mut buf = Unstructured::new(&x);
1034
        let expected = 1 | (2 << 8) | (3 << 16) | (4 << 24);
1035
        let actual = i32::arbitrary(&mut buf).unwrap();
1036
        assert_eq!(expected, actual);
1037
    }
1038
1039
    #[test]
1040
    fn arbitrary_for_bytes() {
1041
        let x = [1, 2, 3, 4, 4];
1042
        let mut buf = Unstructured::new(&x);
1043
        let expected = &[1, 2, 3, 4];
1044
        let actual = <&[u8] as Arbitrary>::arbitrary(&mut buf).unwrap();
1045
        assert_eq!(expected, actual);
1046
    }
1047
1048
    #[test]
1049
    fn arbitrary_take_rest_for_bytes() {
1050
        let x = [1, 2, 3, 4];
1051
        let buf = Unstructured::new(&x);
1052
        let expected = &[1, 2, 3, 4];
1053
        let actual = <&[u8] as Arbitrary>::arbitrary_take_rest(buf).unwrap();
1054
        assert_eq!(expected, actual);
1055
    }
1056
1057
    #[test]
1058
    fn arbitrary_collection() {
1059
        let x = [
1060
            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,
1061
        ];
1062
        assert_eq!(
1063
            Vec::<u8>::arbitrary(&mut Unstructured::new(&x)).unwrap(),
1064
            &[2, 4, 6, 8, 1]
1065
        );
1066
        assert_eq!(
1067
            Vec::<u32>::arbitrary(&mut Unstructured::new(&x)).unwrap(),
1068
            &[84148994]
1069
        );
1070
        assert_eq!(
1071
            String::arbitrary(&mut Unstructured::new(&x)).unwrap(),
1072
            "\x01\x02\x03\x04\x05\x06\x07\x08"
1073
        );
1074
    }
1075
1076
    #[test]
1077
    fn arbitrary_take_rest() {
1078
        let x = [1, 2, 3, 4];
1079
        assert_eq!(
1080
            Vec::<u8>::arbitrary_take_rest(Unstructured::new(&x)).unwrap(),
1081
            &[1, 2, 3, 4]
1082
        );
1083
        assert_eq!(
1084
            Vec::<u32>::arbitrary_take_rest(Unstructured::new(&x)).unwrap(),
1085
            &[0x4030201]
1086
        );
1087
        assert_eq!(
1088
            String::arbitrary_take_rest(Unstructured::new(&x)).unwrap(),
1089
            "\x01\x02\x03\x04"
1090
        );
1091
    }
1092
1093
    #[test]
1094
    fn size_hint_for_tuples() {
1095
        assert_eq!(
1096
            (7, Some(7)),
1097
            <(bool, u16, i32) as Arbitrary<'_>>::size_hint(0)
1098
        );
1099
        assert_eq!(
1100
            (1 + mem::size_of::<usize>(), None),
1101
            <(u8, Vec<u8>) as Arbitrary>::size_hint(0)
1102
        );
1103
    }
1104
}