talrost/
integer.rs

1use crate::algebra::*;
2use crate::element::Element;
3use crate::natural::Natural;
4use crate::{impl_group, impl_ring, impl_semiring};
5
6macro_rules! impl_natural_for_integer {
7    ($($base_type: ty),+) => {
8        $(
9            impl Natural for $base_type {
10                const MIN: Self = Self::MIN;
11                const MAX: Self = Self::MAX;
12                const BITS: Self = Self::BITS as Self;
13
14                // fn abs(&self) -> Self {
15                //     Self::abs(*self)
16                // }
17                fn powi(&self, power: i32) -> Self {
18                    Self::pow(*self, power as u32)
19                }
20            }
21        )+
22    };
23}
24
25pub trait Integer: Natural + Ring {}
26
27macro_rules! stack_integer{
28    ($($base_type: ty),+) => {
29        $(
30            impl Element for $base_type {}
31            impl_group!(($base_type, 0));
32            impl_semiring!(($base_type, 1));
33            impl_ring!($base_type);
34
35            impl_natural_for_integer!($base_type);
36
37            impl Integer for $base_type {}
38        )+
39    };
40}
41
42stack_integer!(i8, i16, i32, i64, i128, isize);
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    fn test_neg<T: Integer>(a: T, b: T) {
49        assert_eq!(a.neg(), b);
50    }
51
52    #[test]
53    fn test_ints() {
54        let a: i32 = 1;
55        let b: i32 = -1;
56        test_neg(a, b);
57    }
58}