num_convert/
extra_traits.rs1use crate::{FromAs, IntoAs};
2use core::ops::Rem;
3use core::ops::Div;
4
5#[allow(clippy::len_without_is_empty)]
24pub trait IntegerLen {
25 fn len(self) -> usize;
27}
28
29impl<T> IntegerLen for T
30where
31 T: Eq + Copy + Div<Output = T> + IntoAs<T> + FromAs<u8>,
32 u8: IntoAs<T>,
33{
34 #[inline]
35 fn len(mut self) -> usize {
36 let mut count = 0;
37 let ten = 10.into_as();
38 let zero = <T>::from_as(0);
39 while self != zero {
40 self = self / ten;
41 count += 1;
42 }
43 if count == 0 {
44 1
45 } else {
46 count
47 }
48 }
49}
50
51pub trait TypeInfo {
53 fn info() -> &'static str;
55}
56
57macro_rules! type_info_impls {
58 ($($type:ty, $type_str:expr);* ) => {
59 $(
60 impl TypeInfo for $type {
61 #[inline]
62 fn info() -> &'static str {
63 $type_str
64 }
65 }
66 )*
67 }
68}
69
70type_info_impls! { i8, "i8"; i16, "i16"; i32, "i32"; i64, "i64"; isize, "isize"; i128, "i128" }
71type_info_impls! { u8, "u8"; u16, "u16"; u32, "u32"; u64, "u64"; usize, "usize"; u128, "u128" }
72
73pub trait ValTypeInfo {
75 fn info(self) -> &'static str;
77}
78
79macro_rules! val_type_info_impls {
80 ($($type:ty, $type_str:expr);* ) => {
81 $(
82 impl ValTypeInfo for $type {
83 #[inline]
84 fn info(self) -> &'static str {
85 $type_str
86 }
87 }
88 )*
89 }
90}
91
92val_type_info_impls! { i8, "i8"; i16, "i16"; i32, "i32"; i64, "i64"; isize, "isize"; i128, "i128" }
93val_type_info_impls! { u8, "u8"; u16, "u16"; u32, "u32"; u64, "u64"; usize, "usize"; u128, "u128" }
94
95impl<T> CheckRem for T
96where
97 T: Copy + Default + Rem<Output = T> + PartialEq,
98{ }
99
100pub trait CheckRem: Rem<Output = Self> + PartialEq
102where
103 Self: Copy + Default,
104{
105 #[inline]
123 fn no_rem(&self, n: Self) -> bool {
124 *self % n == Self::default()
125 }
126
127 #[inline]
142 fn is_rem(&self, n: Self) -> bool {
143 *self % n != Self::default()
144 }
145}