sigma_types/
on_unit.rs

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! Types on the unit interval (between 0 and 1),
//! either inclusive or exclusive at each extreme.

use {
    crate::{One, Sigma, Test, Zero},
    core::{cmp::Ordering, fmt, marker::PhantomData},
};

/// Term expected to be on the unit interval (between 0 and 1) was not.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct NotOnUnit<
    'i,
    Input: One + PartialOrd + Zero + fmt::Debug,
    const INCLUSIVE_AT_ZERO: bool,
    const INCLUSIVE_AT_ONE: bool,
>(&'i Input);

impl<
    Input: One + PartialOrd + Zero + fmt::Debug,
    const INCLUSIVE_AT_ZERO: bool,
    const INCLUSIVE_AT_ONE: bool,
> fmt::Display for NotOnUnit<'_, Input, INCLUSIVE_AT_ZERO, INCLUSIVE_AT_ONE>
{
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        #![expect(
            clippy::use_debug,
            reason = "Intentional and informative, not just forgotten print-debugging"
        )]

        let Self(u) = *self;
        write!(
            f,
            "Not on {}0, 1{}: {u:#?}",
            if INCLUSIVE_AT_ZERO { '[' } else { '(' },
            if INCLUSIVE_AT_ONE { ']' } else { ')' },
        )
    }
}

/// Terms on the unit interval (between 0 and 1),
/// either inclusive or exclusive at each extreme.
pub type OnUnit<Input, const INCLUSIVE_AT_ZERO: bool, const INCLUSIVE_AT_ONE: bool> =
    Sigma<Input, OnUnitInvariant<Input, INCLUSIVE_AT_ZERO, INCLUSIVE_AT_ONE>>;

/// Terms on the unit interval (between 0 and 1),
/// either inclusive or exclusive at each extreme.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct OnUnitInvariant<
    Input: One + PartialOrd + Zero + fmt::Debug,
    const INCLUSIVE_AT_ZERO: bool,
    const INCLUSIVE_AT_ONE: bool,
>(PhantomData<Input>);

impl<
    Input: One + PartialOrd + Zero + fmt::Debug,
    const INCLUSIVE_AT_ZERO: bool,
    const INCLUSIVE_AT_ONE: bool,
> Test<Input, 1> for OnUnitInvariant<Input, INCLUSIVE_AT_ZERO, INCLUSIVE_AT_ONE>
{
    const ADJECTIVE: &str = "on the unit interval";
    type Error<'i>
        = NotOnUnit<'i, Input, INCLUSIVE_AT_ZERO, INCLUSIVE_AT_ONE>
    where
        Input: 'i;

    #[inline]
    fn test([input]: [&Input; 1]) -> Result<(), Self::Error<'_>> {
        match input.partial_cmp(&Input::ZERO) {
            None | Some(Ordering::Less) => return Err(NotOnUnit(input)),
            Some(Ordering::Equal) => {
                if !INCLUSIVE_AT_ZERO {
                    return Err(NotOnUnit(input));
                }
            }
            Some(Ordering::Greater) => {}
        }
        match input.partial_cmp(&Input::ONE) {
            None | Some(Ordering::Greater) => return Err(NotOnUnit(input)),
            Some(Ordering::Equal) => {
                if !INCLUSIVE_AT_ONE {
                    return Err(NotOnUnit(input));
                }
            }
            Some(Ordering::Less) => {}
        }
        Ok(())
    }
}