Add ConstBounded trait

Add test const_bounded_impl
This commit is contained in:
Marc 2019-03-31 22:23:57 +13:00
parent 981d2bf32d
commit 395b90919a
2 changed files with 91 additions and 18 deletions

View File

@ -7,13 +7,29 @@ use core::{u16, u32, u64, u8, usize};
/// Numbers which have upper and lower bounds /// Numbers which have upper and lower bounds
pub trait Bounded { pub trait Bounded {
// FIXME (#5527): These should be associated constants
/// returns the smallest finite number this type can represent /// returns the smallest finite number this type can represent
fn min_value() -> Self; fn min_value() -> Self;
/// returns the largest finite number this type can represent /// returns the largest finite number this type can represent
fn max_value() -> Self; fn max_value() -> Self;
} }
/// Supplimentary trait for [`Bounded`](trait.Bounded.html) types which can be
/// expressed as compile-time constants.
///
/// This is implemented for all primitive types, and should be implemented
/// wherever possible. Implementors must ensure that `ConstBounded::MIN_VALUE`
/// and `ConstBounded::MAX_VALUE` are the same values produced by
/// [`Bounded::min_value()`](trait.Bounded.html#tymethod.min_value) and
/// [`Bounded::max_value()`](trait.Bounded.html#tymethod.max_value)
/// respectively.
#[cfg(has_associated_consts)]
pub trait ConstBounded: Bounded {
/// The smallest finite number this type can represent
const MIN_VALUE: Self;
/// The largest finite number this type can represent
const MAX_VALUE: Self;
}
macro_rules! bounded_impl { macro_rules! bounded_impl {
($t:ty, $min:expr, $max:expr) => { ($t:ty, $min:expr, $max:expr) => {
impl Bounded for $t { impl Bounded for $t {
@ -30,21 +46,39 @@ macro_rules! bounded_impl {
}; };
} }
bounded_impl!(usize, usize::MIN, usize::MAX); #[cfg(has_associated_consts)]
bounded_impl!(u8, u8::MIN, u8::MAX); macro_rules! bounded_const_impl {
bounded_impl!(u16, u16::MIN, u16::MAX); ($t:ty, $min:expr, $max:expr) => {
bounded_impl!(u32, u32::MIN, u32::MAX); bounded_impl!($t, $min, $max);
bounded_impl!(u64, u64::MIN, u64::MAX); impl ConstBounded for $t {
#[cfg(has_i128)] const MIN_VALUE: $t = $min;
bounded_impl!(u128, u128::MIN, u128::MAX); const MAX_VALUE: $t = $max;
}
}
}
#[cfg(not(has_associated_consts))]
macro_rules! bounded_const_impl {
($t:ty, $min:expr, $max:expr) => { bounded_impl!($t, $min, $max); };
}
bounded_impl!(isize, isize::MIN, isize::MAX); bounded_const_impl!(usize, usize::MIN, usize::MAX);
bounded_impl!(i8, i8::MIN, i8::MAX); bounded_const_impl!(u8, u8::MIN, u8::MAX);
bounded_impl!(i16, i16::MIN, i16::MAX); bounded_const_impl!(u16, u16::MIN, u16::MAX);
bounded_impl!(i32, i32::MIN, i32::MAX); bounded_const_impl!(u32, u32::MIN, u32::MAX);
bounded_impl!(i64, i64::MIN, i64::MAX); bounded_const_impl!(u64, u64::MIN, u64::MAX);
#[cfg(has_i128)] #[cfg(has_i128)]
bounded_impl!(i128, i128::MIN, i128::MAX); bounded_const_impl!(u128, u128::MIN, u128::MAX);
bounded_const_impl!(isize, isize::MIN, isize::MAX);
bounded_const_impl!(i8, i8::MIN, i8::MAX);
bounded_const_impl!(i16, i16::MIN, i16::MAX);
bounded_const_impl!(i32, i32::MIN, i32::MAX);
bounded_const_impl!(i64, i64::MIN, i64::MAX);
#[cfg(has_i128)]
bounded_const_impl!(i128, i128::MIN, i128::MAX);
bounded_const_impl!(f32, f32::MIN, f32::MAX);
bounded_const_impl!(f64, f64::MIN, f64::MAX);
impl<T: Bounded> Bounded for Wrapping<T> { impl<T: Bounded> Bounded for Wrapping<T> {
fn min_value() -> Self { fn min_value() -> Self {
@ -54,8 +88,11 @@ impl<T: Bounded> Bounded for Wrapping<T> {
Wrapping(T::max_value()) Wrapping(T::max_value())
} }
} }
#[cfg(has_associated_consts)]
bounded_impl!(f32, f32::MIN, f32::MAX); impl<T: ConstBounded> ConstBounded for Wrapping<T> {
const MIN_VALUE: Self = Wrapping(T::MIN_VALUE);
const MAX_VALUE: Self = Wrapping(T::MAX_VALUE);
}
macro_rules! for_each_tuple_ { macro_rules! for_each_tuple_ {
( $m:ident !! ) => ( ( $m:ident !! ) => (
@ -86,9 +123,19 @@ macro_rules! bounded_tuple {
} }
); );
} }
for_each_tuple!(bounded_tuple); for_each_tuple!(bounded_tuple);
bounded_impl!(f64, f64::MIN, f64::MAX);
#[cfg(has_associated_consts)]
macro_rules! bounded_const_tuple {
( $($name:ident)* ) => (
impl<$($name: ConstBounded,)*> ConstBounded for ($($name,)*) {
const MIN_VALUE: Self = ($($name::MIN_VALUE,)*);
const MAX_VALUE: Self = ($($name::MAX_VALUE,)*);
}
);
}
#[cfg(has_associated_consts)]
for_each_tuple!(bounded_const_tuple);
#[test] #[test]
fn wrapping_bounded() { fn wrapping_bounded() {
@ -125,3 +172,27 @@ fn wrapping_is_bounded() {
require_bounded(&Wrapping(42_u32)); require_bounded(&Wrapping(42_u32));
require_bounded(&Wrapping(-42)); require_bounded(&Wrapping(-42));
} }
#[test]
#[cfg(has_associated_consts)]
fn const_bounded_impl() {
macro_rules! test_traits_match {
($($t:ty),+) => { $(
assert_eq!(
<$t as Bounded>::min_value(),
<$t as ConstBounded>::MIN_VALUE,
);
assert_eq!(
<$t as Bounded>::max_value(),
<$t as ConstBounded>::MAX_VALUE,
);
)+};
}
test_traits_match!(
u8, u16, u32, u64, usize,
i8, i16, i32, i64, isize,
f32, f64,
Wrapping<usize>, Wrapping<isize>,
(u8, i32, f32, Wrapping<u8>, (i8, ((), u16), Wrapping<i16>, f64))
);
}

View File

@ -26,6 +26,8 @@ use core::ops::{Add, Div, Mul, Rem, Sub};
use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign}; use core::ops::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
pub use bounds::Bounded; pub use bounds::Bounded;
#[cfg(has_associated_consts)]
pub use bounds::ConstBounded;
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use float::Float; pub use float::Float;
pub use float::FloatConst; pub use float::FloatConst;