• 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 /// Numbers which have lower bounds
18 pub trait LowerBounded {
19     /// Returns the smallest finite number this type can represent
min_value() -> Self20     fn min_value() -> Self;
21 }
22 
23 // FIXME: With a major version bump, this should be a supertrait instead
24 impl<T: Bounded> LowerBounded for T {
min_value() -> T25     fn min_value() -> T {
26         Bounded::min_value()
27     }
28 }
29 
30 /// Numbers which have upper bounds
31 pub trait UpperBounded {
32     /// Returns the largest finite number this type can represent
max_value() -> Self33     fn max_value() -> Self;
34 }
35 
36 // FIXME: With a major version bump, this should be a supertrait instead
37 impl<T: Bounded> UpperBounded for T {
max_value() -> T38     fn max_value() -> T {
39         Bounded::max_value()
40     }
41 }
42 
43 macro_rules! bounded_impl {
44     ($t:ty, $min:expr, $max:expr) => {
45         impl Bounded for $t {
46             #[inline]
47             fn min_value() -> $t {
48                 $min
49             }
50 
51             #[inline]
52             fn max_value() -> $t {
53                 $max
54             }
55         }
56     };
57 }
58 
59 bounded_impl!(usize, usize::MIN, usize::MAX);
60 bounded_impl!(u8, u8::MIN, u8::MAX);
61 bounded_impl!(u16, u16::MIN, u16::MAX);
62 bounded_impl!(u32, u32::MIN, u32::MAX);
63 bounded_impl!(u64, u64::MIN, u64::MAX);
64 #[cfg(has_i128)]
65 bounded_impl!(u128, u128::MIN, u128::MAX);
66 
67 bounded_impl!(isize, isize::MIN, isize::MAX);
68 bounded_impl!(i8, i8::MIN, i8::MAX);
69 bounded_impl!(i16, i16::MIN, i16::MAX);
70 bounded_impl!(i32, i32::MIN, i32::MAX);
71 bounded_impl!(i64, i64::MIN, i64::MAX);
72 #[cfg(has_i128)]
73 bounded_impl!(i128, i128::MIN, i128::MAX);
74 
75 impl<T: Bounded> Bounded for Wrapping<T> {
min_value() -> Self76     fn min_value() -> Self {
77         Wrapping(T::min_value())
78     }
max_value() -> Self79     fn max_value() -> Self {
80         Wrapping(T::max_value())
81     }
82 }
83 
84 bounded_impl!(f32, f32::MIN, f32::MAX);
85 
86 macro_rules! for_each_tuple_ {
87     ( $m:ident !! ) => (
88         $m! { }
89     );
90     ( $m:ident !! $h:ident, $($t:ident,)* ) => (
91         $m! { $h $($t)* }
92         for_each_tuple_! { $m !! $($t,)* }
93     );
94 }
95 macro_rules! for_each_tuple {
96     ($m:ident) => {
97         for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, }
98     };
99 }
100 
101 macro_rules! bounded_tuple {
102     ( $($name:ident)* ) => (
103         impl<$($name: Bounded,)*> Bounded for ($($name,)*) {
104             #[inline]
105             fn min_value() -> Self {
106                 ($($name::min_value(),)*)
107             }
108             #[inline]
109             fn max_value() -> Self {
110                 ($($name::max_value(),)*)
111             }
112         }
113     );
114 }
115 
116 for_each_tuple!(bounded_tuple);
117 bounded_impl!(f64, f64::MIN, f64::MAX);
118 
119 #[test]
wrapping_bounded()120 fn wrapping_bounded() {
121     macro_rules! test_wrapping_bounded {
122         ($($t:ty)+) => {
123             $(
124                 assert_eq!(<Wrapping<$t> as Bounded>::min_value().0, <$t>::min_value());
125                 assert_eq!(<Wrapping<$t> as Bounded>::max_value().0, <$t>::max_value());
126             )+
127         };
128     }
129 
130     test_wrapping_bounded!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
131 }
132 
133 #[cfg(has_i128)]
134 #[test]
wrapping_bounded_i128()135 fn wrapping_bounded_i128() {
136     macro_rules! test_wrapping_bounded {
137         ($($t:ty)+) => {
138             $(
139                 assert_eq!(<Wrapping<$t> as Bounded>::min_value().0, <$t>::min_value());
140                 assert_eq!(<Wrapping<$t> as Bounded>::max_value().0, <$t>::max_value());
141             )+
142         };
143     }
144 
145     test_wrapping_bounded!(u128 i128);
146 }
147 
148 #[test]
wrapping_is_bounded()149 fn wrapping_is_bounded() {
150     fn require_bounded<T: Bounded>(_: &T) {}
151     require_bounded(&Wrapping(42_u32));
152     require_bounded(&Wrapping(-42));
153 }
154