sigma_types/
non_zero.rs

1//! Nonzero terms (defined by comparison to zero).
2
3use {
4    crate::{Sigma, Test, Zero},
5    core::{fmt, marker::PhantomData},
6};
7
8/// Nonzero terms (defined by comparison to zero).
9pub type NonZero<Input> = Sigma<Input, NonZeroInvariant<Input>>;
10
11/// Nonzero terms (defined by comparison to zero).
12#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
13pub struct NonZeroInvariant<Input: fmt::Debug + PartialEq + Zero>(PhantomData<Input>);
14
15impl<Input: fmt::Debug + PartialEq + Zero> Test<Input, 1> for NonZeroInvariant<Input> {
16    const ADJECTIVE: &str = "nonzero";
17    type Error<'i>
18        = NotNonZero<'i, Input>
19    where
20        Input: 'i;
21
22    #[inline(always)]
23    fn test([input]: [&Input; 1]) -> Result<(), Self::Error<'_>> {
24        if *input == Input::ZERO {
25            Err(NotNonZero(input))
26        } else {
27            Ok(())
28        }
29    }
30}
31
32/// A term expected to be nonzero was, in fact, zero.
33#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
34pub struct NotNonZero<'i, Input: fmt::Debug + PartialEq + Zero>(&'i Input);
35
36impl<Input: fmt::Debug + PartialEq + Zero> fmt::Display for NotNonZero<'_, Input> {
37    #[inline]
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        #![expect(
40            clippy::use_debug,
41            reason = "Intentional and informative, not just forgotten print-debugging"
42        )]
43
44        let Self(z) = *self;
45        write!(f, "{z:#?} == {:#?}", Input::ZERO)
46    }
47}