1use std::fmt::{Debug, Display};
2use std::hash::Hash;
3
4use num_traits::ops::wrapping::{
5 WrappingAdd, WrappingMul, WrappingNeg, WrappingShl, WrappingShr, WrappingSub,
6};
7use num_traits::{AsPrimitive, FromPrimitive, PrimInt, Signed, ToPrimitive, Unsigned};
8
9pub trait Number: PrimInt + Default + Debug + AsPrimitive<usize> + ToPrimitive {}
27
28impl<All: PrimInt + Default + Debug + AsPrimitive<usize> + ToPrimitive> Number for All {}
29
30pub trait SignedNumber: Number + Signed + FromPrimitive {}
46
47impl<All: Number + Signed + FromPrimitive> SignedNumber for All {}
48
49pub trait ToSigned {
64 type Type: Number + Signed;
66
67 fn to_signed(&self) -> Self::Type;
69}
70
71macro_rules! signed_type_impl {
72 ($U:ty, $S:ty) => {
73 impl ToSigned for $U {
74 type Type = $S;
75
76 fn to_signed(&self) -> Self::Type {
77 *self as Self::Type
78 }
79 }
80 };
81}
82
83signed_type_impl!(i8, i8);
84signed_type_impl!(u8, i8);
85signed_type_impl!(i16, i16);
86signed_type_impl!(u16, i16);
87signed_type_impl!(i32, i32);
88signed_type_impl!(u32, i32);
89signed_type_impl!(i64, i64);
90signed_type_impl!(u64, i64);
91signed_type_impl!(i128, i128);
92signed_type_impl!(u128, i128);
93signed_type_impl!(isize, isize);
94signed_type_impl!(usize, isize);
95
96pub trait MaxValue {
108 const MAX: Self;
110}
111
112macro_rules! for_each_integer_type {
118 ($macro:ident) => {
119 $macro!(i8);
120 $macro!(u8);
121 $macro!(i16);
122 $macro!(u16);
123 $macro!(i32);
124 $macro!(u32);
125 $macro!(i64);
126 $macro!(u64);
127 $macro!(i128);
128 $macro!(u128);
129 $macro!(isize);
130 $macro!(usize);
131 };
132}
133
134macro_rules! max_value_impl {
135 ($T:ty) => {
136 impl MaxValue for $T {
137 const MAX: Self = <$T>::MAX;
138 }
139 };
140}
141
142for_each_integer_type!(max_value_impl);
143
144pub trait WrappingArithmetic:
164 WrappingAdd + WrappingSub + WrappingMul + WrappingNeg + WrappingShl + WrappingShr
165{
166}
167
168impl<All: WrappingAdd + WrappingSub + WrappingMul + WrappingNeg + WrappingShl + WrappingShr>
169 WrappingArithmetic for All
170{
171}
172
173#[rustfmt::skip]
201pub trait LinkReference:
202Sized
203+ Number
204+ Unsigned
205+ ToSigned
206+ MaxValue
207+ WrappingArithmetic
208+ FromPrimitive
209+ TryFrom<i8, Error: Debug>
210+ TryFrom<u8, Error: Debug>
211+ TryFrom<i16, Error: Debug>
212+ TryFrom<u16, Error: Debug>
213+ TryFrom<i32, Error: Debug>
214+ TryFrom<u32, Error: Debug>
215+ TryFrom<i64, Error: Debug>
216+ TryFrom<u64, Error: Debug>
217+ TryFrom<i128, Error: Debug>
218+ TryFrom<u128, Error: Debug>
219+ TryFrom<isize, Error: Debug>
220+ TryFrom<usize, Error: Debug>
221+ TryInto<i8, Error: Debug>
222+ TryInto<u8, Error: Debug>
223+ TryInto<i16, Error: Debug>
224+ TryInto<u16, Error: Debug>
225+ TryInto<i32, Error: Debug>
226+ TryInto<u32, Error: Debug>
227+ TryInto<i64, Error: Debug>
228+ TryInto<u64, Error: Debug>
229+ TryInto<i128, Error: Debug>
230+ TryInto<u128, Error: Debug>
231+ TryInto<isize, Error: Debug>
232+ TryInto<usize, Error: Debug>
233+ Debug
234+ Display
235+ Hash
236+ Send
237+ Sync
238+ 'static
239{
240 fn from_byte(n: u8) -> Self {
242 Self::try_from(n).unwrap_or_else(|e| {
243 panic!("LinkReference::from_byte({n}) failed: {e:?}")
244 })
245 }
246}
247
248#[rustfmt::skip]
249impl<
250 All: Sized
251 + Number
252 + Unsigned
253 + ToSigned
254 + MaxValue
255 + WrappingArithmetic
256 + FromPrimitive
257 + TryFrom<i8, Error: Debug>
258 + TryFrom<u8, Error: Debug>
259 + TryFrom<i16, Error: Debug>
260 + TryFrom<u16, Error: Debug>
261 + TryFrom<i32, Error: Debug>
262 + TryFrom<u32, Error: Debug>
263 + TryFrom<i64, Error: Debug>
264 + TryFrom<u64, Error: Debug>
265 + TryFrom<i128, Error: Debug>
266 + TryFrom<u128, Error: Debug>
267 + TryFrom<isize, Error: Debug>
268 + TryFrom<usize, Error: Debug>
269 + TryInto<i8, Error: Debug>
270 + TryInto<u8, Error: Debug>
271 + TryInto<i16, Error: Debug>
272 + TryInto<u16, Error: Debug>
273 + TryInto<i32, Error: Debug>
274 + TryInto<u32, Error: Debug>
275 + TryInto<i64, Error: Debug>
276 + TryInto<u64, Error: Debug>
277 + TryInto<i128, Error: Debug>
278 + TryInto<u128, Error: Debug>
279 + TryInto<isize, Error: Debug>
280 + TryInto<usize, Error: Debug>
281 + Debug
282 + Display
283 + Hash
284 + Send
285 + Sync
286 + 'static,
287> LinkReference for All {}