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
33
34
35
36
37
38
39
40
41
//! The goal test [`Goal`].
/// A goal test over states of type `S`.
///
/// Takes `&self`: goal tests are pure predicates that need no workspace, which keeps the
/// blanket impl for closures and lets callers pass
/// `&|s: &S| ...` directly (the argument type needs an annotation). Implemented for every
/// `Fn(&S) -> bool`. Algorithms take goals by reference, so no `&G` forwarding impl is
/// needed (and one could not coexist with the closure impl).
///
/// # Examples
///
/// ```
/// use search_core::Goal;
///
/// /// Any state at or beyond a threshold.
/// struct AtLeast(u32);
///
/// impl Goal<u32> for AtLeast {
/// fn is_goal(&self, state: &u32) -> bool {
/// *state >= self.0
/// }
/// }
///
/// fn reached<G: Goal<u32> + ?Sized>(goal: &G, state: u32) -> bool {
/// goal.is_goal(&state)
/// }
///
/// assert!(reached(&AtLeast(5), 7));
/// assert!(!reached(&|s: &u32| *s == 3, 7));
/// ```