bounds.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. use std::{usize, u8, u16, u32, u64};
  2. use std::{isize, i8, i16, i32, i64};
  3. use std::{f32, f64};
  4. /// Numbers which have upper and lower bounds
  5. pub trait Bounded {
  6. // FIXME (#5527): These should be associated constants
  7. /// returns the smallest finite number this type can represent
  8. fn min_value() -> Self;
  9. /// returns the largest finite number this type can represent
  10. fn max_value() -> Self;
  11. }
  12. macro_rules! bounded_impl {
  13. ($t:ty, $min:expr, $max:expr) => {
  14. impl Bounded for $t {
  15. #[inline]
  16. fn min_value() -> $t { $min }
  17. #[inline]
  18. fn max_value() -> $t { $max }
  19. }
  20. }
  21. }
  22. bounded_impl!(usize, usize::MIN, usize::MAX);
  23. bounded_impl!(u8, u8::MIN, u8::MAX);
  24. bounded_impl!(u16, u16::MIN, u16::MAX);
  25. bounded_impl!(u32, u32::MIN, u32::MAX);
  26. bounded_impl!(u64, u64::MIN, u64::MAX);
  27. bounded_impl!(isize, isize::MIN, isize::MAX);
  28. bounded_impl!(i8, i8::MIN, i8::MAX);
  29. bounded_impl!(i16, i16::MIN, i16::MAX);
  30. bounded_impl!(i32, i32::MIN, i32::MAX);
  31. bounded_impl!(i64, i64::MIN, i64::MAX);
  32. bounded_impl!(f32, f32::MIN, f32::MAX);
  33. macro_rules! for_each_tuple_ {
  34. ( $m:ident !! ) => (
  35. $m! { }
  36. );
  37. ( $m:ident !! $h:ident, $($t:ident,)* ) => (
  38. $m! { $h $($t)* }
  39. for_each_tuple_! { $m !! $($t,)* }
  40. );
  41. }
  42. macro_rules! for_each_tuple {
  43. ( $m:ident ) => (
  44. for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, }
  45. );
  46. }
  47. macro_rules! bounded_tuple {
  48. ( $($name:ident)* ) => (
  49. impl<$($name: Bounded,)*> Bounded for ($($name,)*) {
  50. #[inline]
  51. fn min_value() -> Self {
  52. ($($name::min_value(),)*)
  53. }
  54. #[inline]
  55. fn max_value() -> Self {
  56. ($($name::max_value(),)*)
  57. }
  58. }
  59. );
  60. }
  61. for_each_tuple!(bounded_tuple);
  62. bounded_impl!(f64, f64::MIN, f64::MAX);