logic/
metric.rs

1use std::rc::Rc;
2use std::sync::Arc;
3
4pub trait Metric<T> {
5    type Error: std::error::Error;
6
7    /// Returns a floating-point value that represents the "goodness" of the input with respect to some criteria
8    ///
9    /// In general, the sign of the value indicates whether the input is good or bad, and the magnitude of the value
10    /// represents how good/bad the input is. Representation in this fashion allows for the use of optimization
11    /// algorithms to find the best input.
12    fn distance(&self, value: &T) -> Result<f64, Self::Error>;
13}
14
15impl<T, M: Metric<T> + ?Sized> Metric<T> for &'_ M {
16    type Error = M::Error;
17
18    fn distance(&self, value: &T) -> Result<f64, Self::Error> {
19        (**self).distance(value)
20    }
21}
22
23// TODO use fix above to remove definition here
24impl<T, M: Metric<T> + ?Sized> Metric<T> for Box<M> {
25    type Error = M::Error;
26
27    fn distance(&self, value: &T) -> Result<f64, Self::Error> {
28        (**self).distance(value)
29    }
30}
31
32impl<T, M: Metric<T> + ?Sized> Metric<T> for Rc<M> {
33    type Error = M::Error;
34
35    fn distance(&self, value: &T) -> Result<f64, Self::Error> {
36        (**self).distance(value)
37    }
38}
39
40impl<T, M: Metric<T> + ?Sized> Metric<T> for Arc<M> {
41    type Error = M::Error;
42
43    fn distance(&self, value: &T) -> Result<f64, Self::Error> {
44        (**self).distance(value)
45    }
46}