• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use core::num::Wrapping;
2 use core::{f32, f64};
3 #[cfg(has_i128)]
4 use core::{i128, u128};
5 use core::{i16, i32, i64, i8, isize};
6 use core::{u16, u32, u64, u8, usize};
7 
8 /// Numbers which have upper and lower bounds
9 pub trait Bounded {
10     // FIXME (#5527): These should be associated constants
11     /// returns the smallest finite number this type can represent
min_value() -> Self12     fn min_value() -> Self;
13     /// returns the largest finite number this type can represent
max_value() -> Self14     fn max_value() -> Self;
15 }
16 
17 macro_rules! bounded_impl {
18     ($t:ty, $min:expr, $max:expr) => {
19         impl Bounded for $t {
20             #[inline]
21             fn min_value() -> $t {
22                 $min
23             }
24 
25             #[inline]
26             fn max_value() -> $t {
27                 $max
28             }
29         }
30     };
31 }
32 
33 bounded_impl!(usize, usize::MIN, usize::MAX);
34 bounded_impl!(u8, u8::MIN, u8::MAX);
35 bounded_impl!(u16, u16::MIN, u16::MAX);
36 bounded_impl!(u32, u32::MIN, u32::MAX);
37 bounded_impl!(u64, u64::MIN, u64::MAX);
38 #[cfg(has_i128)]
39 bounded_impl!(u128, u128::MIN, u128::MAX);
40 
41 bounded_impl!(isize, isize::MIN, isize::MAX);
42 bounded_impl!(i8, i8::MIN, i8::MAX);
43 bounded_impl!(i16, i16::MIN, i16::MAX);
44 bounded_impl!(i32, i32::MIN, i32::MAX);
45 bounded_impl!(i64, i64::MIN, i64::MAX);
46 #[cfg(has_i128)]
47 bounded_impl!(i128, i128::MIN, i128::MAX);
48 
49 impl<T: Bounded> Bounded for Wrapping<T> {
min_value() -> Self50     fn min_value() -> Self {
51         Wrapping(T::min_value())
52     }
max_value() -> Self53     fn max_value() -> Self {
54         Wrapping(T::max_value())
55     }
56 }
57 
58 bounded_impl!(f32, f32::MIN, f32::MAX);
59 
60 macro_rules! for_each_tuple_ {
61     ( $m:ident !! ) => (
62         $m! { }
63     );
64     ( $m:ident !! $h:ident, $($t:ident,)* ) => (
65         $m! { $h $($t)* }
66         for_each_tuple_! { $m !! $($t,)* }
67     );
68 }
69 macro_rules! for_each_tuple {
70     ($m:ident) => {
71         for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, }
72     };
73 }
74 
75 macro_rules! bounded_tuple {
76     ( $($name:ident)* ) => (
77         impl<$($name: Bounded,)*> Bounded for ($($name,)*) {
78             #[inline]
79             fn min_value() -> Self {
80                 ($($name::min_value(),)*)
81             }
82             #[inline]
83             fn max_value() -> Self {
84                 ($($name::max_value(),)*)
85             }
86         }
87     );
88 }
89 
90 for_each_tuple!(bounded_tuple);
91 bounded_impl!(f64, f64::MIN, f64::MAX);
92 
93 #[test]
wrapping_bounded()94 fn wrapping_bounded() {
95     macro_rules! test_wrapping_bounded {
96         ($($t:ty)+) => {
97             $(
98                 assert_eq!(<Wrapping<$t> as Bounded>::min_value().0, <$t>::min_value());
99                 assert_eq!(<Wrapping<$t> as Bounded>::max_value().0, <$t>::max_value());
100             )+
101         };
102     }
103 
104     test_wrapping_bounded!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
105 }
106 
107 #[cfg(has_i128)]
108 #[test]
wrapping_bounded_i128()109 fn wrapping_bounded_i128() {
110     macro_rules! test_wrapping_bounded {
111         ($($t:ty)+) => {
112             $(
113                 assert_eq!(<Wrapping<$t> as Bounded>::min_value().0, <$t>::min_value());
114                 assert_eq!(<Wrapping<$t> as Bounded>::max_value().0, <$t>::max_value());
115             )+
116         };
117     }
118 
119     test_wrapping_bounded!(u128 i128);
120 }
121 
122 #[test]
wrapping_is_bounded()123 fn wrapping_is_bounded() {
124     fn require_bounded<T: Bounded>(_: &T) {}
125     require_bounded(&Wrapping(42_u32));
126     require_bounded(&Wrapping(-42));
127 }
128