sigma_types/
less_than.rs

1//! Terms less than a constant (defined by `PartialOrd` comparison).
2
3#![expect(
4    clippy::arbitrary_source_item_ordering,
5    reason = "macros need to be defined before they're used"
6)]
7
8/// Make a type-specific module, since (unfortunately) we can't use dependent types.
9macro_rules! mk_mod {
10    ($t:ident) => {
11        pub mod $t {
12            //! Terms of type `$t` less than a constant (defined by `PartialOrd` comparison).
13
14            use {
15                crate::{Sigma, Test},
16                core::fmt,
17            };
18
19            /// Terms less than a constant (defined by `PartialOrd` comparison).
20            pub type LessThan<const N: $t> = Sigma<$t, LessThanInvariant<N>>;
21
22            /// Terms less than a constant (defined by `PartialOrd` comparison).
23            #[expect(clippy::exhaustive_structs, reason = "no fields")]
24            #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
25            pub struct LessThanInvariant<const N: $t>;
26
27            impl<const N: $t> Test<$t, 1> for LessThanInvariant<N> {
28                const ADJECTIVE: &str = "positive";
29                type Error<'i>
30                    = NotLessThan<'i, N>
31                where
32                    $t: 'i;
33
34                #[inline(always)]
35                fn test([input]: [&$t; 1]) -> Result<(), Self::Error<'_>> {
36                    if *input < N {
37                        Ok(())
38                    } else {
39                        Err(NotLessThan(input))
40                    }
41                }
42            }
43
44            /// A term expected to be positive was not.
45            #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
46            pub struct NotLessThan<'i, const N: $t>(&'i $t);
47
48            impl<const N: $t> fmt::Display for NotLessThan<'_, N> {
49                #[inline]
50                fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51                    /*
52                    #![expect(
53                        clippy::use_debug,
54                        reason = "Intentional and informative, not just forgotten print-debugging"
55                    )]
56                    */
57
58                    let Self(z) = *self;
59                    write!(f, "{z:#?} >= {N:#?}")
60                }
61            }
62        }
63    };
64}
65
66mk_mod!(i8);
67mk_mod!(i16);
68mk_mod!(i32);
69mk_mod!(i64);
70mk_mod!(i128);
71mk_mod!(isize);
72mk_mod!(u8);
73mk_mod!(u16);
74mk_mod!(u32);
75mk_mod!(u64);
76mk_mod!(u128);
77mk_mod!(usize);