checkito/
map.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::{
    generate::{Generate, State},
    shrink::Shrink,
};

#[derive(Debug, Clone)]
pub struct Map<T: ?Sized, F>(pub(crate) F, pub(crate) T);

impl<G: Generate + ?Sized, T, F: Fn(G::Item) -> T + Clone> Generate for Map<G, F> {
    type Item = T;
    type Shrink = Map<G::Shrink, F>;

    fn generate(&self, state: &mut State) -> Self::Shrink {
        Map(self.0.clone(), self.1.generate(state))
    }

    fn constant(&self) -> bool {
        self.1.constant()
    }
}

impl<S: Shrink, T, F: Fn(S::Item) -> T + Clone> Shrink for Map<S, F> {
    type Item = T;

    fn item(&self) -> Self::Item {
        self.0(self.1.item())
    }

    fn shrink(&mut self) -> Option<Self> {
        Some(Self(self.0.clone(), self.1.shrink()?))
    }
}