Skip to content

Commit

Permalink
17.7.10.16.33
Browse files Browse the repository at this point in the history
  • Loading branch information
3442853561 committed Jul 10, 2017
1 parent b3d8a65 commit 9d22156
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 99 deletions.
98 changes: 0 additions & 98 deletions src/integer/integer16.rs

This file was deleted.

11 changes: 11 additions & 0 deletions src/integer/integer16/add.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
53 changes: 53 additions & 0 deletions src/integer/integer16/into.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use super::Integer16;

/// # Example
/// ```Rust
/// impl Into<Integer16> for u32 {
/// fn into(self) -> Integer16 {
/// Integer16::Num(self as u16)
/// }
/// }
/// ```
macro_rules! into_integer16_for_ux {
($($t:ty)*) => ($(
impl Into<Integer16> for $t {
fn into(self) -> Integer16 {
Integer16::Num(self as u16)
}
}
)*)
}

into_integer16_for_ux!{u8 u16 u32 u64 usize}

/// # Example
/// ```Rust
/// impl Into<Integer16> 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<Integer16> 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}
21 changes: 21 additions & 0 deletions src/integer/integer16/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
}




46 changes: 45 additions & 1 deletion src/integer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,48 @@ pub trait Integer<R = Self> {
fn max_value() -> Self;
}

// Integer
/// # Example
/// ```Rust
/// impl Integer for Integer16 {
/// fn new() -> Self {
/// Integer16::Null
/// }
///
/// fn set<T: Into<Integer16>>(&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<T: Into<$t>>(&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}

0 comments on commit 9d22156

Please sign in to comment.