Lines Matching full:arbitrary
9 //! The `Arbitrary` trait crate.
11 //! This trait provides an [`Arbitrary`](./trait.Arbitrary.html) trait to
14 //! [`Arbitrary`](./trait.Arbitrary.html) trait's documentation for details on
57 /// Generate arbitrary structured values from raw, unstructured data.
59 /// The `Arbitrary` trait allows you to generate valid structured values, like
63 /// # Deriving `Arbitrary`
65 /// Automatically deriving the `Arbitrary` trait is the recommended way to
66 /// implement `Arbitrary` for your types.
73 /// arbitrary = { version = "1", features = ["derive"] }
76 /// Then, you add the `#[derive(Arbitrary)]` annotation to your `struct` or
81 /// use arbitrary::Arbitrary;
84 /// #[derive(Arbitrary)]
89 /// #[derive(Arbitrary, Hash, Eq, PartialEq)]
97 /// Every member of the `struct` or `enum` must also implement `Arbitrary`.
99 /// # Implementing `Arbitrary` By Hand
101 /// Implementing `Arbitrary` mostly involves nested calls to other `Arbitrary`
102 /// arbitrary implementations for each of your `struct` or `enum`'s members. But
114 /// use arbitrary::{Arbitrary, Result, Unstructured};
116 /// impl<'a, T> Arbitrary<'a> for MyCollection<T>
118 /// T: Arbitrary<'a>,
120 /// fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
121 /// // Get an iterator of arbitrary `T`s.
136 pub trait Arbitrary<'a>: Sized { interface
137 /// Generate an arbitrary value of `Self` from the given unstructured data.
139 /// Calling `Arbitrary::arbitrary` requires that you have some raw data,
142 /// Arbitrary>::arbitrary` to construct an arbitrary instance of `MyType`
157 /// use arbitrary::{Arbitrary, Unstructured};
159 /// #[derive(Arbitrary)]
171 /// // Generate an arbitrary instance of `MyType` and do stuff with it.
172 /// if let Ok(value) = MyType::arbitrary(&mut unstructured) {
180 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>; in arbitrary() method
182 /// Generate an arbitrary value of `Self` from the entirety of the given
185 /// This is similar to Arbitrary::arbitrary, however it assumes that it is
190 Self::arbitrary(&mut u) in arbitrary_take_rest()
197 /// creating an arbitrary collection.
205 /// type, but not ultimately that useful. Using `#[derive(Arbitrary)]` will
206 /// create a better implementation. If you are writing an `Arbitrary`
216 /// [`Arbitrary::arbitrary`] and [`Arbitrary::arbitrary_take_rest`].
225 /// If you 100% know that the type you are implementing `Arbitrary` for is
228 /// Note that if you are implementing `Arbitrary` for a generic type, you
232 /// [`arbitrary::size_hint::recursion_guard(depth)`][crate::size_hint::recursion_guard]
237 /// use arbitrary::{Arbitrary, Unstructured, size_hint};
246 /// impl<'a, L, R> Arbitrary<'a> for MyEither<L, R>
248 /// L: Arbitrary<'a>,
249 /// R: Arbitrary<'a>,
251 /// fn arbitrary(u: &mut Unstructured) -> arbitrary::Result<Self> {
267 /// <L as Arbitrary>::size_hint(depth),
268 /// <R as Arbitrary>::size_hint(depth),
283 impl<'a> Arbitrary<'a> for () { implementation
284 fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
294 impl<'a> Arbitrary<'a> for bool {
295 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
296 Ok(<u8 as Arbitrary<'a>>::arbitrary(u)? & 1 == 1) in arbitrary()
301 <u8 as Arbitrary<'a>>::size_hint(depth) in size_hint()
308 impl<'a> Arbitrary<'a> for $ty { impl
309 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
348 impl<'a> Arbitrary<'a> for $ty { impl
349 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
350 Ok(Self::from_bits(<$unsigned as Arbitrary<'a>>::arbitrary(u)?))
355 <$unsigned as Arbitrary<'a>>::size_hint(depth)
367 impl<'a> Arbitrary<'a> for char {
368 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
374 let mut c = <u32 as Arbitrary<'a>>::arbitrary(u)? % CHAR_END; in arbitrary()
381 .expect("Generated character should be valid! This is a bug in arbitrary-rs")) in arbitrary()
387 <u32 as Arbitrary<'a>>::size_hint(depth) in size_hint()
391 impl<'a> Arbitrary<'a> for AtomicBool {
392 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
393 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
398 <bool as Arbitrary<'a>>::size_hint(depth) in size_hint()
402 impl<'a> Arbitrary<'a> for AtomicIsize {
403 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
404 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
409 <isize as Arbitrary<'a>>::size_hint(depth) in size_hint()
413 impl<'a> Arbitrary<'a> for AtomicUsize {
414 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
415 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
420 <usize as Arbitrary<'a>>::size_hint(depth) in size_hint()
432 impl<'a, A> Arbitrary<'a> for $range impl
434 A: Arbitrary<'a> + Clone + PartialOrd,
436 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
437 let value: $value_ty = Arbitrary::arbitrary(u)?;
455 <A as Arbitrary>::size_hint(depth),
456 <A as Arbitrary>::size_hint(depth)
464 |depth| <A as Arbitrary>::size_hint(depth)
472 <A as Arbitrary>::size_hint(depth),
473 <A as Arbitrary>::size_hint(depth)
481 |depth| <A as Arbitrary>::size_hint(depth)
488 |depth| <A as Arbitrary>::size_hint(depth)
512 impl<'a> Arbitrary<'a> for Duration {
513 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
515 <u64 as Arbitrary>::arbitrary(u)?, in arbitrary()
523 <u64 as Arbitrary>::size_hint(depth), in size_hint()
524 <u32 as Arbitrary>::size_hint(depth), in size_hint()
529 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Option<A> {
530 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
531 Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? { in arbitrary()
532 Some(Arbitrary::arbitrary(u)?) in arbitrary()
541 <bool as Arbitrary>::size_hint(depth), in size_hint()
542 crate::size_hint::or((0, Some(0)), <A as Arbitrary>::size_hint(depth)), in size_hint()
547 impl<'a, A: Arbitrary<'a>, B: Arbitrary<'a>> Arbitrary<'a> for std::result::Result<A, B> {
548 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
549 Ok(if <bool as Arbitrary<'a>>::arbitrary(u)? { in arbitrary()
550 Ok(<A as Arbitrary>::arbitrary(u)?) in arbitrary()
552 Err(<B as Arbitrary>::arbitrary(u)?) in arbitrary()
559 <bool as Arbitrary>::size_hint(depth), in size_hint()
561 <A as Arbitrary>::size_hint(depth), in size_hint()
562 <B as Arbitrary>::size_hint(depth), in size_hint()
573 impl<'a, $($xs: Arbitrary<'a>,)* $last: Arbitrary<'a>> Arbitrary<'a> for ($($xs,)* $last,) {
574 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
575 Ok(($($xs::arbitrary(u)?,)* Arbitrary::arbitrary(u)?,))
580 $(let $xs = $xs::arbitrary(&mut u)?;)*
588 <$last as Arbitrary>::size_hint(depth),
589 $( <$xs as Arbitrary>::size_hint(depth) ),*
635 impl<'a, T, const N: usize> Arbitrary<'a> for [T; N] impl
637 T: Arbitrary<'a>,
640 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() function
641 try_create_array(|_| <T as Arbitrary<'a>>::arbitrary(u)) in arbitrary()
646 let mut array = Self::arbitrary(&mut u)?; in arbitrary_take_rest()
648 *last = Arbitrary::arbitrary_take_rest(u)?; in arbitrary_take_rest()
656 <T as Arbitrary>::size_hint(d) in size_hint()
661 impl<'a> Arbitrary<'a> for &'a [u8] { impl
662 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
677 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Vec<A> {
678 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
692 impl<'a, K: Arbitrary<'a> + Ord, V: Arbitrary<'a>> Arbitrary<'a> for BTreeMap<K, V> {
693 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
707 impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BTreeSet<A> {
708 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
722 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Bound<A> {
723 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
725 0 => Ok(Bound::Included(A::arbitrary(u)?)), in arbitrary()
726 1 => Ok(Bound::Excluded(A::arbitrary(u)?)), in arbitrary()
741 impl<'a, A: Arbitrary<'a> + Ord> Arbitrary<'a> for BinaryHeap<A> {
742 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
756 impl<'a, K: Arbitrary<'a> + Eq + ::std::hash::Hash, V: Arbitrary<'a>, S: BuildHasher + Default>
757 Arbitrary<'a> for HashMap<K, V, S>
759 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
773 impl<'a, A: Arbitrary<'a> + Eq + ::std::hash::Hash, S: BuildHasher + Default> Arbitrary<'a>
776 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
790 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for LinkedList<A> {
791 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
805 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for VecDeque<A> {
806 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
820 impl<'a, A> Arbitrary<'a> for Cow<'a, A>
823 <A as ToOwned>::Owned: Arbitrary<'a>,
825 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() function
826 Arbitrary::arbitrary(u).map(Cow::Owned) in arbitrary()
832 <<A as ToOwned>::Owned as Arbitrary>::size_hint(depth) in size_hint()
837 impl<'a> Arbitrary<'a> for &'a str { impl
838 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
868 impl<'a> Arbitrary<'a> for String {
869 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
870 <&str as Arbitrary>::arbitrary(u).map(Into::into) in arbitrary()
874 <&str as Arbitrary>::arbitrary_take_rest(u).map(Into::into) in arbitrary_take_rest()
879 <&str as Arbitrary>::size_hint(depth) in size_hint()
883 impl<'a> Arbitrary<'a> for CString {
884 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
885 <Vec<u8> as Arbitrary>::arbitrary(u).map(|mut x| { in arbitrary()
893 <Vec<u8> as Arbitrary>::size_hint(depth) in size_hint()
897 impl<'a> Arbitrary<'a> for OsString {
898 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
899 <String as Arbitrary>::arbitrary(u).map(From::from) in arbitrary()
904 <String as Arbitrary>::size_hint(depth) in size_hint()
908 impl<'a> Arbitrary<'a> for PathBuf {
909 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
910 <OsString as Arbitrary>::arbitrary(u).map(From::from) in arbitrary()
915 <OsString as Arbitrary>::size_hint(depth) in size_hint()
919 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<A> {
920 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
921 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
926 crate::size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint) in size_hint()
930 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Box<[A]> {
931 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
932 <Vec<A> as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_slice()) in arbitrary()
937 <Vec<A> as Arbitrary>::size_hint(depth) in size_hint()
941 impl<'a> Arbitrary<'a> for Box<str> {
942 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
943 <String as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_str()) in arbitrary()
948 <String as Arbitrary>::size_hint(depth) in size_hint()
952 // impl Arbitrary for Box<CStr> {
953 // fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> {
954 // <CString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_c_str())
958 // impl Arbitrary for Box<OsStr> {
959 // fn arbitrary(u: &mut Unstructured<'_>) -> Result<Self> {
960 // <OsString as Arbitrary>::arbitrary(u).map(|x| x.into_boxed_osstr())
965 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Arc<A> {
966 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
967 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
972 crate::size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint) in size_hint()
976 impl<'a> Arbitrary<'a> for Arc<str> {
977 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
978 <&str as Arbitrary>::arbitrary(u).map(Into::into) in arbitrary()
983 <&str as Arbitrary>::size_hint(depth) in size_hint()
987 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Rc<A> {
988 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
989 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
994 crate::size_hint::recursion_guard(depth, <A as Arbitrary>::size_hint) in size_hint()
998 impl<'a> Arbitrary<'a> for Rc<str> {
999 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1000 <&str as Arbitrary>::arbitrary(u).map(Into::into) in arbitrary()
1005 <&str as Arbitrary>::size_hint(depth) in size_hint()
1009 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Cell<A> {
1010 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1011 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
1016 <A as Arbitrary<'a>>::size_hint(depth) in size_hint()
1020 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for RefCell<A> {
1021 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1022 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
1027 <A as Arbitrary<'a>>::size_hint(depth) in size_hint()
1031 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for UnsafeCell<A> {
1032 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1033 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
1038 <A as Arbitrary<'a>>::size_hint(depth) in size_hint()
1042 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for Mutex<A> {
1043 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1044 Arbitrary::arbitrary(u).map(Self::new) in arbitrary()
1049 <A as Arbitrary<'a>>::size_hint(depth) in size_hint()
1053 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for iter::Empty<A> {
1054 fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1064 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::marker::PhantomData<A> {
1065 fn arbitrary(_: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1075 impl<'a, A: Arbitrary<'a>> Arbitrary<'a> for ::std::num::Wrapping<A> {
1076 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1077 Arbitrary::arbitrary(u).map(::std::num::Wrapping) in arbitrary()
1082 <A as Arbitrary<'a>>::size_hint(depth) in size_hint()
1088 impl<'a> Arbitrary<'a> for $nonzero { impl
1089 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> {
1090 match Self::new(<$int as Arbitrary<'a>>::arbitrary(u)?) {
1098 <$int as Arbitrary<'a>>::size_hint(depth)
1117 impl<'a> Arbitrary<'a> for Ipv4Addr {
1118 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1119 Ok(Ipv4Addr::from(u32::arbitrary(u)?)) in arbitrary()
1128 impl<'a> Arbitrary<'a> for Ipv6Addr {
1129 fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self> { in arbitrary() method
1130 Ok(Ipv6Addr::from(u128::arbitrary(u)?)) in arbitrary()
1143 /// Generates an arbitrary `T`, and checks that the result is consistent with the
1145 fn checked_arbitrary<'a, T: Arbitrary<'a>>(u: &mut Unstructured<'a>) -> Result<T> { in checked_arbitrary()
1149 let result = T::arbitrary(u); in checked_arbitrary()
1174 /// Like `checked_arbitrary()`, but calls `arbitrary_take_rest()` instead of `arbitrary()`.
1175 fn checked_arbitrary_take_rest<'a, T: Arbitrary<'a>>(u: Unstructured<'a>) -> Result<T> { in checked_arbitrary_take_rest()
1290 <(bool, u16, i32) as Arbitrary<'_>>::size_hint(0) in size_hint_for_tuples()
1292 assert_eq!((1, None), <(u8, Vec<u8>) as Arbitrary>::size_hint(0)); in size_hint_for_tuples()
1296 /// Multiple conflicting arbitrary attributes are used on the same field:
1298 /// #[derive(::arbitrary::Arbitrary)]
1300 /// #[arbitrary(value = 2)]
1301 /// #[arbitrary(value = 2)]
1308 /// #[derive(::arbitrary::Arbitrary)]
1310 /// #[arbitrary(unknown_attr)]
1317 /// #[derive(::arbitrary::Arbitrary)]
1319 /// #[arbitrary(unknown_attr = 13)]
1326 /// #[derive(::arbitrary::Arbitrary)]
1328 /// #[arbitrary(value)]
1335 /// #[derive(::arbitrary::Arbitrary)]
1337 /// #[arbitrary(with)]