Skip to main content

checkito/
map.rs

1use crate::{generate::Generate, shrink::Shrink, state::State};
2
3#[derive(Debug, Clone)]
4pub struct Map<T: ?Sized, F>(pub(crate) F, pub(crate) T);
5
6impl<G: Generate + ?Sized, T, F: Fn(G::Item) -> T + Clone> Generate for Map<G, F> {
7    type Item = T;
8    type Shrink = Map<G::Shrink, F>;
9
10    const CARDINALITY: Option<u128> = G::CARDINALITY;
11
12    fn generate(&self, state: &mut State) -> Self::Shrink {
13        Map(self.0.clone(), self.1.generate(state))
14    }
15
16    fn cardinality(&self) -> Option<u128> {
17        self.1.cardinality()
18    }
19}
20
21impl<S: Shrink, T, F: Fn(S::Item) -> T + Clone> Shrink for Map<S, F> {
22    type Item = T;
23
24    fn item(&self) -> Self::Item {
25        self.0(self.1.item())
26    }
27
28    fn shrink(&mut self) -> Option<Self> {
29        Some(Self(self.0.clone(), self.1.shrink()?))
30    }
31}