random as generic parameter in corpus

This commit is contained in:
Andrea Fioraldi 2020-10-27 15:50:32 +01:00
parent c5e38d6961
commit a2e50c7b51

View File

@ -31,13 +31,13 @@ pub trait Corpus: Debug {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct RandomCorpus<'a> { pub struct RandomCorpus<'a, RandT: Rand> {
rand: &'a mut dyn Rand, rand: &'a mut RandT,
entries: Vec<Box<dyn Testcase>>, entries: Vec<Box<dyn Testcase>>,
dir_path: String, dir_path: String,
} }
impl Corpus for RandomCorpus<'_> { impl<RandT: Rand> Corpus for RandomCorpus<'_, RandT> {
/// Returns the number of elements /// Returns the number of elements
fn count(&self) -> usize { fn count(&self) -> usize {
self.entries.len() self.entries.len()
@ -76,8 +76,8 @@ impl Corpus for RandomCorpus<'_> {
} }
} }
impl RandomCorpus<'_> { impl<RandT: Rand> RandomCorpus<'_, RandT> {
pub fn new<'a>(rand: &'a mut dyn Rand, dir_path: &str) -> RandomCorpus<'a> { pub fn new<'a>(rand: &'a mut RandT, dir_path: &str) -> RandomCorpus<'a, RandT> {
RandomCorpus { RandomCorpus {
dir_path: dir_path.to_owned(), dir_path: dir_path.to_owned(),
entries: vec![], entries: vec![],
@ -88,13 +88,13 @@ impl RandomCorpus<'_> {
/// A queue-like corpus /// A queue-like corpus
#[derive(Debug)] #[derive(Debug)]
pub struct QueueCorpus<'a> { pub struct QueueCorpus<'a, RandT: Rand> {
random_corpus: RandomCorpus<'a>, random_corpus: RandomCorpus<'a, RandT>,
pos: usize, pos: usize,
cycles: u64, cycles: u64,
} }
impl Corpus for QueueCorpus<'_> { impl<RandT: Rand> Corpus for QueueCorpus<'_, RandT> {
/// Returns the number of elements /// Returns the number of elements
fn count(&self) -> usize { fn count(&self) -> usize {
self.random_corpus.count() self.random_corpus.count()
@ -128,8 +128,8 @@ impl Corpus for QueueCorpus<'_> {
} }
} }
impl QueueCorpus<'_> { impl<RandT: Rand> QueueCorpus<'_, RandT> {
pub fn new<'a>(rand: &'a mut dyn Rand, dir_path: &str) -> QueueCorpus<'a> { pub fn new<'a>(rand: &'a mut RandT, dir_path: &str) -> QueueCorpus<'a, RandT> {
QueueCorpus { QueueCorpus {
random_corpus: RandomCorpus::new(rand, dir_path), random_corpus: RandomCorpus::new(rand, dir_path),
cycles: 0, cycles: 0,