Skip to main content

csp_solver/constraint/
lambda.rs

1//! Generic closure-based constraint.
2
3use crate::domain::Domain;
4
5use super::traits::{Constraint, VarId};
6
7/// Boxed predicate over a partial assignment.
8pub(crate) type CheckerFn<D> = Box<dyn Fn(&[Option<<D as Domain>::Value>]) -> bool>;
9
10pub struct LambdaConstraint<D: Domain> {
11    pub(crate) scope: Vec<VarId>,
12    pub(crate) checker: CheckerFn<D>,
13    label: String,
14}
15
16impl<D: Domain> std::fmt::Debug for LambdaConstraint<D> {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        write!(f, "LambdaConstraint({}, {:?})", self.label, self.scope)
19    }
20}
21
22impl<D: Domain> LambdaConstraint<D> {
23    pub fn new(
24        scope: Vec<VarId>,
25        checker: impl Fn(&[Option<D::Value>]) -> bool + 'static,
26        label: impl Into<String>,
27    ) -> Self {
28        Self { scope, checker: Box::new(checker), label: label.into() }
29    }
30}
31
32impl<D: Domain> Constraint<D> for LambdaConstraint<D> {
33    fn scope(&self) -> &[VarId] { &self.scope }
34    fn check(&self, assignment: &[Option<D::Value>]) -> bool { (self.checker)(assignment) }
35}