Skip to content

Commit

Permalink
Merge branch 'feature-to-arbitrary-int' of git://github.com/jerry7320…
Browse files Browse the repository at this point in the history
…4/typenum into jerry73204-feature-to-arbitrary-int
  • Loading branch information
paholg committed Mar 12, 2021
2 parents 7e3a878 + 466df16 commit 98a1fd4
Show file tree
Hide file tree
Showing 3 changed files with 452 additions and 44 deletions.
166 changes: 164 additions & 2 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
consts::{N1, P1, U0, U1},
private::{Internal, InternalMarker, PrivateDivInt, PrivateIntegerAdd, PrivateRem},
uint::{UInt, Unsigned},
Cmp, Equal, Greater, Less, NonZero, Pow, PowerOfTwo, Zero,
Cmp, Equal, Greater, Less, NonZero, Pow, PowerOfTwo, ToInt, Zero,
};
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};

Expand Down Expand Up @@ -1176,13 +1176,175 @@ where
}
}

// -----------------------------------------
// ToInt

impl ToInt<i8> for Z0 {
#[inline]
fn to_int() -> i8 {
Self::I8
}
}

impl ToInt<i16> for Z0 {
#[inline]
fn to_int() -> i16 {
Self::I16
}
}

impl ToInt<i32> for Z0 {
#[inline]
fn to_int() -> i32 {
Self::I32
}
}

impl ToInt<i64> for Z0 {
#[inline]
fn to_int() -> i64 {
Self::I64
}
}

// negative numbers

impl<U> ToInt<i8> for NInt<U>
where
U: Unsigned + NonZero,
{
#[inline]
fn to_int() -> i8 {
Self::I8
}
}

impl<U> ToInt<i16> for NInt<U>
where
U: Unsigned + NonZero,
{
#[inline]
fn to_int() -> i16 {
Self::I16
}
}

impl<U> ToInt<i32> for NInt<U>
where
U: Unsigned + NonZero,
{
#[inline]
fn to_int() -> i32 {
Self::I32
}
}

impl<U> ToInt<i64> for NInt<U>
where
U: Unsigned + NonZero,
{
#[inline]
fn to_int() -> i64 {
Self::I64
}
}

// positive numbers

impl<U> ToInt<i8> for PInt<U>
where
U: Unsigned + NonZero,
{
#[inline]
fn to_int() -> i8 {
Self::I8
}
}

impl<U> ToInt<i16> for PInt<U>
where
U: Unsigned + NonZero,
{
#[inline]
fn to_int() -> i16 {
Self::I16
}
}

impl<U> ToInt<i32> for PInt<U>
where
U: Unsigned + NonZero,
{
#[inline]
fn to_int() -> i32 {
Self::I32
}
}

impl<U> ToInt<i64> for PInt<U>
where
U: Unsigned + NonZero,
{
#[inline]
fn to_int() -> i64 {
Self::I64
}
}

#[cfg(test)]
mod tests {
use crate::{consts::*, Integer};
use crate::{consts::*, Integer, ToInt};

#[test]
fn to_ix_min() {
assert_eq!(N128::to_i8(), ::core::i8::MIN);
assert_eq!(N32768::to_i16(), ::core::i16::MIN);
}

#[test]
fn int_toint_test() {
// i8
assert_eq!(0_i8, Z0::to_int());
assert_eq!(1_i8, P1::to_int());
assert_eq!(2_i8, P2::to_int());
assert_eq!(3_i8, P3::to_int());
assert_eq!(4_i8, P4::to_int());
assert_eq!(-1_i8, N1::to_int());
assert_eq!(-2_i8, N2::to_int());
assert_eq!(-3_i8, N3::to_int());
assert_eq!(-4_i8, N4::to_int());

// i16
assert_eq!(0_i16, Z0::to_int());
assert_eq!(1_i16, P1::to_int());
assert_eq!(2_i16, P2::to_int());
assert_eq!(3_i16, P3::to_int());
assert_eq!(4_i16, P4::to_int());
assert_eq!(-1_i16, N1::to_int());
assert_eq!(-2_i16, N2::to_int());
assert_eq!(-3_i16, N3::to_int());
assert_eq!(-4_i16, N4::to_int());

// i32
assert_eq!(0_i32, Z0::to_int());
assert_eq!(1_i32, P1::to_int());
assert_eq!(2_i32, P2::to_int());
assert_eq!(3_i32, P3::to_int());
assert_eq!(4_i32, P4::to_int());
assert_eq!(-1_i32, N1::to_int());
assert_eq!(-2_i32, N2::to_int());
assert_eq!(-3_i32, N3::to_int());
assert_eq!(-4_i32, N4::to_int());

// i64
assert_eq!(0_i64, Z0::to_int());
assert_eq!(1_i64, P1::to_int());
assert_eq!(2_i64, P2::to_int());
assert_eq!(3_i64, P3::to_int());
assert_eq!(4_i64, P4::to_int());
assert_eq!(-1_i64, N1::to_int());
assert_eq!(-2_i64, N2::to_int());
assert_eq!(-3_i64, N3::to_int());
assert_eq!(-4_i64, N4::to_int());
}
}
10 changes: 10 additions & 0 deletions src/type_operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,13 @@ pub trait Gcd<Rhs> {
/// The greatest common divisor.
type Output;
}

/// A **type operator** for taking a concrete integer value from a type.
///
/// It returns arbitrary integer value without explicitly specifying the
/// type. It is useful when you pass the values to methods that accept
/// distinct types without runtime casting.
pub trait ToInt<T> {
/// Method returning the concrete value for the type.
fn to_int() -> T;
}
Loading

0 comments on commit 98a1fd4

Please sign in to comment.