Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement u128/i128 support #13

Merged
merged 5 commits into from
May 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ default = ["std"]
# Enable this to get a std::error::Error impl for convenient use with other
# libraries.
std = []
# Enable this for i128/u128 support
x128 = []

[dev-dependencies]
quickcheck = "0.4.1"
3 changes: 3 additions & 0 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ main() {
cross test --target $TARGET
cross test --target $TARGET --release

[ "$TRAVIS_RUST_VERSION" -eq "nightly" ] && cross test --feature x128 --target $TARGET
[ "$TRAVIS_RUST_VERSION" -eq "nightly" ] && cross test --feature x128 --target $TARGET --release

cross test --no-default-features --target $TARGET
cross test --no-default-features --target $TARGET --release
}
Expand Down
53 changes: 53 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@

#![cfg_attr(not(feature = "std"), no_std)]

#![cfg_attr(feature = "x128", feature(i128_type, i128))]

#[cfg(feature = "std")]
extern crate core;

Expand Down Expand Up @@ -176,6 +178,9 @@ macro_rules! fns {

fns!(f32, f64, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize);

#[cfg(feature = "x128")]
fns!(i128, u128);

/// `$dst` can hold any value of `$src`
macro_rules! promotion {
($($src:ty => $($dst: ty),+);+;) => {
Expand Down Expand Up @@ -415,6 +420,54 @@ mod _64 {
}
}

#[cfg(feature = "x128")]
mod _x128 {
use {Error, From};

// Signed
promotion! {
i8 => i128;
i16 => i128;
i32 => i128;
i64 => i128;
isize => i128;
i128 => f32, f64, i128;
}

half_promotion! {
i8 => u128;
i16 => u128;
i32 => u128;
i64 => u128;
isize => u128;
i128 => u128;
}

from_signed! {
i128 => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize;
}

// Unsigned
promotion! {
u8 => i128, u128;
u16 => i128, u128;
u32 => i128, u128;
u64 => i128, u128;
usize => i128, u128;
u128 => f64, u128;
}

from_unsigned! {
u128 => f32, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, usize;
}

// Float
from_float! {
f32 => i128, u128;
f64 => i128, u128;
}
}

// The missing piece
impl From<f64> for f32 {
type Output = Result<f32, Error>;
Expand Down
33 changes: 33 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@ promote_and_back! {
usize => f32, f64 ;
}

// TODO uncomment this once quickcheck supports Arbitrary for i128/u128
// https://github.com/BurntSushi/quickcheck/issues/162
/*#[cfg(feature = "x128")]
promote_and_back! {
i8 => i128 ;
i16 => i128 ;
i32 => i128 ;
isize => i128 ;
i64 => i128 ;
i128 => f32, f64 ;
u8 => i128, u128;
u16 => i128, u128;
u32 => i128, u128;
usize => i128, u128;
u64 => i128, u128;
u128 => f32, f64 ;
}*/

// If it's Ok to cast `src` to `$dst`, it must also be Ok to cast `dst` back to
// `$src`
macro_rules! symmetric_cast_between {
Expand Down Expand Up @@ -99,6 +117,13 @@ symmetric_cast_between! {
usize => i8, i16, i32, i64, isize;
}

// TODO uncomment this once quickcheck supports Arbitrary for i128/u128
// https://github.com/BurntSushi/quickcheck/issues/162
/*#[cfg(feature = "x128")]
symmetric_cast_between! {
u128 => i8, i16, i32, isize, i64, i128;
}*/

macro_rules! from_float {
($($src:ident => $($dst:ident),+);+;) => {
$(
Expand Down Expand Up @@ -149,3 +174,11 @@ from_float! {
f32 => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize;
f64 => i8, i16, i32, i64, isize, u8, u16, u32, u64, usize;
}

// TODO uncomment this once quickcheck supports Arbitrary for i128/u128
// https://github.com/BurntSushi/quickcheck/issues/162
/*#[cfg(feature = "x128")]
from_float! {
f32 => i128, u128;
f64 => i128, u128;
}*/