use crate::BoxGen;
pub fn sequence<E>(examples: &[E]) -> BoxGen<E>
where
E: Clone + std::fmt::Debug + 'static,
{
let example_vec = examples.to_vec();
crate::gens::from_fn(move |_seed, _size| example_vec.clone().into_iter())
}
pub fn constant<E: Clone + 'static>(example: E) -> BoxGen<E> {
crate::gens::from_fn(move |_seed, _size| std::iter::repeat(example.clone()))
}
pub fn in_loop<E>(examples: &[E]) -> BoxGen<E>
where
E: Clone + 'static,
{
let examples_vec = examples.to_vec().clone();
crate::gens::from_fn(move |_seed, _size| {
let x = examples_vec.clone();
LoopIter { data: x, index: 0 }
})
}
#[derive(Clone)]
struct LoopIter<E> {
data: Vec<E>,
index: usize,
}
impl<E: Clone + 'static> Iterator for LoopIter<E> {
type Item = E;
fn next(&mut self) -> Option<Self::Item> {
let index_to_use = self.index;
self.index = (self.index + 1) % self.data.len();
self.data.get(index_to_use).cloned()
}
}