use std::cmp::PartialEq; use std::num::{Zero, One}; use std::ops::{Add, Sub, Mul, Div, Rem}; /// A trait that defines what is required to be considered /// a number. pub trait Number : Zero + One + Add + Sub + Mul + Div + Rem + PartialEq + Copy + Clone { type StrRadixError; /// Create a number from a given string and base radix. fn from_str_radix(src: &str, radix: u32) -> Result; } // Create some macros to ease typing and reading. /// A macro to make implementing the trait easier for all the /// base integer types in rust. macro_rules! int_trait_impl { ($traitName: ident for $($varType: ty)*) => ($( impl Number for $varType { type StrRadixError = ::std::num::ParseIntError; fn from_str_radix(src: &str, radix: u32) -> Result { <$varType>::from_str_radix(src, radix) } } )*) } /// A macro to make implementing the trait easier for all the /// base float types in rust. macro_rules! float_trait_impl { ($traitName: ident for $($varType: ty)*) => ($( impl $traitName for $varType { type StrRadixError = ::std::num::ParseFloatError; fn from_str_radix(src: &str, radix: u32) -> Result { <$varType>::from_str_radix(src, radix) } } )*) } // Implement the trait for the types that are Numbers. int_trait_impl!(Number for u8 u16 u32 u64 usize); int_trait_impl!(Number for i8 i16 i32 i64 isize); float_trait_impl!(Number for f32 f64);