haar_lib/num/
one_zero.rs

1//! 加法と乗法の単位元を定める。
2
3/// 加算についての単位元をもつ。
4pub trait Zero {
5    /// 加法の単位元を返す。
6    fn zero() -> Self;
7}
8
9/// 乗算についての単位元をもつ。
10pub trait One {
11    /// 乗法の単位元を返す。
12    fn one() -> Self;
13}
14
15macro_rules! impl_one_zero {
16    ($($t:tt),*) => {
17        $(
18            impl Zero for $t { fn zero() -> Self { 0 as $t } }
19            impl One for $t { fn one() -> Self { 1 as $t } }
20            impl Zero for &$t { fn zero() -> Self { &(0 as $t) } }
21            impl One for &$t { fn one() -> Self { &(1 as $t) } }
22        )*
23    }
24}
25
26use std::num::Saturating;
27use std::num::Wrapping;
28macro_rules! impl_one_zero_wrap {
29    ($($t:tt),*) => {
30        $(
31            impl Zero for Wrapping<$t> { fn zero() -> Self { Wrapping($t::zero()) }}
32            impl One for Wrapping<$t> { fn one() -> Self { Wrapping($t::one()) }}
33            impl Zero for Saturating<$t> { fn zero() -> Self { Saturating($t::zero()) }}
34            impl One for Saturating<$t> { fn one() -> Self { Saturating($t::one()) }}
35        )*
36    }
37}
38
39impl_one_zero!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64);
40impl_one_zero_wrap!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);