1#![deny(bad_style)]
14#![deny(future_incompatible)]
16#![deny(nonstandard_style)]
17#![deny(rust_2018_compatibility)]
18#![deny(rust_2018_idioms)]
19#![allow(unused)]
21
22#[cfg(feature = "derive")]
23pub use derive_dearbitrary::*;
24
25mod error;
26pub use error::*;
27
28use core::array;
29use core::cell::{Cell, RefCell, UnsafeCell};
30use core::iter;
31use core::mem;
32use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
33use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
34use core::ops::{Range, RangeBounds, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive};
35use core::str;
36use core::time::Duration;
37use std::borrow::{Cow, ToOwned};
38use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};
39use std::ffi::{CString, OsString};
40use std::hash::BuildHasher;
41use std::net::{Ipv4Addr, Ipv6Addr};
42use std::ops::Bound;
43use std::path::PathBuf;
44use std::rc::Rc;
45use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize};
46use std::sync::{Arc, Mutex};
47
48#[derive(Default)]
49pub struct Dearbitrator {
50 data: VecDeque<u8>,
51}
52
53impl Dearbitrator {
54 pub fn new() -> Self {
55 Dearbitrator::default()
56 }
57
58 pub fn len(&self) -> usize {
59 self.data.len()
60 }
61
62 pub fn is_empty(&self) -> bool {
63 self.data.is_empty()
64 }
65
66 pub fn push_rev_iter<I: Iterator>(&mut self, iter: I)
67 where
68 <I as Iterator>::Item: Dearbitrary,
69 {
70 false.dearbitrary(self);
71 for v in iter {
72 v.dearbitrary(self);
73 true.dearbitrary(self);
74 }
75 }
76
77 pub fn push_rev_iter_first<I: Iterator>(mut iter: I) -> Dearbitrator
78 where
79 <I as Iterator>::Item: Dearbitrary,
80 {
81 let mut d = Dearbitrator::new();
82 d.push_rev_iter(iter);
83 d
84 }
94
95 pub fn push_bytes(&mut self, data: &[u8]) {
96 for b in data.iter().rev() {
97 self.data.push_front(*b)
98 }
99 }
100
101 pub fn push_len(&mut self, len: usize) {
102 if self.data.len() as u64 <= std::u8::MAX as u64 {
103 let len = len as u8;
104 for b in len.to_be_bytes() {
105 self.data.push_back(b);
106 }
107 } else if self.data.len() as u64 <= std::u16::MAX as u64 {
108 let len = len as u16;
109 for b in len.to_be_bytes() {
110 self.data.push_back(b);
111 }
112 } else if self.data.len() as u64 <= std::u32::MAX as u64 {
113 let len = len as u32;
114 for b in len.to_be_bytes() {
115 self.data.push_back(b);
116 }
117 } else {
118 let len = len as u64;
119 for b in len.to_be_bytes() {
120 self.data.push_back(b);
121 }
122 };
123 }
124
125 pub fn finish(self) -> Vec<u8> {
126 self.data.into()
127 }
128}
129
130pub trait Dearbitrary {
131 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator);
132
133 fn dearbitrary_first(&self) -> Dearbitrator {
134 let mut d = Dearbitrator::new();
135 self.dearbitrary(&mut d);
136 d
137 }
138}
139
140impl<T: Dearbitrary> Dearbitrary for &T {
141 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
142 (*self).dearbitrary(dearbitrator)
143 }
144
145 fn dearbitrary_first(&self) -> Dearbitrator {
146 (*self).dearbitrary_first()
147 }
148}
149
150impl Dearbitrary for () {
151 fn dearbitrary(&self, _dearbitrator: &mut Dearbitrator) {}
152}
153
154impl Dearbitrary for bool {
155 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
156 (*self as u8).dearbitrary(dearbitrator);
157 }
158}
159
160macro_rules! impl_dearbitrary_for_integers {
161 ( $( $ty:ty: $unsigned:ty; )* ) => {
162 $(
163 impl Dearbitrary for $ty {
164 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
165 let mut buf = [0; mem::size_of::<$ty>()];
166 let x: $unsigned = *self as $unsigned;
167 for i in 0..mem::size_of::<$ty>() {
168 buf[i] = ((x >> ( i * 8 )) & 0xff) as u8;
169 }
170 dearbitrator.push_bytes(&buf);
171 }
172 }
173 )*
174 }
175}
176
177impl_dearbitrary_for_integers! {
178 u8: u8;
179 u16: u16;
180 u32: u32;
181 u64: u64;
182 u128: u128;
183 usize: usize;
184 i8: u8;
185 i16: u16;
186 i32: u32;
187 i64: u64;
188 i128: u128;
189 isize: usize;
190}
191
192macro_rules! impl_dearbitrary_for_floats {
193 ( $( $ty:ident : $unsigned:ty; )* ) => {
194 $(
195 impl Dearbitrary for $ty {
196 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
197 ((*self).to_bits()).dearbitrary(dearbitrator);
198 }
199 }
200
201 )*
202 }
203}
204
205impl_dearbitrary_for_floats! {
206 f32: u32;
207 f64: u64;
208}
209
210impl Dearbitrary for char {
211 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
212 (*self as u32).dearbitrary(dearbitrator);
213 }
214}
215
216impl Dearbitrary for AtomicBool {
217 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
218 (self.load(std::sync::atomic::Ordering::SeqCst)).dearbitrary(dearbitrator);
219 }
220}
221
222impl Dearbitrary for AtomicIsize {
223 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
224 (self.load(std::sync::atomic::Ordering::SeqCst)).dearbitrary(dearbitrator);
225 }
226}
227
228impl Dearbitrary for AtomicUsize {
229 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
230 (self.load(std::sync::atomic::Ordering::SeqCst)).dearbitrary(dearbitrator);
231 }
232}
233
234pub(crate) fn bounded_range<CB, I, R>(bounds: (I, I), cb: CB) -> R
235where
236 CB: Fn((I, I)) -> R,
237 I: PartialOrd,
238 R: RangeBounds<I>,
239{
240 let (mut start, mut end) = bounds;
241 if start > end {
242 mem::swap(&mut start, &mut end);
243 }
244 cb((start, end))
245}
246
247pub(crate) fn unbounded_range<CB, I, R>(bound: I, cb: CB) -> R
248where
249 CB: Fn(I) -> R,
250 R: RangeBounds<I>,
251{
252 cb(bound)
253}
254
255impl<A: Dearbitrary> Dearbitrary for Option<A> {
256 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
257 match self {
258 Some(v) => {
259 v.dearbitrary(dearbitrator);
260 true.dearbitrary(dearbitrator);
261 }
262 None => false.dearbitrary(dearbitrator),
263 }
264 }
265}
266
267impl<A: Dearbitrary, B: Dearbitrary> Dearbitrary for std::result::Result<A, B> {
268 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
269 match self {
270 Ok(v) => {
271 v.dearbitrary(dearbitrator);
272 true.dearbitrary(dearbitrator);
273 }
274 Err(v) => {
275 v.dearbitrary(dearbitrator);
276 false.dearbitrary(dearbitrator);
277 }
278 }
279 }
280}
281
282macro_rules! arbitrary_tuple {
283 () => {};
284 ($ln: tt $last: ident $($n: tt $xs: ident)*) => {
285 arbitrary_tuple!($($n $xs)*);
286
287 impl<$($xs: Dearbitrary,)* $last: Dearbitrary> Dearbitrary for ($($xs,)* $last,) {
288 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
289 self.$ln.dearbitrary(dearbitrator);
290 $( self.$n.dearbitrary(dearbitrator); )*
291 }
292
293 fn dearbitrary_first(&self) -> Dearbitrator {
294 let mut dearbitrator = self.$ln.dearbitrary_first();
295 $( self.$n.dearbitrary(&mut dearbitrator); )*
296 dearbitrator
297 }
298
299 }
300
301
302 };
303}
304
305arbitrary_tuple!(25 A 24 B 23 C 22 D 21 E 20 F 19 G 18 H 17 I 16 J 15 K 14 L 13 M 12 N 11 O 10 P 9 Q 8 R 7 S 6 T 5 U 4 V 3 W 2 X 1 Y 0 Z);
306
307struct ArrayGuard<T, const N: usize> {
310 dst: *mut T,
311 initialized: usize,
312}
313
314impl<T, const N: usize> Drop for ArrayGuard<T, N> {
315 fn drop(&mut self) {
316 debug_assert!(self.initialized <= N);
317 let initialized_part = core::ptr::slice_from_raw_parts_mut(self.dst, self.initialized);
318 unsafe {
319 core::ptr::drop_in_place(initialized_part);
320 }
321 }
322}
323
324fn try_create_array<F, T, const N: usize>(mut cb: F) -> Result<[T; N]>
325where
326 F: FnMut(usize) -> Result<T>,
327{
328 let mut array: mem::MaybeUninit<[T; N]> = mem::MaybeUninit::uninit();
329 let array_ptr = array.as_mut_ptr();
330 let dst = array_ptr as _;
331 let mut guard: ArrayGuard<T, N> = ArrayGuard {
332 dst,
333 initialized: 0,
334 };
335 unsafe {
336 for (idx, value_ptr) in (*array.as_mut_ptr()).iter_mut().enumerate() {
337 core::ptr::write(value_ptr, cb(idx)?);
338 guard.initialized += 1;
339 }
340 mem::forget(guard);
341 Ok(array.assume_init())
342 }
343}
344
345impl<T: Dearbitrary, const N: usize> Dearbitrary for [T; N] {
346 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
347 for v in self.iter().rev() {
348 v.dearbitrary(dearbitrator)
349 }
350 }
351
352 fn dearbitrary_first(&self) -> Dearbitrator {
353 let mut d = if let Some(last) = self.last() {
355 last.dearbitrary_first()
356 } else {
357 Dearbitrator::new()
358 };
359 self.dearbitrary(&mut d);
360 d
361 }
362}
363
364impl<'a> Dearbitrary for &'a [u8] {
365 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
366 dearbitrator.push_bytes(self);
367 dearbitrator.push_len(self.len());
368 }
369
370 fn dearbitrary_first(&self) -> Dearbitrator {
371 let mut d = Dearbitrator::new();
372 d.push_bytes(self);
373 d
374 }
375}
376
377impl<A: Dearbitrary> Dearbitrary for Vec<A> {
378 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
379 dearbitrator.push_rev_iter(self.iter().rev())
380 }
381
382 fn dearbitrary_first(&self) -> Dearbitrator {
383 let d = Dearbitrator::push_rev_iter_first(self.iter().rev());
384 d
385 }
386}
387
388impl<K: Dearbitrary, V: Dearbitrary> Dearbitrary for BTreeMap<K, V> {
389 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
390 dearbitrator.push_rev_iter(self.iter().rev())
391 }
392
393 fn dearbitrary_first(&self) -> Dearbitrator {
394 let d = Dearbitrator::push_rev_iter_first(self.iter().rev());
395 d
396 }
397}
398
399impl<A: Dearbitrary> Dearbitrary for BTreeSet<A> {
400 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
401 dearbitrator.push_rev_iter(self.iter().rev())
402 }
403
404 fn dearbitrary_first(&self) -> Dearbitrator {
405 let d = Dearbitrator::push_rev_iter_first(self.iter().rev());
406 d
407 }
408}
409
410impl<A: Dearbitrary> Dearbitrary for BinaryHeap<A> {
411 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
412 dearbitrator.push_rev_iter(self.iter().rev())
413 }
414
415 fn dearbitrary_first(&self) -> Dearbitrator {
416 let d = Dearbitrator::push_rev_iter_first(self.iter().rev());
417 d
418 }
419}
420
421impl<A: Dearbitrary + Eq + ::std::hash::Hash, S: BuildHasher + Default> Dearbitrary
422 for HashSet<A, S>
423{
424 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
425 dearbitrator.push_rev_iter(self.iter()) }
427
428 fn dearbitrary_first(&self) -> Dearbitrator {
429 let d = Dearbitrator::push_rev_iter_first(self.iter());
430 d
431 }
432}
433
434impl<A: Dearbitrary> Dearbitrary for LinkedList<A> {
435 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
436 dearbitrator.push_rev_iter(self.iter()) }
438
439 fn dearbitrary_first(&self) -> Dearbitrator {
440 let d = Dearbitrator::push_rev_iter_first(self.iter().rev());
441 d
442 }
443}
444
445impl<A: Dearbitrary> Dearbitrary for VecDeque<A> {
446 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
447 dearbitrator.push_rev_iter(self.iter()) }
449
450 fn dearbitrary_first(&self) -> Dearbitrator {
451 let d = Dearbitrator::push_rev_iter_first(self.iter().rev());
452 d
453 }
454}
455
456impl<'a> Dearbitrary for &'a str {
457 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
458 self.as_bytes().dearbitrary(dearbitrator)
459 }
460
461 fn dearbitrary_first(&self) -> Dearbitrator {
462 let d = self.as_bytes().dearbitrary_first();
463 d
464 }
465}
466
467impl Dearbitrary for String {
468 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
469 (self as &str).dearbitrary(dearbitrator)
470 }
471
472 fn dearbitrary_first(&self) -> Dearbitrator {
473 (self as &str).dearbitrary_first()
474 }
475}
476
477impl Dearbitrary for Box<str> {
478 fn dearbitrary(&self, dearbitrator: &mut Dearbitrator) {
479 (self as &str).dearbitrary(dearbitrator)
480 }
481}
482
483#[cfg(test)]
484mod test {
485 use super::*;
486 use arbitrary::*;
487
488 macro_rules! assert_dearb_arb_eq {
489 ($v:expr, $t:ty) => {{
490 let x: $t = $v;
492 let bytes = x.dearbitrary_first().finish();
493
494 let mut u = Unstructured::new(&bytes);
495 let y = <$t>::arbitrary_take_rest(u).unwrap();
496 assert_eq!(x, y);
497
498 let x: $t = $v;
500 let mut d = Dearbitrator::new();
501 x.dearbitrary(&mut d);
502 let bytes = d.finish();
503
504 let mut u = Unstructured::new(&bytes);
505 let y = <$t>::arbitrary(&mut u).unwrap();
506 assert_eq!(x, y);
507 }};
508 }
509
510 #[test]
511 fn dearbitrary_for_integers() {
512 assert_dearb_arb_eq!(1 | (2 << 8) | (3 << 16) | (4 << 24), i32);
513 }
514
515 #[test]
516 fn dearbitrary_for_bytes() {
517 assert_dearb_arb_eq!(&[1, 2, 3, 4], &[u8]);
518 }
519
520 #[test]
521 fn dearbitrary_collection() {
522 assert_dearb_arb_eq!(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3], &[u8]);
523 assert_dearb_arb_eq!(vec![2, 4, 6, 8, 1], Vec<u16>);
524 assert_dearb_arb_eq!(vec![84148994], Vec<u32>);
525 assert_dearb_arb_eq!(vec![123; 100], Vec<usize>);
526 assert_dearb_arb_eq!(
527 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x01\x02\x03".to_string(),
528 String
529 );
530 }
531
532 #[test]
533 fn test_multiple_vecs_i32() {
534 assert_dearb_arb_eq!((vec![1, 2, 3], vec![10, 20, 21]), (Vec<i32>, Vec<i32>));
535 }
536
537 #[test]
538 fn test_multiple_vecs_u8() {
539 assert_dearb_arb_eq!((vec![1, 2, 3, 4], vec![10, 20, 21]), (Vec<u8>, Vec<u8>));
540 }
541
542 #[test]
543 fn test_optional() {
544 assert_dearb_arb_eq!(
545 Some((vec![1, 0xfffffff_u64, 3], 0xfffffff_u64, vec![10, 20])),
546 Option<(Vec<u64>, u64, Vec<u8>)>
547 );
548 }
549
550 #[test]
551 fn test_integers() {
552 assert_dearb_arb_eq!(-0x12345678_i32, i32);
553 assert_dearb_arb_eq!(0x12345678_u64, u64);
554 assert_dearb_arb_eq!(1.123f64, f64);
555 assert_dearb_arb_eq!(1.123f32, f32);
556 assert_dearb_arb_eq!(255, u8);
557 assert_dearb_arb_eq!(0x12345678, isize);
558 assert_dearb_arb_eq!(0x12345678, usize);
559 }
560
561 #[test]
562 fn test_strings() {
563 assert_dearb_arb_eq!("ABCDEFG", &str);
564 assert_dearb_arb_eq!("ABCDEFG".to_string(), String);
565 assert_dearb_arb_eq!("ABCDEFG".into(), Box<str>);
566 }
567
568 #[test]
569 fn test_deep_nest() {
570 assert_dearb_arb_eq!(
571 vec![(10u8, vec![10f32, 20f32]), (12u8, vec![100f32, 10000f32])],
572 Vec<(u8, Vec<f32>)>
573 );
574 assert_dearb_arb_eq!(
575 vec![(10u64, vec![10f32, 20f32]), (12u64, vec![100f32, 10000f32])],
576 Vec<(u64, Vec<f32>)>
577 );
578 }
579}