num_traits/
bounds.rs

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