1#![feature(arc_new_cyclic)]
2use std::rc::{Rc, Weak};
3
4pub trait Mutator<Value: Clone>: Sized {
89 type Cache: Clone;
90 type MutationStep: Clone;
91 type ArbitraryStep: Clone + Default;
92 type UnmutateToken;
93
94 fn cache_from_value(&self, value: &Value) -> Self::Cache;
96 fn initial_step_from_value(&self, value: &Value) -> Self::MutationStep;
98 fn max_complexity(&self) -> f64;
100 fn min_complexity(&self) -> f64;
102 fn complexity(&self, value: &Value, cache: &Self::Cache) -> f64;
103
104 fn ordered_arbitrary(&self, step: &mut Self::ArbitraryStep, max_cplx: f64) -> Option<(Value, Self::Cache)>;
105 fn random_arbitrary(&self, max_cplx: f64) -> (Value, Self::Cache);
106
107 fn ordered_mutate(
108 &self,
109 value: &mut Value,
110 cache: &mut Self::Cache,
111 step: &mut Self::MutationStep,
112 max_cplx: f64,
113 ) -> Option<Self::UnmutateToken>;
114
115 fn random_mutate(&self, value: &mut Value, cache: &mut Self::Cache, max_cplx: f64) -> Self::UnmutateToken;
116
117 fn unmutate(&self, value: &mut Value, cache: &mut Self::Cache, t: Self::UnmutateToken);
118}
119
120pub trait Serializer {
129 type Value;
130 fn is_utf8(&self) -> bool;
131 fn extension(&self) -> &str;
132 fn from_data(&self, data: &[u8]) -> Option<Self::Value>;
133 fn to_data(&self, value: &Self::Value) -> Vec<u8>;
134}
135
136#[derive(Clone)]
137pub enum RecursingArbitraryStep<AS> {
138 Default,
139 Initialized(AS),
140}
141impl<AS> Default for RecursingArbitraryStep<AS> {
142 fn default() -> Self {
143 Self::Default
144 }
145}
146
147pub struct RecursiveMutator<M> {
148 pub mutator: Rc<M>,
149}
150impl<M> RecursiveMutator<M> {
151 pub fn new(data_fn: impl FnOnce(&Weak<M>) -> M) -> Self {
152 Self {
153 mutator: Rc::new_cyclic(data_fn),
154 }
155 }
156}
157
158pub struct RecurToMutator<M> {
159 reference: Weak<M>,
160}
161impl<M> From<&Weak<M>> for RecurToMutator<M> {
162 fn from(reference: &Weak<M>) -> Self {
163 Self {
164 reference: reference.clone(),
165 }
166 }
167}
168
169impl<T, M> Mutator<T> for RecurToMutator<M>
170where
171 M: Mutator<T>,
172 T: Clone,
173{
174 type Cache = <M as Mutator<T>>::Cache;
175 type MutationStep = <M as Mutator<T>>::MutationStep;
176 type ArbitraryStep = RecursingArbitraryStep<<M as Mutator<T>>::ArbitraryStep>;
177 type UnmutateToken = <M as Mutator<T>>::UnmutateToken;
178
179 fn cache_from_value(&self, value: &T) -> Self::Cache {
180 self.reference.upgrade().unwrap().cache_from_value(value)
181 }
182
183 fn initial_step_from_value(&self, value: &T) -> Self::MutationStep {
184 self.reference.upgrade().unwrap().initial_step_from_value(value)
185 }
186
187 fn max_complexity(&self) -> f64 {
188 std::f64::INFINITY
189 }
190
191 fn min_complexity(&self) -> f64 {
192 0.0 }
194
195 fn complexity(&self, value: &T, cache: &Self::Cache) -> f64 {
196 self.reference.upgrade().unwrap().complexity(value, cache)
197 }
198
199 fn ordered_arbitrary(&self, step: &mut Self::ArbitraryStep, max_cplx: f64) -> Option<(T, Self::Cache)> {
200 match step {
201 RecursingArbitraryStep::Default => {
202 let mut inner_step = <_>::default();
203 let result = self
204 .reference
205 .upgrade()
206 .unwrap()
207 .ordered_arbitrary(&mut inner_step, max_cplx);
208 *step = RecursingArbitraryStep::Initialized(inner_step);
209 result
210 }
211 RecursingArbitraryStep::Initialized(inner_step) => self
212 .reference
213 .upgrade()
214 .unwrap()
215 .ordered_arbitrary(inner_step, max_cplx),
216 }
217 }
218
219 fn random_arbitrary(&self, max_cplx: f64) -> (T, Self::Cache) {
220 self.reference.upgrade().unwrap().random_arbitrary(max_cplx)
221 }
222
223 fn ordered_mutate(
224 &self,
225 value: &mut T,
226 cache: &mut Self::Cache,
227 step: &mut Self::MutationStep,
228 max_cplx: f64,
229 ) -> Option<Self::UnmutateToken> {
230 self.reference
231 .upgrade()
232 .unwrap()
233 .ordered_mutate(value, cache, step, max_cplx)
234 }
235
236 fn random_mutate(&self, value: &mut T, cache: &mut Self::Cache, max_cplx: f64) -> Self::UnmutateToken {
237 self.reference.upgrade().unwrap().random_mutate(value, cache, max_cplx)
238 }
239
240 fn unmutate(&self, value: &mut T, cache: &mut Self::Cache, t: Self::UnmutateToken) {
241 self.reference.upgrade().unwrap().unmutate(value, cache, t)
242 }
243}
244
245impl<T, M> Mutator<T> for RecursiveMutator<M>
246where
247 M: Mutator<T>,
248 T: Clone,
249{
250 type Cache = <M as Mutator<T>>::Cache;
251 type MutationStep = <M as Mutator<T>>::MutationStep;
252 type ArbitraryStep = <M as Mutator<T>>::ArbitraryStep;
253 type UnmutateToken = <M as Mutator<T>>::UnmutateToken;
254
255 fn cache_from_value(&self, value: &T) -> Self::Cache {
256 Rc::as_ref(&self.mutator).cache_from_value(value)
257 }
258
259 fn initial_step_from_value(&self, value: &T) -> Self::MutationStep {
260 Rc::as_ref(&self.mutator).initial_step_from_value(value)
261 }
262
263 fn max_complexity(&self) -> f64 {
264 std::f64::INFINITY
265 }
266
267 fn min_complexity(&self) -> f64 {
268 Rc::as_ref(&self.mutator).min_complexity()
269 }
270
271 fn complexity(&self, value: &T, cache: &Self::Cache) -> f64 {
272 Rc::as_ref(&self.mutator).complexity(value, cache)
273 }
274
275 fn ordered_arbitrary(&self, step: &mut Self::ArbitraryStep, max_cplx: f64) -> Option<(T, Self::Cache)> {
276 Rc::as_ref(&self.mutator).ordered_arbitrary(step, max_cplx)
277 }
278
279 fn random_arbitrary(&self, max_cplx: f64) -> (T, Self::Cache) {
280 Rc::as_ref(&self.mutator).random_arbitrary(max_cplx)
281 }
282
283 fn ordered_mutate(
284 &self,
285 value: &mut T,
286 cache: &mut Self::Cache,
287 step: &mut Self::MutationStep,
288 max_cplx: f64,
289 ) -> Option<Self::UnmutateToken> {
290 Rc::as_ref(&self.mutator).ordered_mutate(value, cache, step, max_cplx)
291 }
292
293 fn random_mutate(&self, value: &mut T, cache: &mut Self::Cache, max_cplx: f64) -> Self::UnmutateToken {
294 Rc::as_ref(&self.mutator).random_mutate(value, cache, max_cplx)
295 }
296
297 fn unmutate(&self, value: &mut T, cache: &mut Self::Cache, t: Self::UnmutateToken) {
298 Rc::as_ref(&self.mutator).unmutate(value, cache, t)
299 }
300}