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.
This commit is contained in:
Jason Travis Smith 2016-01-06 00:54:44 -05:00
parent 902aeb5dbe
commit 946ad90fa0

View File

@ -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<Output=Self> + Sub<Output=Self> +
{
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<Self, ::std::num::ParseIntError>
{
@ -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<Self, ::std::num::ParseFloatError>
{
@ -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