Skip to main content

const_num_traits/
bounds.rs

1use core::num::Wrapping;
2use core::num::{
3    NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8,
4    NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize,
5};
6
7c0nst::c0nst! {
8/// Numbers which have upper and lower bounds
9pub c0nst trait Bounded {
10    // FIXME (#5527): These should be associated constants
11    /// Returns the smallest finite number this type can represent
12    fn min_value() -> Self;
13    /// Returns the largest finite number this type can represent
14    fn max_value() -> Self;
15}
16}
17
18c0nst::c0nst! {
19/// Numbers which have lower bounds
20pub c0nst trait LowerBounded {
21    /// Returns the smallest finite number this type can represent
22    fn min_value() -> Self;
23}
24}
25
26c0nst::c0nst! {
27// FIXME: With a major version bump, this should be a supertrait instead
28c0nst impl<T: [c0nst] Bounded> LowerBounded for T {
29    fn min_value() -> T {
30        Bounded::min_value()
31    }
32}
33}
34
35c0nst::c0nst! {
36/// Numbers which have upper bounds
37pub c0nst trait UpperBounded {
38    /// Returns the largest finite number this type can represent
39    fn max_value() -> Self;
40}
41}
42
43c0nst::c0nst! {
44// FIXME: With a major version bump, this should be a supertrait instead
45c0nst impl<T: [c0nst] Bounded> UpperBounded for T {
46    fn max_value() -> T {
47        Bounded::max_value()
48    }
49}
50}
51
52macro_rules! bounded_impl {
53    ($t:ty, $min:expr, $max:expr) => {
54        c0nst::c0nst! {
55        c0nst impl Bounded for $t {
56            #[inline]
57            fn min_value() -> $t {
58                $min
59            }
60
61            #[inline]
62            fn max_value() -> $t {
63                $max
64            }
65        }
66        }
67    };
68}
69
70bounded_impl!(usize, usize::MIN, usize::MAX);
71bounded_impl!(u8, u8::MIN, u8::MAX);
72bounded_impl!(u16, u16::MIN, u16::MAX);
73bounded_impl!(u32, u32::MIN, u32::MAX);
74bounded_impl!(u64, u64::MIN, u64::MAX);
75bounded_impl!(u128, u128::MIN, u128::MAX);
76
77bounded_impl!(isize, isize::MIN, isize::MAX);
78bounded_impl!(i8, i8::MIN, i8::MAX);
79bounded_impl!(i16, i16::MIN, i16::MAX);
80bounded_impl!(i32, i32::MIN, i32::MAX);
81bounded_impl!(i64, i64::MIN, i64::MAX);
82bounded_impl!(i128, i128::MIN, i128::MAX);
83
84macro_rules! bounded_impl_nonzero_const {
85    ($t:ty, $v:expr, $i:ident) => {
86        const $i: $t = match <$t>::new($v) {
87            Some(nz) => nz,
88            None => panic!("bad nonzero bound!"),
89        };
90    };
91}
92
93macro_rules! bounded_impl_nonzero {
94    ($t:ty, $min:expr, $max:expr) => {
95        c0nst::c0nst! {
96        c0nst impl Bounded for $t {
97            #[inline]
98            fn min_value() -> $t {
99                // when MSRV is 1.70 we can use $t::MIN
100                bounded_impl_nonzero_const!($t, $min, MIN);
101                MIN
102            }
103
104            #[inline]
105            fn max_value() -> $t {
106                // when MSRV is 1.70 we can use $t::MAX
107                bounded_impl_nonzero_const!($t, $max, MAX);
108                MAX
109            }
110        }
111        }
112    };
113}
114
115bounded_impl_nonzero!(NonZeroUsize, 1, usize::MAX);
116bounded_impl_nonzero!(NonZeroU8, 1, u8::MAX);
117bounded_impl_nonzero!(NonZeroU16, 1, u16::MAX);
118bounded_impl_nonzero!(NonZeroU32, 1, u32::MAX);
119bounded_impl_nonzero!(NonZeroU64, 1, u64::MAX);
120bounded_impl_nonzero!(NonZeroU128, 1, u128::MAX);
121
122bounded_impl_nonzero!(NonZeroIsize, isize::MIN, isize::MAX);
123bounded_impl_nonzero!(NonZeroI8, i8::MIN, i8::MAX);
124bounded_impl_nonzero!(NonZeroI16, i16::MIN, i16::MAX);
125bounded_impl_nonzero!(NonZeroI32, i32::MIN, i32::MAX);
126bounded_impl_nonzero!(NonZeroI64, i64::MIN, i64::MAX);
127bounded_impl_nonzero!(NonZeroI128, i128::MIN, i128::MAX);
128
129c0nst::c0nst! {
130c0nst impl<T: [c0nst] Bounded> Bounded for Wrapping<T> {
131    fn min_value() -> Self {
132        Wrapping(T::min_value())
133    }
134    fn max_value() -> Self {
135        Wrapping(T::max_value())
136    }
137}
138}
139
140bounded_impl!(f32, f32::MIN, f32::MAX);
141
142macro_rules! for_each_tuple_ {
143    ( $m:ident !! ) => (
144        $m! { }
145    );
146    ( $m:ident !! $h:ident, $($t:ident,)* ) => (
147        $m! { $h $($t)* }
148        for_each_tuple_! { $m !! $($t,)* }
149    );
150}
151macro_rules! for_each_tuple {
152    ($m:ident) => {
153        for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, }
154    };
155}
156
157macro_rules! bounded_tuple {
158    ( $($name:ident)* ) => (
159        c0nst::c0nst! {
160        c0nst impl<$($name: [c0nst] Bounded,)*> Bounded for ($($name,)*) {
161            // The empty-tuple expansion yields the unit value `()` as the body.
162            #[inline]
163            #[allow(clippy::unused_unit)]
164            fn min_value() -> Self {
165                ($($name::min_value(),)*)
166            }
167            #[inline]
168            #[allow(clippy::unused_unit)]
169            fn max_value() -> Self {
170                ($($name::max_value(),)*)
171            }
172        }
173        }
174    );
175}
176
177for_each_tuple!(bounded_tuple);
178bounded_impl!(f64, f64::MIN, f64::MAX);
179
180#[test]
181fn wrapping_bounded() {
182    macro_rules! test_wrapping_bounded {
183        ($($t:ty)+) => {
184            $(
185                assert_eq!(<Wrapping<$t> as Bounded>::min_value().0, <$t>::min_value());
186                assert_eq!(<Wrapping<$t> as Bounded>::max_value().0, <$t>::max_value());
187            )+
188        };
189    }
190
191    test_wrapping_bounded!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
192}
193
194#[test]
195fn wrapping_bounded_i128() {
196    macro_rules! test_wrapping_bounded {
197        ($($t:ty)+) => {
198            $(
199                assert_eq!(<Wrapping<$t> as Bounded>::min_value().0, <$t>::min_value());
200                assert_eq!(<Wrapping<$t> as Bounded>::max_value().0, <$t>::max_value());
201            )+
202        };
203    }
204
205    test_wrapping_bounded!(u128 i128);
206}
207
208#[test]
209fn wrapping_is_bounded() {
210    fn require_bounded<T: Bounded>(_: &T) {}
211    require_bounded(&Wrapping(42_u32));
212    require_bounded(&Wrapping(-42));
213}
214
215#[test]
216fn bounded_unsigned_nonzero() {
217    macro_rules! test_bounded_impl_unsigned_nonzero {
218        ($t:ty, $base_ty:ty) => {
219            assert_eq!(<$t as Bounded>::min_value().get(), 1);
220            assert_eq!(<$t as Bounded>::max_value().get(), <$base_ty>::MAX);
221        };
222    }
223
224    test_bounded_impl_unsigned_nonzero!(NonZeroUsize, usize);
225    test_bounded_impl_unsigned_nonzero!(NonZeroU8, u8);
226    test_bounded_impl_unsigned_nonzero!(NonZeroU16, u16);
227    test_bounded_impl_unsigned_nonzero!(NonZeroU32, u32);
228    test_bounded_impl_unsigned_nonzero!(NonZeroU64, u64);
229    test_bounded_impl_unsigned_nonzero!(NonZeroU128, u128);
230}
231
232#[test]
233fn bounded_signed_nonzero() {
234    macro_rules! test_bounded_impl_signed_nonzero {
235        ($t:ty, $base_ty:ty) => {
236            assert_eq!(<$t as Bounded>::min_value().get(), <$base_ty>::MIN);
237            assert_eq!(<$t as Bounded>::max_value().get(), <$base_ty>::MAX);
238        };
239    }
240
241    test_bounded_impl_signed_nonzero!(NonZeroIsize, isize);
242    test_bounded_impl_signed_nonzero!(NonZeroI8, i8);
243    test_bounded_impl_signed_nonzero!(NonZeroI16, i16);
244    test_bounded_impl_signed_nonzero!(NonZeroI32, i32);
245    test_bounded_impl_signed_nonzero!(NonZeroI64, i64);
246    test_bounded_impl_signed_nonzero!(NonZeroI128, i128);
247}