Skip to main content

score_set/
traits.rs

1use witnessed::Witnessed;
2
3/// Witness attached to values known to be valid for the normalized `[0, 1]`
4/// scoring boundary.
5pub struct V01;
6
7/// Measures a context and returns a value chosen by the implementation.
8///
9/// `Output` is the value consumed by a corresponding `Map01F32` or `Map01F64`
10/// implementation.
11pub trait Measure<Ctx: ?Sized>: Send + Sync {
12    /// The value produced by [`Measure::measure`].
13    type Output;
14
15    /// Extracts a measurable value from `ctx`.
16    fn measure(&self, ctx: &Ctx) -> Self::Output;
17}
18
19/// Maps a measurement into the `[0, 1]` range.
20///
21/// `Input` is associated with the mapper rather than fixed in the trait, so a
22/// mapper can consume any measurement type. The normalized result carries the
23/// [`V01`] witness.
24pub trait Map01F32: Send + Sync {
25    /// The value accepted by [`Map01F32::map`].
26    type Input;
27
28    /// Converts `value` into a normalized score.
29    fn map(&self, value: Self::Input) -> Witnessed<f32, V01>;
30}
31
32/// Maps a measurement into the `[0, 1]` range.
33///
34/// `Input` is associated with the mapper rather than fixed in the trait, so a
35/// mapper can consume any measurement type. The normalized result carries the
36/// [`V01`] witness.
37pub trait Map01F64: Send + Sync {
38    /// The value accepted by [`Map01F64::map`].
39    type Input;
40
41    /// Converts `value` into a normalized score.
42    fn map(&self, value: Self::Input) -> Witnessed<f64, V01>;
43}
44
45/// Evaluates a context into an `f64` score.
46pub trait EvalF64<Ctx: ?Sized>: Send + Sync {
47    /// Computes a score from `ctx`.
48    fn eval(&self, ctx: &Ctx) -> f64;
49}
50
51/// Evaluates a context into an `f32` score.
52pub trait EvalF32<Ctx: ?Sized>: Send + Sync {
53    /// Computes a score from `ctx`.
54    fn eval(&self, ctx: &Ctx) -> f32;
55}