Skip to main content

ffl/
num_traits.rs

1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5pub trait Zero: Sized {
6    fn zero() -> Self;
7    fn is_zero(&self) -> bool;
8}
9
10pub trait One: Sized {
11    fn one() -> Self;
12}
13
14pub trait Bounded {
15    fn min_value() -> Self;
16    fn max_value() -> Self;
17}
18
19pub trait Signed {}
20
21pub trait NumCast: TryFrom<i128> + Into<i128> + TryFrom<u128> {}
22impl<T: TryFrom<i128> + Into<i128> + TryFrom<u128>> NumCast for T {}
23
24pub trait SaturatingAdd: Sized {
25    fn saturating_add(&self, v: &Self) -> Self;
26}
27
28pub trait SaturatingSub: Sized {
29    fn saturating_sub(&self, v: &Self) -> Self;
30}
31
32pub trait SaturatingMul: Sized {
33    fn saturating_mul(&self, v: &Self) -> Self;
34}
35
36pub trait CheckedDiv: Sized {
37    fn checked_div(&self, v: &Self) -> Option<Self>;
38}
39
40pub trait FixedInt:
41    Copy
42    + Ord
43    + Zero
44    + One
45    + SaturatingAdd
46    + SaturatingSub
47    + SaturatingMul
48    + CheckedDiv
49    + core::ops::BitAnd<Output = Self>
50    + core::ops::BitOr<Output = Self>
51    + core::ops::Not<Output = Self>
52    + core::ops::Shl<usize, Output = Self>
53    + core::ops::Shr<usize, Output = Self>
54    + core::ops::Add<Output = Self>
55    + core::ops::Sub<Output = Self>
56    + core::ops::Mul<Output = Self>
57    + core::ops::Div<Output = Self>
58{
59}
60
61pub trait PrimInt: FixedInt {}
62impl<T: FixedInt> PrimInt for T {}
63
64macro_rules! impl_numeric_traits {
65    ($($t:ty),*) => {
66        $(
67            impl Zero for $t {
68                #[inline]
69                fn zero() -> Self { 0 }
70                #[inline]
71                fn is_zero(&self) -> bool { *self == 0 }
72            }
73
74            impl One for $t {
75                #[inline]
76                fn one() -> Self { 1 }
77            }
78
79            impl Bounded for $t {
80                #[inline]
81                fn min_value() -> Self { <$t>::MIN }
82                #[inline]
83                fn max_value() -> Self { <$t>::MAX }
84            }
85
86            impl SaturatingAdd for $t {
87                #[inline]
88                fn saturating_add(&self, v: &Self) -> Self { (*self).saturating_add(*v) }
89            }
90
91            impl SaturatingSub for $t {
92                #[inline]
93                fn saturating_sub(&self, v: &Self) -> Self { (*self).saturating_sub(*v) }
94            }
95
96            impl SaturatingMul for $t {
97                #[inline]
98                fn saturating_mul(&self, v: &Self) -> Self { (*self).saturating_mul(*v) }
99            }
100
101            impl CheckedDiv for $t {
102                #[inline]
103                fn checked_div(&self, v: &Self) -> Option<Self> { (*self).checked_div(*v) }
104            }
105
106            impl FixedInt for $t {}
107        )*
108    };
109}
110
111impl_numeric_traits!(i8, i16, i32, i64, i128, u8, u16, u32, u64, u128);
112
113macro_rules! impl_signed {
114    ($($t:ty),*) => {
115        $(
116            impl Signed for $t {}
117        )*
118    };
119}
120
121impl_signed!(i8, i16, i32, i64, i128);