From c6163dac4086d788fbd5b917bb92808f431f6027 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 25 Apr 2015 16:43:18 -0700 Subject: [PATCH 1/2] traits.rs: Implement Bounded for tuples of Bounded types For example, the following will print (4294967295, 65535): extern crate num; use num::traits::Bounded; fn main() { let t : (u32, u16) = Bounded::max_value(); println!("{:?}", t); } --- src/traits.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/traits.rs b/src/traits.rs index f1fa70d..bcd69ef 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -463,6 +463,30 @@ bounded_impl!(i64, i64::MIN, i64::MAX); bounded_impl!(f32, f32::MIN, f32::MAX); bounded_impl!(f64, f64::MIN, f64::MAX); +macro_rules! bounded_tuple { + ( $($name:ident)* ) => ( + impl<$($name: Bounded,)*> Bounded for ($($name,)*) { + fn min_value() -> Self { + ($($name::min_value(),)*) + } + fn max_value() -> Self { + ($($name::max_value(),)*) + } + } + ); +} +macro_rules! bounded_tuples { + () => ( + bounded_tuple! { } + ); + ( $h:ident, $($t:ident,)* ) => ( + bounded_tuple! { $h $($t)* } + bounded_tuples! { $($t,)* } + ); +} + +bounded_tuples! { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, } + /// Saturating math operations pub trait Saturating { /// Saturating addition operator. From 6f2db9b6317b07386233f801bddcbbc851177acb Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 25 Apr 2015 17:03:05 -0700 Subject: [PATCH 2/2] Factor out more generic macro to run a macro on each tuple type --- src/traits.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/traits.rs b/src/traits.rs index bcd69ef..61374b5 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -463,6 +463,21 @@ bounded_impl!(i64, i64::MIN, i64::MAX); bounded_impl!(f32, f32::MIN, f32::MAX); bounded_impl!(f64, f64::MIN, f64::MAX); +macro_rules! for_each_tuple_ { + ( $m:ident !! ) => ( + $m! { } + ); + ( $m:ident !! $h:ident, $($t:ident,)* ) => ( + $m! { $h $($t)* } + for_each_tuple_! { $m !! $($t,)* } + ); +} +macro_rules! for_each_tuple { + ( $m:ident ) => ( + for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, } + ); +} + macro_rules! bounded_tuple { ( $($name:ident)* ) => ( impl<$($name: Bounded,)*> Bounded for ($($name,)*) { @@ -475,17 +490,8 @@ macro_rules! bounded_tuple { } ); } -macro_rules! bounded_tuples { - () => ( - bounded_tuple! { } - ); - ( $h:ident, $($t:ident,)* ) => ( - bounded_tuple! { $h $($t)* } - bounded_tuples! { $($t,)* } - ); -} -bounded_tuples! { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, } +for_each_tuple!(bounded_tuple); /// Saturating math operations pub trait Saturating {