/rust/registry/src/index.crates.io-6f17d22bba15001f/num-traits-0.2.19/src/bounds.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use core::num::Wrapping; |
2 | | use core::{f32, f64}; |
3 | | use core::{i128, i16, i32, i64, i8, isize}; |
4 | | use core::{u128, u16, u32, u64, u8, usize}; |
5 | | |
6 | | /// Numbers which have upper and lower bounds |
7 | | pub 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 |
16 | | pub 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 |
22 | | impl<T: Bounded> LowerBounded for T { |
23 | 0 | fn min_value() -> T { |
24 | 0 | Bounded::min_value() |
25 | 0 | } |
26 | | } |
27 | | |
28 | | /// Numbers which have upper bounds |
29 | | pub 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 |
35 | | impl<T: Bounded> UpperBounded for T { |
36 | 0 | fn max_value() -> T { |
37 | 0 | Bounded::max_value() |
38 | 0 | } |
39 | | } |
40 | | |
41 | | macro_rules! bounded_impl { |
42 | | ($t:ty, $min:expr, $max:expr) => { |
43 | | impl Bounded for $t { |
44 | | #[inline] |
45 | 0 | fn min_value() -> $t { |
46 | 0 | $min |
47 | 0 | } Unexecuted instantiation: <u8 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <u16 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <f32 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <usize as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <u8 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <u16 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <u32 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <u64 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <u128 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <isize as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <i8 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <i16 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <i32 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <i64 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <i128 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <f32 as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <f64 as num_traits::bounds::Bounded>::min_value |
48 | | |
49 | | #[inline] |
50 | 0 | fn max_value() -> $t { |
51 | 0 | $max |
52 | 0 | } Unexecuted instantiation: <u8 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u16 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <f32 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <usize as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u8 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u16 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u32 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u64 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u128 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <isize as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i8 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i16 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i32 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i64 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i128 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <usize as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u8 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u16 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u32 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u64 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <u128 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <isize as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i8 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i16 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i32 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i64 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <i128 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <f32 as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <f64 as num_traits::bounds::Bounded>::max_value |
53 | | } |
54 | | }; |
55 | | } |
56 | | |
57 | | bounded_impl!(usize, usize::MIN, usize::MAX); |
58 | | bounded_impl!(u8, u8::MIN, u8::MAX); |
59 | | bounded_impl!(u16, u16::MIN, u16::MAX); |
60 | | bounded_impl!(u32, u32::MIN, u32::MAX); |
61 | | bounded_impl!(u64, u64::MIN, u64::MAX); |
62 | | bounded_impl!(u128, u128::MIN, u128::MAX); |
63 | | |
64 | | bounded_impl!(isize, isize::MIN, isize::MAX); |
65 | | bounded_impl!(i8, i8::MIN, i8::MAX); |
66 | | bounded_impl!(i16, i16::MIN, i16::MAX); |
67 | | bounded_impl!(i32, i32::MIN, i32::MAX); |
68 | | bounded_impl!(i64, i64::MIN, i64::MAX); |
69 | | bounded_impl!(i128, i128::MIN, i128::MAX); |
70 | | |
71 | | impl<T: Bounded> Bounded for Wrapping<T> { |
72 | 0 | fn min_value() -> Self { |
73 | 0 | Wrapping(T::min_value()) |
74 | 0 | } |
75 | 0 | fn max_value() -> Self { |
76 | 0 | Wrapping(T::max_value()) |
77 | 0 | } |
78 | | } |
79 | | |
80 | | bounded_impl!(f32, f32::MIN, f32::MAX); |
81 | | |
82 | | macro_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 | | } |
91 | | macro_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 | | |
97 | | macro_rules! bounded_tuple { |
98 | | ( $($name:ident)* ) => ( |
99 | | impl<$($name: Bounded,)*> Bounded for ($($name,)*) { |
100 | | #[inline] |
101 | 0 | fn min_value() -> Self { |
102 | 0 | ($($name::min_value(),)*) |
103 | 0 | } Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_, _) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <(_,) as num_traits::bounds::Bounded>::min_value Unexecuted instantiation: <() as num_traits::bounds::Bounded>::min_value |
104 | | #[inline] |
105 | 0 | fn max_value() -> Self { |
106 | 0 | ($($name::max_value(),)*) |
107 | 0 | } Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_, _) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <(_,) as num_traits::bounds::Bounded>::max_value Unexecuted instantiation: <() as num_traits::bounds::Bounded>::max_value |
108 | | } |
109 | | ); |
110 | | } |
111 | | |
112 | | for_each_tuple!(bounded_tuple); |
113 | | bounded_impl!(f64, f64::MIN, f64::MAX); |
114 | | |
115 | | #[test] |
116 | | fn 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] |
130 | | fn 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] |
144 | | fn wrapping_is_bounded() { |
145 | | fn require_bounded<T: Bounded>(_: &T) {} |
146 | | require_bounded(&Wrapping(42_u32)); |
147 | | require_bounded(&Wrapping(-42)); |
148 | | } |