bolero_engine/
input.rs

1use crate::{driver, Driver};
2use bolero_generator::driver::ByteSliceDriver;
3use core::fmt;
4use pretty_hex::pretty_hex_write;
5use std::panic::RefUnwindSafe;
6
7pub trait Input<Output> {
8    type Driver: Driver + RefUnwindSafe;
9
10    /// Provide a slice of the test input
11    fn with_slice<F: FnMut(&[u8]) -> Output>(&mut self, f: &mut F) -> Output;
12
13    /// Provide a test driver for the test input
14    ///
15    /// Note: Drivers are used with `bolero_generator::ValueGenerator` implementations.
16    fn with_driver<F: FnMut(&mut Self::Driver) -> Output>(&mut self, f: &mut F) -> Output;
17}
18
19impl<'a, Output> Input<Output> for &'a [u8] {
20    type Driver = ByteSliceDriver<'a>;
21
22    fn with_slice<F: FnMut(&[u8]) -> Output>(&mut self, f: &mut F) -> Output {
23        f(self)
24    }
25
26    fn with_driver<F: FnMut(&mut Self::Driver) -> Output>(&mut self, f: &mut F) -> Output {
27        let options = driver::Options::default();
28        f(&mut ByteSliceDriver::new(self, &options))
29    }
30}
31
32#[derive(Clone, Copy, Debug)]
33pub struct Bytes<'a> {
34    slice: &'a [u8],
35    options: &'a driver::Options,
36}
37
38impl<'a> Bytes<'a> {
39    pub fn new(slice: &'a [u8], options: &'a driver::Options) -> Self {
40        Self { slice, options }
41    }
42}
43
44impl<'a, Output> Input<Output> for Bytes<'a> {
45    type Driver = ByteSliceDriver<'a>;
46
47    fn with_slice<F: FnMut(&[u8]) -> Output>(&mut self, f: &mut F) -> Output {
48        f(self.slice)
49    }
50
51    fn with_driver<F: FnMut(&mut Self::Driver) -> Output>(&mut self, f: &mut F) -> Output {
52        f(&mut ByteSliceDriver::new(self.slice, self.options))
53    }
54}
55
56#[cfg(feature = "cache")]
57pub mod cache {
58    use super::*;
59    use driver::cache::{self, Cache};
60
61    pub struct Bytes<'a> {
62        driver: cache::Driver<'a, ByteSliceDriver<'a>>,
63    }
64
65    impl<'a> Bytes<'a> {
66        #[inline]
67        pub fn new(slice: &'a [u8], options: &'a driver::Options, cache: &'a mut Cache) -> Self {
68            let driver = ByteSliceDriver::new(slice, options);
69            let driver = cache::Driver::new(driver, cache);
70            Self { driver }
71        }
72    }
73
74    impl<'a, Output> Input<Output> for Bytes<'a> {
75        type Driver = cache::Driver<'a, ByteSliceDriver<'a>>;
76
77        #[inline]
78        fn with_slice<F: FnMut(&[u8]) -> Output>(&mut self, f: &mut F) -> Output {
79            f(self.driver.as_ref().as_slice())
80        }
81
82        #[inline]
83        fn with_driver<F: FnMut(&mut Self::Driver) -> Output>(&mut self, f: &mut F) -> Output {
84            f(&mut self.driver)
85        }
86    }
87
88    pub struct Driver<'a, I: crate::Driver> {
89        slice: &'a mut Vec<u8>,
90        driver: cache::Driver<'a, I>,
91    }
92
93    impl<'a, I: crate::Driver> Driver<'a, I> {
94        #[inline]
95        pub fn new(inner: I, cache: &'a mut driver::cache::Cache, slice: &'a mut Vec<u8>) -> Self {
96            Self {
97                slice,
98                driver: cache::Driver::new(inner, cache),
99            }
100        }
101    }
102
103    impl<'a, Output, I: crate::Driver + core::panic::RefUnwindSafe> Input<Output> for Driver<'a, I> {
104        type Driver = cache::Driver<'a, I>;
105
106        #[inline]
107        fn with_slice<F: FnMut(&[u8]) -> Output>(&mut self, f: &mut F) -> Output {
108            bolero_generator::TypeGenerator::mutate(self.slice, &mut self.driver);
109            f(self.slice)
110        }
111
112        #[inline]
113        fn with_driver<F: FnMut(&mut Self::Driver) -> Output>(&mut self, f: &mut F) -> Output {
114            f(&mut self.driver)
115        }
116    }
117}
118
119#[derive(Clone, Copy)]
120pub struct SliceDebug<T>(pub(crate) T);
121
122impl<T> core::ops::Deref for SliceDebug<T> {
123    type Target = T;
124
125    fn deref(&self) -> &Self::Target {
126        &self.0
127    }
128}
129
130impl<T: AsRef<[u8]>> fmt::Debug for SliceDebug<T> {
131    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132        pretty_hex_write(f, &self.0.as_ref())
133    }
134}