diff --git a/src/integer/integer16.rs b/src/integer/integer16.rs deleted file mode 100644 index 086ee10..0000000 --- a/src/integer/integer16.rs +++ /dev/null @@ -1,98 +0,0 @@ -use super::Integer; -use std::ops::Add; - -#[derive(Debug, Copy, Clone, PartialEq)] -pub enum Integer16 { - Num(u16), - Null, -} - -impl Into for u16 { - fn into(self) -> Integer16 { - Integer16::Num(self) - } -} - -impl Into for u32 { - fn into(self) -> Integer16 { - Integer16::Num(self as u16) - } -} - -impl Into for u64 { - fn into(self) -> Integer16 { - Integer16::Num(self as u16) - } -} - -impl Into for i16 { - fn into(self) -> Integer16 { - let mut _self = 0; - if self < 0 { - _self = (u16::max_value() as i32 + self as i32 + 1) as u16 - } else { - _self = self as u16 - } - Integer16::Num(_self) - } -} - -impl Into for i32 { - fn into(self) -> Integer16 { - let mut _self = 0; - if self < 0 { - _self = (u16::max_value() as i32 + (self as i16) as i32 + 1) as u16 - } else { - _self = self as u16 - } - Integer16::Num(_self) - } -} - -impl Into for i64 { - fn into(self) -> Integer16 { - let mut _self = 0; - if self < 0 { - _self = (u16::max_value() as i32 + (self as i16) as i32 + 1) as u16 - } else { - _self = self as u16 - } - Integer16::Num(_self) - } -} - -impl Integer16 { - fn get_num(&self) -> u16 { - match *self { - Integer16::Num(e) => e, - _ => 0, - } - } -} - -impl Integer for Integer16 { - fn new() -> Self { - Integer16::Null - } - - fn set>(&mut self, num: T){ - *self = num.into(); - } - - fn min_value() -> Self{ - Integer16::Num(0) - } - fn max_value() -> Self{ - Integer16::Num(65535) - } -} - -impl Add for Integer16 { - type Output = Integer16; - fn add(self, other: Integer16) -> Integer16 { - let _self = self.get_num() as u32; - let _other = other.get_num() as u32; - Integer16::Num((_self + _other) as u16) - } -} - diff --git a/src/integer/integer16/add.rs b/src/integer/integer16/add.rs new file mode 100644 index 0000000..264b01c --- /dev/null +++ b/src/integer/integer16/add.rs @@ -0,0 +1,11 @@ +use super::Integer16; +use std::ops::Add; + +impl Add for Integer16 { + type Output = Integer16; + fn add(self, other: Integer16) -> Integer16 { + let _self = self.get_num() as u32; + let _other = other.get_num() as u32; + Integer16::Num((_self + _other) as u16) + } +} diff --git a/src/integer/integer16/into.rs b/src/integer/integer16/into.rs new file mode 100644 index 0000000..e0d25bc --- /dev/null +++ b/src/integer/integer16/into.rs @@ -0,0 +1,53 @@ +use super::Integer16; + +/// # Example +/// ```Rust +/// impl Into for u32 { +/// fn into(self) -> Integer16 { +/// Integer16::Num(self as u16) +/// } +/// } +/// ``` +macro_rules! into_integer16_for_ux { + ($($t:ty)*) => ($( + impl Into for $t { + fn into(self) -> Integer16 { + Integer16::Num(self as u16) + } + } + )*) +} + +into_integer16_for_ux!{u8 u16 u32 u64 usize} + +/// # Example +/// ```Rust +/// impl Into for i32 { +/// fn into(self) -> Integer16 { +/// let mut _self = 0; +/// if self < 0 { +/// _self = (u16::max_value() as i32 + (self as i16) as i32 + 1) as u16 +/// } else { +/// _self = self as u16 +/// } +/// Integer16::Num(_self) +/// } +/// } +/// ``` +macro_rules! into_integer16_for_ix { + ($($t:ty)*) => ($( + impl Into for $t { + fn into(self) -> Integer16 { + let mut _self = 0; + if self < 0 { + _self = (u16::max_value() as i32 + (self as i16) as i32 + 1) as u16 + } else { + _self = self as u16 + } + Integer16::Num(_self) + } + } + )*) +} + +into_integer16_for_ix!{i8 i16 i32 i64 isize} diff --git a/src/integer/integer16/mod.rs b/src/integer/integer16/mod.rs new file mode 100644 index 0000000..ee4c393 --- /dev/null +++ b/src/integer/integer16/mod.rs @@ -0,0 +1,21 @@ +mod into; +mod add; + +#[derive(Debug, Copy, Clone, PartialEq)] +pub enum Integer16 { + Num(u16), + Null, +} + +impl Integer16 { + fn get_num(&self) -> u16 { + match *self { + Integer16::Num(e) => e, + _ => 0, + } + } +} + + + + diff --git a/src/integer/mod.rs b/src/integer/mod.rs index d011acf..0f530e5 100644 --- a/src/integer/mod.rs +++ b/src/integer/mod.rs @@ -8,4 +8,48 @@ pub trait Integer { fn max_value() -> Self; } -// Integer +/// # Example +/// ```Rust +/// impl Integer for Integer16 { +/// fn new() -> Self { +/// Integer16::Null +/// } +/// +/// fn set>(&mut self, num: T) { +/// *self = num.into(); +/// } +/// +/// fn min_value() -> Self { +/// Integer16::Num(0) +/// } +/// +/// fn max_value() -> Self { +/// Integer16::Num(65535) +/// } +/// } +/// ``` +macro_rules! integer_impl { + ($($t:ident)*) => ($( + impl Integer for $t { + fn new() -> Self { + $t::Null + } + + fn set>(&mut self, num: T) { + *self = num.into(); + } + + fn min_value() -> Self { + $t::Num(0) + } + + fn max_value() -> Self { + let foo = -1; + foo.into() + } + } + )*) +} + +integer_impl!{Integer16} +