From 946ad90fa033b01d5581d11c93f79d1de39e7921 Mon Sep 17 00:00:00 2001 From: Jason Travis Smith Date: Wed, 6 Jan 2016 00:54:44 -0500 Subject: [PATCH] Numbers now define how many bytes it takes to represent them. Numbers now have an associated constant value that defines how many bytes it takes to define them. --- src/number.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/number.rs b/src/number.rs index 6f13328..88a56e8 100644 --- a/src/number.rs +++ b/src/number.rs @@ -1,3 +1,5 @@ +use std::{u8, u16, u32, u64, usize}; +use std::{i8, i16, i32, i64, isize}; use std::cmp::PartialEq; use std::mem::size_of; use std::num::{Zero, One}; @@ -15,6 +17,11 @@ pub trait Number : Zero + One + Add + Sub + { type StrRadixError; + + /// The amount of Bytes required to represent this Number. + const BYTES: u8; + + /// Create a number from a given string and base radix. /// ///``` @@ -749,6 +756,10 @@ macro_rules! int_number_trait_impl { type StrRadixError = ::std::num::ParseIntError; + + const BYTES: u8 = <$varType>::BYTES; + + fn from_str_radix(src: &str, radix: u32) -> Result { @@ -762,12 +773,16 @@ macro_rules! int_number_trait_impl /// base float types in rust. macro_rules! float_number_trait_impl { - ($traitName: ident for $($varType: ty)*) => + ($traitName: ident for $($varType: ty, $numBytes: expr)*) => ($( impl $traitName for $varType { type StrRadixError = ::std::num::ParseFloatError; + + const BYTES: u8 = $numBytes; + + fn from_str_radix(src: &str, radix: u32) -> Result { @@ -787,7 +802,7 @@ macro_rules! float_number_trait_impl // Implement the Number trait for the types that are Numbers. int_number_trait_impl!(Number for u8 u16 u32 u64 usize); int_number_trait_impl!(Number for i8 i16 i32 i64 isize); -float_number_trait_impl!(Number for f32 f64); +float_number_trait_impl!(Number for f32, 4 f64, 8); // Implement the ToNumber and FromNumber traits for // the types that are Numbers. The FromNumber trait needs