]> git.proxmox.com Git - rustc.git/blame - src/vendor/num-traits/src/bounds.rs
New upstream version 1.26.0+dfsg1
[rustc.git] / src / vendor / num-traits / src / bounds.rs
CommitLineData
0531ce1d
XL
1use core::{usize, u8, u16, u32, u64};
2use core::{isize, i8, i16, i32, i64};
3use core::{f32, f64};
4use core::num::Wrapping;
8bb4bdeb
XL
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
15macro_rules! bounded_impl {
16 ($t:ty, $min:expr, $max:expr) => {
17 impl Bounded for $t {
18 #[inline]
19 fn min_value() -> $t { $min }
20
21 #[inline]
22 fn max_value() -> $t { $max }
23 }
24 }
25}
26
27bounded_impl!(usize, usize::MIN, usize::MAX);
28bounded_impl!(u8, u8::MIN, u8::MAX);
29bounded_impl!(u16, u16::MIN, u16::MAX);
30bounded_impl!(u32, u32::MIN, u32::MAX);
31bounded_impl!(u64, u64::MIN, u64::MAX);
32
33bounded_impl!(isize, isize::MIN, isize::MAX);
34bounded_impl!(i8, i8::MIN, i8::MAX);
35bounded_impl!(i16, i16::MIN, i16::MAX);
36bounded_impl!(i32, i32::MIN, i32::MAX);
37bounded_impl!(i64, i64::MIN, i64::MAX);
38
041b39d2
XL
39impl<T: Bounded> Bounded for Wrapping<T> {
40 fn min_value() -> Self { Wrapping(T::min_value()) }
41 fn max_value() -> Self { Wrapping(T::max_value()) }
42}
43
8bb4bdeb
XL
44bounded_impl!(f32, f32::MIN, f32::MAX);
45
46macro_rules! for_each_tuple_ {
47 ( $m:ident !! ) => (
48 $m! { }
49 );
50 ( $m:ident !! $h:ident, $($t:ident,)* ) => (
51 $m! { $h $($t)* }
52 for_each_tuple_! { $m !! $($t,)* }
53 );
54}
55macro_rules! for_each_tuple {
56 ( $m:ident ) => (
57 for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, }
58 );
59}
60
61macro_rules! bounded_tuple {
62 ( $($name:ident)* ) => (
63 impl<$($name: Bounded,)*> Bounded for ($($name,)*) {
64 #[inline]
65 fn min_value() -> Self {
66 ($($name::min_value(),)*)
67 }
68 #[inline]
69 fn max_value() -> Self {
70 ($($name::max_value(),)*)
71 }
72 }
73 );
74}
75
76for_each_tuple!(bounded_tuple);
77bounded_impl!(f64, f64::MIN, f64::MAX);
041b39d2
XL
78
79
041b39d2
XL
80#[test]
81fn wrapping_bounded() {
ff7c6d11
XL
82 macro_rules! test_wrapping_bounded {
83 ($($t:ty)+) => {
84 $(
85 assert_eq!(Wrapping::<$t>::min_value().0, <$t>::min_value());
86 assert_eq!(Wrapping::<$t>::max_value().0, <$t>::max_value());
87 )+
88 };
89 }
90
041b39d2
XL
91 test_wrapping_bounded!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
92}
93
94#[test]
95fn wrapping_is_bounded() {
96 fn require_bounded<T: Bounded>(_: &T) {}
97 require_bounded(&Wrapping(42_u32));
98 require_bounded(&Wrapping(-42));
99}