fuzzcheck_mutators/
unit.rs

1use std::marker::PhantomData;
2
3use fuzzcheck_traits::Mutator;
4
5use crate::DefaultMutator;
6
7pub type VoidMutator = UnitMutator<()>;
8
9impl DefaultMutator for () {
10    type Mutator = VoidMutator;
11    fn default_mutator() -> Self::Mutator {
12        Self::Mutator::default()
13    }
14}
15
16pub type PhantomDataMutator<T> = UnitMutator<PhantomData<T>>;
17impl<T> DefaultMutator for PhantomData<T>
18where
19    T: 'static,
20{
21    type Mutator = PhantomDataMutator<T>;
22    fn default_mutator() -> Self::Mutator {
23        Self::Mutator::default()
24    }
25}
26
27#[derive(Clone)]
28pub struct UnitMutator<T>
29where
30    T: Clone,
31{
32    value: T,
33}
34
35impl<T> UnitMutator<T>
36where
37    T: Clone,
38{
39    pub fn new(value: T) -> Self {
40        Self { value }
41    }
42}
43
44impl<T> Default for UnitMutator<T>
45where
46    T: Default + Clone,
47{
48    fn default() -> Self {
49        Self { value: T::default() }
50    }
51}
52
53impl<T> Mutator<T> for UnitMutator<T>
54where
55    T: Clone + 'static,
56{
57    type Cache = ();
58    type MutationStep = ();
59    type ArbitraryStep = bool;
60    type UnmutateToken = ();
61
62    fn cache_from_value(&self, _value: &T) -> Self::Cache {}
63
64    fn initial_step_from_value(&self, _value: &T) -> Self::MutationStep {}
65
66    fn max_complexity(&self) -> f64 {
67        0.0
68    }
69
70    fn min_complexity(&self) -> f64 {
71        0.0
72    }
73
74    fn complexity(&self, _value: &T, _cache: &Self::Cache) -> f64 {
75        0.0
76    }
77
78    fn ordered_arbitrary(&self, step: &mut Self::ArbitraryStep, _max_cplx: f64) -> Option<(T, Self::Cache)> {
79        if !*step {
80            *step = true;
81            Some((self.value.clone(), ()))
82        } else {
83            None
84        }
85    }
86
87    fn random_arbitrary(&self, _max_cplx: f64) -> (T, Self::Cache) {
88        (self.value.clone(), ())
89    }
90
91    fn ordered_mutate(
92        &self,
93        _value: &mut T,
94        _cache: &mut Self::Cache,
95        _step: &mut Self::MutationStep,
96        _max_cplx: f64,
97    ) -> Option<Self::UnmutateToken> {
98        None
99    }
100
101    fn random_mutate(&self, _value: &mut T, _cache: &mut Self::Cache, _max_cplx: f64) -> Self::UnmutateToken {}
102
103    fn unmutate(&self, _value: &mut T, _cache: &mut Self::Cache, _t: Self::UnmutateToken) {}
104}