Skip to content

Commit c1b526e

Browse files
Add FilterMutator::new method.
1 parent 1557cca commit c1b526e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

fuzzcheck/src/mutators/filter.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,31 @@ use std::any::Any;
22

33
use crate::Mutator;
44

5+
/// A [`FilterMutator`] provides a way to filter values outputted by a mutator.
6+
/// Given any [`Mutator<Value=T>`] and a function [`Fn(&T) -> bool`] it creates
7+
/// a new mutator which can generate all the values of `T` the underlying
8+
/// mutator can, except those for which the filtering function returns false.
59
pub struct FilterMutator<M, F> {
6-
pub mutator: M,
7-
pub filter: F,
10+
mutator: M,
11+
filter: F,
812
}
13+
14+
impl<M, F> FilterMutator<M, F> {
15+
/// Creates a new [`FilterMutator`].
16+
///
17+
/// Note that the mutator will filter all values for which the filtering
18+
/// function returns _false_.
19+
pub fn new<T>(mutator: M, filter: F) -> FilterMutator<M, F>
20+
where
21+
M: Mutator<T>,
22+
T: Clone + 'static,
23+
F: Fn(&T) -> bool,
24+
Self: 'static,
25+
{
26+
FilterMutator { mutator, filter }
27+
}
28+
}
29+
930
impl<T, M, F> Mutator<T> for FilterMutator<M, F>
1031
where
1132
M: Mutator<T>,

fuzzcheck/src/mutators/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ where
140140
#[no_coverage]
141141
fn filter<F>(self, filter: F) -> FilterMutator<Self, F>
142142
where
143-
F: Fn(&T) -> bool,
143+
F: Fn(&T) -> bool + 'static,
144144
{
145-
FilterMutator { mutator: self, filter }
145+
FilterMutator::new(self, filter)
146146
}
147147
/// Create a mutator which wraps `self` and transforms the values generated by `self`
148148
/// using the `map` closure. The second closure, `parse`, should apply the opposite

0 commit comments

Comments
 (0)