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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Constraint

use alloc::{boxed::Box, string::String};

/// Constraint
///
/// # Types
///
/// * `S`: Solution
pub trait Cstr<S> {
  fn reasons(&self, _: &S) -> String {
    String::new()
  }

  fn violations(&self, solution: &S) -> usize;
}

impl<C, S> Cstr<S> for &'_ C
where
  C: Cstr<S> + ?Sized,
{
  fn reasons(&self, solution: &S) -> String {
    (*self).reasons(solution)
  }

  fn violations(&self, solution: &S) -> usize {
    (*self).violations(solution)
  }
}

impl<C, S> Cstr<S> for Box<C>
where
  C: Cstr<S>,
{
  fn reasons(&self, solution: &S) -> String {
    self.as_ref().reasons(solution)
  }

  fn violations(&self, solution: &S) -> usize {
    self.as_ref().violations(solution)
  }
}

impl<S> Cstr<S> for fn(&S) -> usize {
  fn violations(&self, solution: &S) -> usize {
    self(solution)
  }
}

impl<S> Cstr<S> for (fn(&S) -> String, fn(&S) -> usize) {
  fn reasons(&self, solution: &S) -> String {
    self.0(solution)
  }

  fn violations(&self, solution: &S) -> usize {
    self.1(solution)
  }
}

//impl<S> Cstr<S> for () {
//  fn violations(&self, _: &S) -> usize {
//    usize::MAX
//  }
//}