| 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::{IpAddr, 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 |
fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<Self> { |
| 190 |
Self::arbitrary(&mut u) |
| 191 |
} |
| 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 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 278 |
let _ = depth; |
| 279 |
(0, None) |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
impl<'a> Arbitrary<'a> for () { |
| 284 |
fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> { |
| 285 |
Ok(()) |
| 286 |
} |
| 287 |
|
| 288 |
#[inline] |
| 289 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 290 |
(0, Some(0)) |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
impl<'a> Arbitrary<'a> for bool { |
| 295 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 296 |
Ok(<u8 as Arbitrary<'a>>::arbitrary(u)? & 1 == 1) |
| 297 |
} |
| 298 |
|
| 299 |
#[inline] |
| 300 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 301 |
<u8 as Arbitrary<'a>>::size_hint(depth) |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
macro_rules! impl_arbitrary_for_integers { |
| 306 |
( $( $ty:ty: $unsigned:ty; )* ) => { |
| 307 |
$( |
| 308 |
impl<'a> Arbitrary<'a> for $ty { |
| 309 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 310 |
let mut buf = [0; mem::size_of::<$ty>()]; |
| 311 |
u.fill_buffer(&mut buf)?; |
| 312 |
let mut x: $unsigned = 0; |
| 313 |
for i in 0..mem::size_of::<$ty>() { |
| 314 |
x |= buf[i] as $unsigned << (i * 8); |
| 315 |
} |
| 316 |
Ok(x as $ty) |
| 317 |
} |
| 318 |
|
| 319 |
#[inline] |
| 320 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 321 |
let n = mem::size_of::<$ty>(); |
| 322 |
(n, Some(n)) |
| 323 |
} |
| 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 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 350 |
Ok(Self::from_bits(<$unsigned as Arbitrary<'a>>::arbitrary(u)?)) |
| 351 |
} |
| 352 |
|
| 353 |
#[inline] |
| 354 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 355 |
<$unsigned as Arbitrary<'a>>::size_hint(depth) |
| 356 |
} |
| 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 |
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 |
let mut c = <u32 as Arbitrary<'a>>::arbitrary(u)? % CHAR_END; |
| 375 |
if let Some(c) = char::from_u32(c) { |
| 376 |
Ok(c) |
| 377 |
} else { |
| 378 |
// We found a surrogate, wrap and try again |
| 379 |
c -= SURROGATES_START; |
| 380 |
Ok(char::from_u32(c) |
| 381 |
.expect("Generated character should be valid! This is a bug in arbitrary-rs")) |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
#[inline] |
| 386 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 387 |
<u32 as Arbitrary<'a>>::size_hint(depth) |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
impl<'a> Arbitrary<'a> for AtomicBool { |
| 392 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 393 |
Arbitrary::arbitrary(u).map(Self::new) |
| 394 |
} |
| 395 |
|
| 396 |
#[inline] |
| 397 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 398 |
<bool as Arbitrary<'a>>::size_hint(depth) |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
impl<'a> Arbitrary<'a> for AtomicIsize { |
| 403 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 404 |
Arbitrary::arbitrary(u).map(Self::new) |
| 405 |
} |
| 406 |
|
| 407 |
#[inline] |
| 408 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 409 |
<isize as Arbitrary<'a>>::size_hint(depth) |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
impl<'a> Arbitrary<'a> for AtomicUsize { |
| 414 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 415 |
Arbitrary::arbitrary(u).map(Self::new) |
| 416 |
} |
| 417 |
|
| 418 |
#[inline] |
| 419 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 420 |
<usize as Arbitrary<'a>>::size_hint(depth) |
| 421 |
} |
| 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 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 437 |
let value: $value_ty = Arbitrary::arbitrary(u)?; |
| 438 |
Ok($fun(value, $fun_closure)) |
| 439 |
} |
| 440 |
|
| 441 |
#[inline] |
| 442 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 443 |
#[allow(clippy::redundant_closure_call)] |
| 444 |
$size_hint_closure(depth) |
| 445 |
} |
| 446 |
} |
| 447 |
}; |
| 448 |
} |
| 449 |
|
| 450 |
impl_range!( |
| 451 |
Range<A>, |
| 452 |
|r: &Range<A>| (r.start.clone(), r.end.clone()), |
| 453 |
(A, A), |
| 454 |
bounded_range(|(a, b)| a..b), |
| 455 |
|depth| crate::size_hint::and( |
| 456 |
<A as Arbitrary>::size_hint(depth), |
| 457 |
<A as Arbitrary>::size_hint(depth) |
| 458 |
) |
| 459 |
); |
| 460 |
impl_range!( |
| 461 |
RangeFrom<A>, |
| 462 |
|r: &RangeFrom<A>| r.start.clone(), |
| 463 |
A, |
| 464 |
unbounded_range(|a| a..), |
| 465 |
|depth| <A as Arbitrary>::size_hint(depth) |
| 466 |
); |
| 467 |
impl_range!( |
| 468 |
RangeInclusive<A>, |
| 469 |
|r: &RangeInclusive<A>| (r.start().clone(), r.end().clone()), |
| 470 |
(A, A), |
| 471 |
bounded_range(|(a, b)| a..=b), |
| 472 |
|depth| crate::size_hint::and( |
| 473 |
<A as Arbitrary>::size_hint(depth), |
| 474 |
<A as Arbitrary>::size_hint(depth) |
| 475 |
) |
| 476 |
); |
| 477 |
impl_range!( |
| 478 |
RangeTo<A>, |
| 479 |
|r: &RangeTo<A>| r.end.clone(), |
| 480 |
A, |
| 481 |
unbounded_range(|b| ..b), |
| 482 |
|depth| <A as Arbitrary>::size_hint(depth) |
| 483 |
); |
| 484 |
impl_range!( |
| 485 |
RangeToInclusive<A>, |
| 486 |
|r: &RangeToInclusive<A>| r.end.clone(), |
| 487 |
A, |
| 488 |
unbounded_range(|b| ..=b), |
| 489 |
|depth| <A as Arbitrary>::size_hint(depth) |
| 490 |
); |
| 491 |
|
| 492 |
pub(crate) fn bounded_range<CB, I, R>(bounds: (I, I), cb: CB) -> R |
| 493 |
where |
| 494 |
CB: Fn((I, I)) -> R, |
| 495 |
I: PartialOrd, |
| 496 |
R: RangeBounds<I>, |
| 497 |
{ |
| 498 |
let (mut start, mut end) = bounds; |
| 499 |
if start > end { |
| 500 |
mem::swap(&mut start, &mut end); |
| 501 |
} |
| 502 |
cb((start, end)) |
| 503 |
} |
| 504 |
|
| 505 |
pub(crate) fn unbounded_range<CB, I, R>(bound: I, cb: CB) -> R |
| 506 |
where |
| 507 |
CB: Fn(I) -> R, |
| 508 |
R: RangeBounds<I>, |
| 509 |
{ |
| 510 |
cb(bound) |
| 511 |
} |
| 512 |
|
| 513 |
impl<'a> Arbitrary<'a> for Duration { |
| 514 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 515 |
Ok(Self::new( |
| 516 |
<u64 as Arbitrary>::arbitrary(u)?, |
| 517 |
u.int_in_range(0..=999_999_999)?, |
| 518 |
)) |
| 519 |
} |
| 520 |
|
| 521 |
#[inline] |
| 522 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 523 |
crate::size_hint::and( |
| 524 |
<u64 as Arbitrary>::size_hint(depth), |
| 525 |
<u32 as Arbitrary>::size_hint(depth), |
| 526 |
) |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Option<A> { |
| 531 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 532 |
Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? { |
| 533 |
Some(Arbitrary::arbitrary(u)?) |
| 534 |
} else { |
| 535 |
None |
| 536 |
}) |
| 537 |
} |
| 538 |
|
| 539 |
#[inline] |
| 540 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 541 |
crate::size_hint::and( |
| 542 |
<bool as Arbitrary>::size_hint(depth), |
| 543 |
crate::size_hint::or((0, Some(0)), <A as Arbitrary>::size_hint(depth)), |
| 544 |
) |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
impl<'a, A: Arbitrary<'a>, B: Arbitrary<'a>> Arbitrary<'a> for std::result::Result<A, B> { |
| 549 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 550 |
Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? { |
| 551 |
Ok(<A as Arbitrary>::arbitrary(u)?) |
| 552 |
} else { |
| 553 |
Err(<B as Arbitrary>::arbitrary(u)?) |
| 554 |
}) |
| 555 |
} |
| 556 |
|
| 557 |
#[inline] |
| 558 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 559 |
crate::size_hint::and( |
| 560 |
<bool as Arbitrary>::size_hint(depth), |
| 561 |
crate::size_hint::or( |
| 562 |
<A as Arbitrary>::size_hint(depth), |
| 563 |
<B as Arbitrary>::size_hint(depth), |
| 564 |
), |
| 565 |
) |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
macro_rules! arbitrary_tuple { |
| 570 |
() => {}; |
| 571 |
($last: ident $($xs: ident)*) => { |
| 572 |
arbitrary_tuple!($($xs)*); |
| 573 |
|
| 574 |
impl<'a, $($xs: Arbitrary<'a>,)* $last: Arbitrary<'a>> Arbitrary<'a> for ($($xs,)* $last,) { |
| 575 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 576 |
Ok(($($xs::arbitrary(u)?,)* Arbitrary::arbitrary(u)?,)) |
| 577 |
} |
| 578 |
|
| 579 |
#[allow(unused_mut, non_snake_case)] |
| 580 |
fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<Self> { |
| 581 |
$(let $xs = $xs::arbitrary(&mut u)?;)* |
| 582 |
let $last = $last::arbitrary_take_rest(u)?; |
| 583 |
Ok(($($xs,)* $last,)) |
| 584 |
} |
| 585 |
|
| 586 |
#[inline] |
| 587 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 588 |
crate::size_hint::and_all(&[ |
| 589 |
<$last as Arbitrary>::size_hint(depth), |
| 590 |
$( <$xs as Arbitrary>::size_hint(depth) ),* |
| 591 |
]) |
| 592 |
} |
| 593 |
} |
| 594 |
}; |
| 595 |
} |
| 596 |
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); |
| 597 |
|
| 598 |
// Helper to safely create arrays since the standard library doesn't |
| 599 |
// provide one yet. Shouldn't be necessary in the future. |
| 600 |
struct ArrayGuard<T, const N: usize> { |
| 601 |
dst: *mut T, |
| 602 |
initialized: usize, |
| 603 |
} |
| 604 |
|
| 605 |
impl<T, const N: usize> Drop for ArrayGuard<T, N> { |
| 606 |
fn drop(&mut self) { |
| 607 |
debug_assert!(self.initialized <= N); |
| 608 |
let initialized_part = core::ptr::slice_from_raw_parts_mut(self.dst, self.initialized); |
| 609 |
unsafe { |
| 610 |
core::ptr::drop_in_place(initialized_part); |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
fn try_create_array<F, T, const N: usize>(mut cb: F) -> Result<[T; N]> |
| 616 |
where |
| 617 |
F: FnMut(usize) -> Result<T>, |
| 618 |
{ |
| 619 |
let mut array: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninit(); |
| 620 |
let array_ptr = array.as_mut_ptr(); |
| 621 |
let dst = array_ptr as _; |
| 622 |
let mut guard: ArrayGuard<T, N> = ArrayGuard { |
| 623 |
dst, |
| 624 |
initialized: 0, |
| 625 |
}; |
| 626 |
unsafe { |
| 627 |
for (idx, value_ptr) in (*array.as_mut_ptr()).iter_mut().enumerate() { |
| 628 |
core::ptr::write(value_ptr, cb(idx)?); |
| 629 |
guard.initialized += 1; |
| 630 |
} |
| 631 |
mem::forget(guard); |
| 632 |
Ok(array.assume_init()) |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
impl<'a, T, const N: usize> Arbitrary<'a> for [T; N] |
| 637 |
where |
| 638 |
T: Arbitrary<'a>, |
| 639 |
{ |
| 640 |
#[inline] |
| 641 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 642 |
try_create_array(|_| <T as Arbitrary<'a>>::arbitrary(u)) |
| 643 |
} |
| 644 |
|
| 645 |
#[inline] |
| 646 |
fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<Self> { |
| 647 |
let mut array = Self::arbitrary(&mut u)?; |
| 648 |
if let Some(last) = array.last_mut() { |
| 649 |
*last = Arbitrary::arbitrary_take_rest(u)?; |
| 650 |
} |
| 651 |
Ok(array) |
| 652 |
} |
| 653 |
|
| 654 |
#[inline] |
| 655 |
fn size_hint(d: usize) -> (usize, Option<usize>) { |
| 656 |
crate::size_hint::and_all(&array::from_fn::<_, N, _>(|_| { |
| 657 |
<T as Arbitrary>::size_hint(d) |
| 658 |
})) |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
impl<'a> Arbitrary<'a> for &'a [u8] { |
| 663 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 664 |
let len = u.arbitrary_len::<u8>()?; |
| 665 |
u.bytes(len) |
| 666 |
} |
| 667 |
|
| 668 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 669 |
Ok(u.take_rest()) |
| 670 |
} |
| 671 |
|
| 672 |
#[inline] |
| 673 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 674 |
(0, None) |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> { |
| 679 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 680 |
u.arbitrary_iter()?.collect() |
| 681 |
} |
| 682 |
|
| 683 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 684 |
u.arbitrary_take_rest_iter()?.collect() |
| 685 |
} |
| 686 |
|
| 687 |
#[inline] |
| 688 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 689 |
(0, None) |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K, V> { |
| 694 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 695 |
u.arbitrary_iter()?.collect() |
| 696 |
} |
| 697 |
|
| 698 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 699 |
u.arbitrary_take_rest_iter()?.collect() |
| 700 |
} |
| 701 |
|
| 702 |
#[inline] |
| 703 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 704 |
(0, None) |
| 705 |
} |
| 706 |
} |
| 707 |
|
| 708 |
impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BTreeSet<A> { |
| 709 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 710 |
u.arbitrary_iter()?.collect() |
| 711 |
} |
| 712 |
|
| 713 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 714 |
u.arbitrary_take_rest_iter()?.collect() |
| 715 |
} |
| 716 |
|
| 717 |
#[inline] |
| 718 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 719 |
(0, None) |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Bound<A> { |
| 724 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 725 |
match u.int_in_range::<u8>(0..=2)? { |
| 726 |
0 => Ok(Bound::Included(A::arbitrary(u)?)), |
| 727 |
1 => Ok(Bound::Excluded(A::arbitrary(u)?)), |
| 728 |
2 => Ok(Bound::Unbounded), |
| 729 |
_ => unreachable!(), |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
#[inline] |
| 734 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 735 |
size_hint::or( |
| 736 |
size_hint::and((1, Some(1)), A::size_hint(depth)), |
| 737 |
(1, Some(1)), |
| 738 |
) |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> { |
| 743 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 744 |
u.arbitrary_iter()?.collect() |
| 745 |
} |
| 746 |
|
| 747 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 748 |
u.arbitrary_take_rest_iter()?.collect() |
| 749 |
} |
| 750 |
|
| 751 |
#[inline] |
| 752 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 753 |
(0, None) |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
impl<'a, K: Arbitrary<'a> + Eq + ::std::hash::Hash, V: Arbitrary<'a>, S: BuildHasher + Default> |
| 758 |
Arbitrary<'a> for HashMap<K, V, S> |
| 759 |
{ |
| 760 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 761 |
u.arbitrary_iter()?.collect() |
| 762 |
} |
| 763 |
|
| 764 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 765 |
u.arbitrary_take_rest_iter()?.collect() |
| 766 |
} |
| 767 |
|
| 768 |
#[inline] |
| 769 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 770 |
(0, None) |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
impl<'a, A: Arbitrary<'a> + Eq + ::std::hash::Hash, S: BuildHasher + Default> Arbitrary<'a> |
| 775 |
for HashSet<A, S> |
| 776 |
{ |
| 777 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 778 |
u.arbitrary_iter()?.collect() |
| 779 |
} |
| 780 |
|
| 781 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 782 |
u.arbitrary_take_rest_iter()?.collect() |
| 783 |
} |
| 784 |
|
| 785 |
#[inline] |
| 786 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 787 |
(0, None) |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> { |
| 792 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 793 |
u.arbitrary_iter()?.collect() |
| 794 |
} |
| 795 |
|
| 796 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 797 |
u.arbitrary_take_rest_iter()?.collect() |
| 798 |
} |
| 799 |
|
| 800 |
#[inline] |
| 801 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 802 |
(0, None) |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> { |
| 807 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 808 |
u.arbitrary_iter()?.collect() |
| 809 |
} |
| 810 |
|
| 811 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 812 |
u.arbitrary_take_rest_iter()?.collect() |
| 813 |
} |
| 814 |
|
| 815 |
#[inline] |
| 816 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 817 |
(0, None) |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
impl<'a, A> Arbitrary<'a> for Cow<'a, A> |
| 822 |
where |
| 823 |
A: ToOwned + ?Sized, |
| 824 |
<A as ToOwned>::Owned: Arbitrary<'a>, |
| 825 |
{ |
| 826 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 827 |
Arbitrary::arbitrary(u).map(Cow::Owned) |
| 828 |
} |
| 829 |
|
| 830 |
#[inline] |
| 831 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 832 |
crate::size_hint::recursion_guard(depth, |depth| { |
| 833 |
<<A as ToOwned>::Owned as Arbitrary>::size_hint(depth) |
| 834 |
}) |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
fn arbitrary_str<'a>(u: &mut Unstructured<'a>, size: usize) -> Result<&'a str> { |
| 839 |
match str::from_utf8(u.peek_bytes(size).unwrap()) { |
| 840 |
Ok(s) => { |
| 841 |
u.bytes(size).unwrap(); |
| 842 |
Ok(s) |
| 843 |
} |
| 844 |
Err(e) => { |
| 845 |
let i = e.valid_up_to(); |
| 846 |
let valid = u.bytes(i).unwrap(); |
| 847 |
let s = unsafe { |
| 848 |
debug_assert!(str::from_utf8(valid).is_ok()); |
| 849 |
str::from_utf8_unchecked(valid) |
| 850 |
}; |
| 851 |
Ok(s) |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
impl<'a> Arbitrary<'a> for &'a str { |
| 857 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 858 |
let size = u.arbitrary_len::<u8>()?; |
| 859 |
arbitrary_str(u, size) |
| 860 |
} |
| 861 |
|
| 862 |
fn arbitrary_take_rest(mut u: Unstructured<'a>) -> Result<Self> { |
| 863 |
let size = u.len(); |
| 864 |
arbitrary_str(&mut u, size) |
| 865 |
} |
| 866 |
|
| 867 |
#[inline] |
| 868 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 869 |
(0, None) |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
impl<'a> Arbitrary<'a> for String { |
| 874 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 875 |
<&str as Arbitrary>::arbitrary(u).map(Into::into) |
| 876 |
} |
| 877 |
|
| 878 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 879 |
<&str as Arbitrary>::arbitrary_take_rest(u).map(Into::into) |
| 880 |
} |
| 881 |
|
| 882 |
#[inline] |
| 883 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 884 |
<&str as Arbitrary>::size_hint(depth) |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
impl<'a> Arbitrary<'a> for CString { |
| 889 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 890 |
<Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| { |
| 891 |
x.retain(|&c| c != 0); |
| 892 |
Self::new(x).unwrap() |
| 893 |
}) |
| 894 |
} |
| 895 |
|
| 896 |
#[inline] |
| 897 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 898 |
<Vec<u8> as Arbitrary>::size_hint(depth) |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
impl<'a> Arbitrary<'a> for OsString { |
| 903 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 904 |
<String as Arbitrary>::arbitrary(u).map(From::from) |
| 905 |
} |
| 906 |
|
| 907 |
#[inline] |
| 908 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 909 |
<String as Arbitrary>::size_hint(depth) |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
impl<'a> Arbitrary<'a> for PathBuf { |
| 914 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 915 |
<OsString as Arbitrary>::arbitrary(u).map(From::from) |
| 916 |
} |
| 917 |
|
| 918 |
#[inline] |
| 919 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 920 |
<OsString as Arbitrary>::size_hint(depth) |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> { |
| 925 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 926 |
Arbitrary::arbitrary(u).map(Self::new) |
| 927 |
} |
| 928 |
|
| 929 |
#[inline] |
| 930 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 931 |
crate::size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint) |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> { |
| 936 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 937 |
u.arbitrary_iter()?.collect() |
| 938 |
} |
| 939 |
|
| 940 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 941 |
u.arbitrary_take_rest_iter()?.collect() |
| 942 |
} |
| 943 |
|
| 944 |
#[inline] |
| 945 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 946 |
(0, None) |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
impl<'a> Arbitrary<'a> for Box<str> { |
| 951 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 952 |
<String as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_str()) |
| 953 |
} |
| 954 |
|
| 955 |
#[inline] |
| 956 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 957 |
<String as Arbitrary>::size_hint(depth) |
| 958 |
} |
| 959 |
} |
| 960 |
|
| 961 |
// impl Arbitrary for Box<CStr> { |
| 962 |
// fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> { |
| 963 |
// <CString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_c_str()) |
| 964 |
// } |
| 965 |
// } |
| 966 |
|
| 967 |
// impl Arbitrary for Box<OsStr> { |
| 968 |
// fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> { |
| 969 |
// <OsString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_osstr()) |
| 970 |
// |
| 971 |
// } |
| 972 |
// } |
| 973 |
|
| 974 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> { |
| 975 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 976 |
Arbitrary::arbitrary(u).map(Self::new) |
| 977 |
} |
| 978 |
|
| 979 |
#[inline] |
| 980 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 981 |
crate::size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint) |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<[A]> { |
| 986 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 987 |
u.arbitrary_iter()?.collect() |
| 988 |
} |
| 989 |
|
| 990 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 991 |
u.arbitrary_take_rest_iter()?.collect() |
| 992 |
} |
| 993 |
|
| 994 |
#[inline] |
| 995 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 996 |
(0, None) |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
impl<'a> Arbitrary<'a> for Arc<str> { |
| 1001 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1002 |
<&str as Arbitrary>::arbitrary(u).map(Into::into) |
| 1003 |
} |
| 1004 |
|
| 1005 |
#[inline] |
| 1006 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1007 |
<&str as Arbitrary>::size_hint(depth) |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> { |
| 1012 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1013 |
Arbitrary::arbitrary(u).map(Self::new) |
| 1014 |
} |
| 1015 |
|
| 1016 |
#[inline] |
| 1017 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1018 |
crate::size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint) |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<[A]> { |
| 1023 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1024 |
u.arbitrary_iter()?.collect() |
| 1025 |
} |
| 1026 |
|
| 1027 |
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self> { |
| 1028 |
u.arbitrary_take_rest_iter()?.collect() |
| 1029 |
} |
| 1030 |
|
| 1031 |
#[inline] |
| 1032 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 1033 |
(0, None) |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
impl<'a> Arbitrary<'a> for Rc<str> { |
| 1038 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1039 |
<&str as Arbitrary>::arbitrary(u).map(Into::into) |
| 1040 |
} |
| 1041 |
|
| 1042 |
#[inline] |
| 1043 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1044 |
<&str as Arbitrary>::size_hint(depth) |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Cell<A> { |
| 1049 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1050 |
Arbitrary::arbitrary(u).map(Self::new) |
| 1051 |
} |
| 1052 |
|
| 1053 |
#[inline] |
| 1054 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1055 |
<A as Arbitrary<'a>>::size_hint(depth) |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for RefCell<A> { |
| 1060 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1061 |
Arbitrary::arbitrary(u).map(Self::new) |
| 1062 |
} |
| 1063 |
|
| 1064 |
#[inline] |
| 1065 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1066 |
<A as Arbitrary<'a>>::size_hint(depth) |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for UnsafeCell<A> { |
| 1071 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1072 |
Arbitrary::arbitrary(u).map(Self::new) |
| 1073 |
} |
| 1074 |
|
| 1075 |
#[inline] |
| 1076 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1077 |
<A as Arbitrary<'a>>::size_hint(depth) |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Mutex<A> { |
| 1082 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1083 |
Arbitrary::arbitrary(u).map(Self::new) |
| 1084 |
} |
| 1085 |
|
| 1086 |
#[inline] |
| 1087 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1088 |
<A as Arbitrary<'a>>::size_hint(depth) |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for iter::Empty<A> { |
| 1093 |
fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> { |
| 1094 |
Ok(iter::empty()) |
| 1095 |
} |
| 1096 |
|
| 1097 |
#[inline] |
| 1098 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 1099 |
(0, Some(0)) |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::marker::PhantomData<A> { |
| 1104 |
fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> { |
| 1105 |
Ok(::std::marker::PhantomData) |
| 1106 |
} |
| 1107 |
|
| 1108 |
#[inline] |
| 1109 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 1110 |
(0, Some(0)) |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::num::Wrapping<A> { |
| 1115 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1116 |
Arbitrary::arbitrary(u).map(::std::num::Wrapping) |
| 1117 |
} |
| 1118 |
|
| 1119 |
#[inline] |
| 1120 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1121 |
<A as Arbitrary<'a>>::size_hint(depth) |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
macro_rules! implement_nonzero_int { |
| 1126 |
($nonzero:ty, $int:ty) => { |
| 1127 |
impl<'a> Arbitrary<'a> for $nonzero { |
| 1128 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1129 |
match Self::new(<$int as Arbitrary<'a>>::arbitrary(u)?) { |
| 1130 |
Some(n) => Ok(n), |
| 1131 |
None => Err(Error::IncorrectFormat), |
| 1132 |
} |
| 1133 |
} |
| 1134 |
|
| 1135 |
#[inline] |
| 1136 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1137 |
<$int as Arbitrary<'a>>::size_hint(depth) |
| 1138 |
} |
| 1139 |
} |
| 1140 |
}; |
| 1141 |
} |
| 1142 |
|
| 1143 |
implement_nonzero_int! { NonZeroI8, i8 } |
| 1144 |
implement_nonzero_int! { NonZeroI16, i16 } |
| 1145 |
implement_nonzero_int! { NonZeroI32, i32 } |
| 1146 |
implement_nonzero_int! { NonZeroI64, i64 } |
| 1147 |
implement_nonzero_int! { NonZeroI128, i128 } |
| 1148 |
implement_nonzero_int! { NonZeroIsize, isize } |
| 1149 |
implement_nonzero_int! { NonZeroU8, u8 } |
| 1150 |
implement_nonzero_int! { NonZeroU16, u16 } |
| 1151 |
implement_nonzero_int! { NonZeroU32, u32 } |
| 1152 |
implement_nonzero_int! { NonZeroU64, u64 } |
| 1153 |
implement_nonzero_int! { NonZeroU128, u128 } |
| 1154 |
implement_nonzero_int! { NonZeroUsize, usize } |
| 1155 |
|
| 1156 |
impl<'a> Arbitrary<'a> for Ipv4Addr { |
| 1157 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1158 |
Ok(Ipv4Addr::from(u32::arbitrary(u)?)) |
| 1159 |
} |
| 1160 |
|
| 1161 |
#[inline] |
| 1162 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 1163 |
(4, Some(4)) |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
impl<'a> Arbitrary<'a> for Ipv6Addr { |
| 1168 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1169 |
Ok(Ipv6Addr::from(u128::arbitrary(u)?)) |
| 1170 |
} |
| 1171 |
|
| 1172 |
#[inline] |
| 1173 |
fn size_hint(_depth: usize) -> (usize, Option<usize>) { |
| 1174 |
(16, Some(16)) |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
impl<'a> Arbitrary<'a> for IpAddr { |
| 1179 |
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { |
| 1180 |
if u.arbitrary()? { |
| 1181 |
Ok(IpAddr::V4(u.arbitrary()?)) |
| 1182 |
} else { |
| 1183 |
Ok(IpAddr::V6(u.arbitrary()?)) |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
fn size_hint(depth: usize) -> (usize, Option<usize>) { |
| 1188 |
size_hint::and( |
| 1189 |
bool::size_hint(depth), |
| 1190 |
size_hint::or(Ipv4Addr::size_hint(depth), Ipv6Addr::size_hint(depth)), |
| 1191 |
) |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
#[cfg(test)] |
| 1196 |
mod test { |
| 1197 |
use super::*; |
| 1198 |
|
| 1199 |
/// Assert that the given expected values are all generated. |
| 1200 |
/// |
| 1201 |
/// Exhaustively enumerates all buffers up to length 10 containing the |
| 1202 |
/// following bytes: `0x00`, `0x01`, `0x61` (aka ASCII 'a'), and `0xff` |
| 1203 |
fn assert_generates<T>(expected_values: impl IntoIterator<Item = T>) |
| 1204 |
where |
| 1205 |
T: Clone + std::fmt::Debug + std::hash::Hash + Eq + for<'a> Arbitrary<'a>, |
| 1206 |
{ |
| 1207 |
let expected_values: HashSet<_> = expected_values.into_iter().collect(); |
| 1208 |
let mut arbitrary_expected = expected_values.clone(); |
| 1209 |
let mut arbitrary_take_rest_expected = expected_values; |
| 1210 |
|
| 1211 |
let bytes = [0, 1, b'a', 0xff]; |
| 1212 |
let max_len = 10; |
| 1213 |
|
| 1214 |
let mut buf = Vec::with_capacity(max_len); |
| 1215 |
|
| 1216 |
let mut g = exhaustigen::Gen::new(); |
| 1217 |
while !g.done() { |
| 1218 |
let len = g.gen(max_len); |
| 1219 |
|
| 1220 |
buf.clear(); |
| 1221 |
buf.extend( |
| 1222 |
std::iter::repeat_with(|| { |
| 1223 |
let index = g.gen(bytes.len() - 1); |
| 1224 |
bytes[index] |
| 1225 |
}) |
| 1226 |
.take(len), |
| 1227 |
); |
| 1228 |
|
| 1229 |
let mut u = Unstructured::new(&buf); |
| 1230 |
let val = T::arbitrary(&mut u).unwrap(); |
| 1231 |
arbitrary_expected.remove(&val); |
| 1232 |
|
| 1233 |
let u = Unstructured::new(&buf); |
| 1234 |
let val = T::arbitrary_take_rest(u).unwrap(); |
| 1235 |
arbitrary_take_rest_expected.remove(&val); |
| 1236 |
|
| 1237 |
if arbitrary_expected.is_empty() && arbitrary_take_rest_expected.is_empty() { |
| 1238 |
return; |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
panic!( |
| 1243 |
"failed to generate all expected values!\n\n\ |
| 1244 |
T::arbitrary did not generate: {arbitrary_expected:#?}\n\n\ |
| 1245 |
T::arbitrary_take_rest did not generate {arbitrary_take_rest_expected:#?}" |
| 1246 |
) |
| 1247 |
} |
| 1248 |
|
| 1249 |
/// Generates an arbitrary `T`, and checks that the result is consistent with the |
| 1250 |
/// `size_hint()` reported by `T`. |
| 1251 |
fn checked_arbitrary<'a, T: Arbitrary<'a>>(u: &mut Unstructured<'a>) -> Result<T> { |
| 1252 |
let (min, max) = T::size_hint(0); |
| 1253 |
|
| 1254 |
let len_before = u.len(); |
| 1255 |
let result = T::arbitrary(u); |
| 1256 |
|
| 1257 |
let consumed = len_before - u.len(); |
| 1258 |
|
| 1259 |
if let Some(max) = max { |
| 1260 |
assert!( |
| 1261 |
consumed <= max, |
| 1262 |
"incorrect maximum size: indicated {}, actually consumed {}", |
| 1263 |
max, |
| 1264 |
consumed |
| 1265 |
); |
| 1266 |
} |
| 1267 |
|
| 1268 |
if result.is_ok() { |
| 1269 |
assert!( |
| 1270 |
consumed >= min, |
| 1271 |
"incorrect minimum size: indicated {}, actually consumed {}", |
| 1272 |
min, |
| 1273 |
consumed |
| 1274 |
); |
| 1275 |
} |
| 1276 |
|
| 1277 |
result |
| 1278 |
} |
| 1279 |
|
| 1280 |
/// Like `checked_arbitrary()`, but calls `arbitrary_take_rest()` instead of `arbitrary()`. |
| 1281 |
fn checked_arbitrary_take_rest<'a, T: Arbitrary<'a>>(u: Unstructured<'a>) -> Result<T> { |
| 1282 |
let (min, _) = T::size_hint(0); |
| 1283 |
|
| 1284 |
let len_before = u.len(); |
| 1285 |
let result = T::arbitrary_take_rest(u); |
| 1286 |
|
| 1287 |
if result.is_ok() { |
| 1288 |
assert!( |
| 1289 |
len_before >= min, |
| 1290 |
"incorrect minimum size: indicated {}, worked with {}", |
| 1291 |
min, |
| 1292 |
len_before |
| 1293 |
); |
| 1294 |
} |
| 1295 |
|
| 1296 |
result |
| 1297 |
} |
| 1298 |
|
| 1299 |
#[test] |
| 1300 |
fn finite_buffer_fill_buffer() { |
| 1301 |
let x = [1, 2, 3, 4]; |
| 1302 |
let mut rb = Unstructured::new(&x); |
| 1303 |
let mut z = [0; 2]; |
| 1304 |
rb.fill_buffer(&mut z).unwrap(); |
| 1305 |
assert_eq!(z, [1, 2]); |
| 1306 |
rb.fill_buffer(&mut z).unwrap(); |
| 1307 |
assert_eq!(z, [3, 4]); |
| 1308 |
rb.fill_buffer(&mut z).unwrap(); |
| 1309 |
assert_eq!(z, [0, 0]); |
| 1310 |
} |
| 1311 |
|
| 1312 |
#[test] |
| 1313 |
fn arbitrary_for_integers() { |
| 1314 |
let x = [1, 2, 3, 4]; |
| 1315 |
let mut buf = Unstructured::new(&x); |
| 1316 |
let expected = 1 | (2 << 8) | (3 << 16) | (4 << 24); |
| 1317 |
let actual = checked_arbitrary::<i32>(&mut buf).unwrap(); |
| 1318 |
assert_eq!(expected, actual); |
| 1319 |
|
| 1320 |
assert_generates([ |
| 1321 |
i32::from_ne_bytes([0, 0, 0, 0]), |
| 1322 |
i32::from_ne_bytes([0, 0, 0, 1]), |
| 1323 |
i32::from_ne_bytes([0, 0, 1, 0]), |
| 1324 |
i32::from_ne_bytes([0, 1, 0, 0]), |
| 1325 |
i32::from_ne_bytes([1, 0, 0, 0]), |
| 1326 |
i32::from_ne_bytes([1, 1, 1, 1]), |
| 1327 |
i32::from_ne_bytes([0xff, 0xff, 0xff, 0xff]), |
| 1328 |
]); |
| 1329 |
} |
| 1330 |
|
| 1331 |
#[test] |
| 1332 |
fn arbitrary_for_bytes() { |
| 1333 |
let x = [1, 2, 3, 4, 4]; |
| 1334 |
let mut buf = Unstructured::new(&x); |
| 1335 |
let expected = &[1, 2, 3, 4]; |
| 1336 |
let actual = checked_arbitrary::<&[u8]>(&mut buf).unwrap(); |
| 1337 |
assert_eq!(expected, actual); |
| 1338 |
} |
| 1339 |
|
| 1340 |
#[test] |
| 1341 |
fn arbitrary_take_rest_for_bytes() { |
| 1342 |
let x = [1, 2, 3, 4]; |
| 1343 |
let buf = Unstructured::new(&x); |
| 1344 |
let expected = &[1, 2, 3, 4]; |
| 1345 |
let actual = checked_arbitrary_take_rest::<&[u8]>(buf).unwrap(); |
| 1346 |
assert_eq!(expected, actual); |
| 1347 |
} |
| 1348 |
|
| 1349 |
#[test] |
| 1350 |
fn arbitrary_for_vec_u8() { |
| 1351 |
assert_generates::<Vec<u8>>([ |
| 1352 |
vec![], |
| 1353 |
vec![0], |
| 1354 |
vec![1], |
| 1355 |
vec![0, 0], |
| 1356 |
vec![0, 1], |
| 1357 |
vec![1, 0], |
| 1358 |
vec![1, 1], |
| 1359 |
vec![0, 0, 0], |
| 1360 |
vec![0, 0, 1], |
| 1361 |
vec![0, 1, 0], |
| 1362 |
vec![0, 1, 1], |
| 1363 |
vec![1, 0, 0], |
| 1364 |
vec![1, 0, 1], |
| 1365 |
vec![1, 1, 0], |
| 1366 |
vec![1, 1, 1], |
| 1367 |
]); |
| 1368 |
} |
| 1369 |
|
| 1370 |
#[test] |
| 1371 |
fn arbitrary_for_vec_vec_u8() { |
| 1372 |
assert_generates::<Vec<Vec<u8>>>([ |
| 1373 |
vec![], |
| 1374 |
vec![vec![]], |
| 1375 |
vec![vec![0]], |
| 1376 |
vec![vec![1]], |
| 1377 |
vec![vec![0, 1]], |
| 1378 |
vec![vec![], vec![]], |
| 1379 |
vec![vec![0], vec![]], |
| 1380 |
vec![vec![], vec![1]], |
| 1381 |
vec![vec![0], vec![1]], |
| 1382 |
vec![vec![0, 1], vec![]], |
| 1383 |
vec![vec![], vec![1, 0]], |
| 1384 |
vec![vec![], vec![], vec![]], |
| 1385 |
]); |
| 1386 |
} |
| 1387 |
|
| 1388 |
#[test] |
| 1389 |
fn arbitrary_for_vec_vec_vec_u8() { |
| 1390 |
assert_generates::<Vec<Vec<Vec<u8>>>>([ |
| 1391 |
vec![], |
| 1392 |
vec![vec![]], |
| 1393 |
vec![vec![vec![0]]], |
| 1394 |
vec![vec![vec![1]]], |
| 1395 |
vec![vec![vec![0, 1]]], |
| 1396 |
vec![vec![], vec![]], |
| 1397 |
vec![vec![], vec![vec![]]], |
| 1398 |
vec![vec![vec![]], vec![]], |
| 1399 |
vec![vec![vec![]], vec![vec![]]], |
| 1400 |
vec![vec![vec![0]], vec![]], |
| 1401 |
vec![vec![], vec![vec![1]]], |
| 1402 |
vec![vec![vec![0]], vec![vec![1]]], |
| 1403 |
vec![vec![vec![0, 1]], vec![]], |
| 1404 |
vec![vec![], vec![vec![0, 1]]], |
| 1405 |
vec![vec![], vec![], vec![]], |
| 1406 |
vec![vec![vec![]], vec![], vec![]], |
| 1407 |
vec![vec![], vec![vec![]], vec![]], |
| 1408 |
vec![vec![], vec![], vec![vec![]]], |
| 1409 |
]); |
| 1410 |
} |
| 1411 |
|
| 1412 |
#[test] |
| 1413 |
fn arbitrary_for_string() { |
| 1414 |
assert_generates::<String>(["".into(), "a".into(), "aa".into(), "aaa".into()]); |
| 1415 |
} |
| 1416 |
|
| 1417 |
#[test] |
| 1418 |
fn arbitrary_collection() { |
| 1419 |
let x = [ |
| 1420 |
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, |
| 1421 |
]; |
| 1422 |
assert_eq!( |
| 1423 |
checked_arbitrary::<&[u8]>(&mut Unstructured::new(&x)).unwrap(), |
| 1424 |
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3] |
| 1425 |
); |
| 1426 |
assert_eq!( |
| 1427 |
checked_arbitrary::<Vec<u8>>(&mut Unstructured::new(&x)).unwrap(), |
| 1428 |
&[2, 4, 6, 8, 1] |
| 1429 |
); |
| 1430 |
assert_eq!( |
| 1431 |
&*checked_arbitrary::<Box<[u8]>>(&mut Unstructured::new(&x)).unwrap(), |
| 1432 |
&[2, 4, 6, 8, 1] |
| 1433 |
); |
| 1434 |
assert_eq!( |
| 1435 |
&*checked_arbitrary::<Arc<[u8]>>(&mut Unstructured::new(&x)).unwrap(), |
| 1436 |
&[2, 4, 6, 8, 1] |
| 1437 |
); |
| 1438 |
assert_eq!( |
| 1439 |
&*checked_arbitrary::<Rc<[u8]>>(&mut Unstructured::new(&x)).unwrap(), |
| 1440 |
&[2, 4, 6, 8, 1] |
| 1441 |
); |
| 1442 |
assert_eq!( |
| 1443 |
checked_arbitrary::<Vec<u32>>(&mut Unstructured::new(&x)).unwrap(), |
| 1444 |
&[84148994] |
| 1445 |
); |
| 1446 |
assert_eq!( |
| 1447 |
checked_arbitrary::<String>(&mut Unstructured::new(&x)).unwrap(), |
| 1448 |
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03" |
| 1449 |
); |
| 1450 |
} |
| 1451 |
|
| 1452 |
#[test] |
| 1453 |
fn arbitrary_take_rest() { |
| 1454 |
// Basic examples |
| 1455 |
let x = [1, 2, 3, 4]; |
| 1456 |
assert_eq!( |
| 1457 |
checked_arbitrary_take_rest::<&[u8]>(Unstructured::new(&x)).unwrap(), |
| 1458 |
&[1, 2, 3, 4] |
| 1459 |
); |
| 1460 |
assert_eq!( |
| 1461 |
checked_arbitrary_take_rest::<Vec<u8>>(Unstructured::new(&x)).unwrap(), |
| 1462 |
&[2, 4] |
| 1463 |
); |
| 1464 |
assert_eq!( |
| 1465 |
&*checked_arbitrary_take_rest::<Box<[u8]>>(Unstructured::new(&x)).unwrap(), |
| 1466 |
&[2, 4] |
| 1467 |
); |
| 1468 |
assert_eq!( |
| 1469 |
&*checked_arbitrary_take_rest::<Arc<[u8]>>(Unstructured::new(&x)).unwrap(), |
| 1470 |
&[2, 4] |
| 1471 |
); |
| 1472 |
assert_eq!( |
| 1473 |
&*checked_arbitrary_take_rest::<Rc<[u8]>>(Unstructured::new(&x)).unwrap(), |
| 1474 |
&[2, 4] |
| 1475 |
); |
| 1476 |
assert_eq!( |
| 1477 |
checked_arbitrary_take_rest::<Vec<u32>>(Unstructured::new(&x)).unwrap(), |
| 1478 |
&[0x040302] |
| 1479 |
); |
| 1480 |
assert_eq!( |
| 1481 |
checked_arbitrary_take_rest::<String>(Unstructured::new(&x)).unwrap(), |
| 1482 |
"\x01\x02\x03\x04" |
| 1483 |
); |
| 1484 |
|
| 1485 |
// Empty remainder |
| 1486 |
assert_eq!( |
| 1487 |
checked_arbitrary_take_rest::<&[u8]>(Unstructured::new(&[])).unwrap(), |
| 1488 |
&[] |
| 1489 |
); |
| 1490 |
assert_eq!( |
| 1491 |
checked_arbitrary_take_rest::<Vec<u8>>(Unstructured::new(&[])).unwrap(), |
| 1492 |
&[] |
| 1493 |
); |
| 1494 |
|
| 1495 |
// Cannot consume all but can consume part of the input |
| 1496 |
assert_eq!( |
| 1497 |
checked_arbitrary_take_rest::<String>(Unstructured::new(&[1, 0xFF, 2])).unwrap(), |
| 1498 |
"\x01" |
| 1499 |
); |
| 1500 |
} |
| 1501 |
|
| 1502 |
#[test] |
| 1503 |
fn size_hint_for_tuples() { |
| 1504 |
assert_eq!( |
| 1505 |
(7, Some(7)), |
| 1506 |
<(bool, u16, i32) as Arbitrary<'_>>::size_hint(0) |
| 1507 |
); |
| 1508 |
assert_eq!((1, None), <(u8, Vec<u8>) as Arbitrary>::size_hint(0)); |
| 1509 |
} |
| 1510 |
} |
| 1511 |
|
| 1512 |
/// Multiple conflicting arbitrary attributes are used on the same field: |
| 1513 |
/// ```compile_fail |
| 1514 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1515 |
/// struct Point { |
| 1516 |
/// #[arbitrary(value = 2)] |
| 1517 |
/// #[arbitrary(value = 2)] |
| 1518 |
/// x: i32, |
| 1519 |
/// } |
| 1520 |
/// ``` |
| 1521 |
/// |
| 1522 |
/// An unknown attribute: |
| 1523 |
/// ```compile_fail |
| 1524 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1525 |
/// struct Point { |
| 1526 |
/// #[arbitrary(unknown_attr)] |
| 1527 |
/// x: i32, |
| 1528 |
/// } |
| 1529 |
/// ``` |
| 1530 |
/// |
| 1531 |
/// An unknown attribute with a value: |
| 1532 |
/// ```compile_fail |
| 1533 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1534 |
/// struct Point { |
| 1535 |
/// #[arbitrary(unknown_attr = 13)] |
| 1536 |
/// x: i32, |
| 1537 |
/// } |
| 1538 |
/// ``` |
| 1539 |
/// |
| 1540 |
/// `value` without RHS: |
| 1541 |
/// ```compile_fail |
| 1542 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1543 |
/// struct Point { |
| 1544 |
/// #[arbitrary(value)] |
| 1545 |
/// x: i32, |
| 1546 |
/// } |
| 1547 |
/// ``` |
| 1548 |
/// |
| 1549 |
/// `with` without RHS: |
| 1550 |
/// ```compile_fail |
| 1551 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1552 |
/// struct Point { |
| 1553 |
/// #[arbitrary(with)] |
| 1554 |
/// x: i32, |
| 1555 |
/// } |
| 1556 |
/// ``` |
| 1557 |
/// |
| 1558 |
/// Multiple conflicting bounds at the container-level: |
| 1559 |
/// ```compile_fail |
| 1560 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1561 |
/// #[arbitrary(bound = "T: Default")] |
| 1562 |
/// #[arbitrary(bound = "T: Default")] |
| 1563 |
/// struct Point<T: Default> { |
| 1564 |
/// #[arbitrary(default)] |
| 1565 |
/// x: T, |
| 1566 |
/// } |
| 1567 |
/// ``` |
| 1568 |
/// |
| 1569 |
/// Multiple conflicting bounds in a single bound attribute: |
| 1570 |
/// ```compile_fail |
| 1571 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1572 |
/// #[arbitrary(bound = "T: Default, T: Default")] |
| 1573 |
/// struct Point<T: Default> { |
| 1574 |
/// #[arbitrary(default)] |
| 1575 |
/// x: T, |
| 1576 |
/// } |
| 1577 |
/// ``` |
| 1578 |
/// |
| 1579 |
/// Multiple conflicting bounds in multiple bound attributes: |
| 1580 |
/// ```compile_fail |
| 1581 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1582 |
/// #[arbitrary(bound = "T: Default", bound = "T: Default")] |
| 1583 |
/// struct Point<T: Default> { |
| 1584 |
/// #[arbitrary(default)] |
| 1585 |
/// x: T, |
| 1586 |
/// } |
| 1587 |
/// ``` |
| 1588 |
/// |
| 1589 |
/// Too many bounds supplied: |
| 1590 |
/// ```compile_fail |
| 1591 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1592 |
/// #[arbitrary(bound = "T: Default")] |
| 1593 |
/// struct Point { |
| 1594 |
/// x: i32, |
| 1595 |
/// } |
| 1596 |
/// ``` |
| 1597 |
/// |
| 1598 |
/// Too many bounds supplied across multiple attributes: |
| 1599 |
/// ```compile_fail |
| 1600 |
/// #[derive(::arbitrary::Arbitrary)] |
| 1601 |
/// #[arbitrary(bound = "T: Default")] |
| 1602 |
/// #[arbitrary(bound = "U: Default")] |
| 1603 |
/// struct Point<T: Default> { |
| 1604 |
/// #[arbitrary(default)] |
| 1605 |
/// x: T, |
| 1606 |
/// } |
| 1607 |
/// ``` |
| 1608 |
#[cfg(all(doctest, feature = "derive"))] |
| 1609 |
pub struct CompileFailTests; |