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

Add support for 128-bit integers #29

Merged
merged 1 commit into from
Jun 16, 2018
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: rust
rust:
- 1.15.0
- 1.26.0
- stable
- beta
- nightly
Expand Down
16 changes: 10 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ repository = "https://github.com/rust-num/num-rational"
version = "0.2.0-git"
publish = false
readme = "README.md"
build = "build.rs"

[package.metadata.docs.rs]
all-features = true
features = ["std", "bigint-std", "serde"]

[dependencies]

[dependencies.num-bigint]
optional = true
version = "0.2.0"
default-features = false

[dependencies.num-integer]
version = "0.1.36"
version = "0.1.38"
default-features = false

[dependencies.num-traits]
version = "0.2.1"
version = "0.2.4"
default-features = false

[dependencies.serde]
Expand All @@ -35,6 +37,8 @@ version = "1.0.0"
default-features = false

[features]
default = ["bigint", "std"]
bigint = ["num-bigint", "std"]
std = ["num-traits/std"]
default = ["bigint-std", "std"]
i128 = ["num-integer/i128", "num-traits/i128"]
std = ["num-integer/std", "num-traits/std"]
bigint = ["num-bigint"]
bigint-std = ["bigint", "num-bigint/std"]
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![crate](https://img.shields.io/crates/v/num-rational.svg)](https://crates.io/crates/num-rational)
[![documentation](https://docs.rs/num-rational/badge.svg)](https://docs.rs/num-rational)
![minimum rustc 1.8](https://img.shields.io/badge/rustc-1.8+-red.svg)
![minimum rustc 1.15](https://img.shields.io/badge/rustc-1.15+-red.svg)
[![Travis status](https://travis-ci.org/rust-num/num-rational.svg?branch=master)](https://travis-ci.org/rust-num/num-rational)

Generic `Rational` numbers for Rust.
Expand Down Expand Up @@ -33,10 +33,14 @@ version = "0.2"
default-features = false
```

Implementations for `i128` and `u128` are only available with Rust 1.26 and
later. The build script automatically detects this, but you can make it
mandatory by enabling the `i128` crate feature.

## Releases

Release notes are available in [RELEASES.md](RELEASES.md).

## Compatibility

The `num-rational` crate is tested for rustc 1.8 and greater.
The `num-rational` crate is tested for rustc 1.15 and greater.
35 changes: 35 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::env;
use std::io::Write;
use std::process::{Command, Stdio};

fn main() {
if probe("fn main() { 0i128; }") {
println!("cargo:rustc-cfg=has_i128");
} else if env::var_os("CARGO_FEATURE_I128").is_some() {
panic!("i128 support was not detected!");
}
}

/// Test if a code snippet can be compiled
fn probe(code: &str) -> bool {
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
let out_dir = env::var_os("OUT_DIR").expect("environment variable OUT_DIR");

let mut child = Command::new(rustc)
.arg("--out-dir")
.arg(out_dir)
.arg("--emit=obj")
.arg("-")
.stdin(Stdio::piped())
.spawn()
.expect("rustc probe");

child
.stdin
.as_mut()
.expect("rustc stdin")
.write_all(code.as_bytes())
.expect("write rustc stdin");

child.wait().expect("rustc probe").success()
}
4 changes: 2 additions & 2 deletions ci/rustup.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/sh
# Use rustup to locally run the same suite of tests as .travis.yml.
# (You should first install/update 1.15.0, stable, beta, and nightly.)
# (You should first install/update all versions listed below.)

set -ex

export TRAVIS_RUST_VERSION
for TRAVIS_RUST_VERSION in 1.15.0 stable beta nightly; do
for TRAVIS_RUST_VERSION in 1.15.0 1.26.0 stable beta nightly; do
run="rustup run $TRAVIS_RUST_VERSION"
$run cargo build --verbose
$run $PWD/ci/test_full.sh
Expand Down
11 changes: 10 additions & 1 deletion ci/test_full.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ set -ex

echo Testing num-rational on rustc ${TRAVIS_RUST_VERSION}

FEATURES="std bigint-std serde"
if [[ "$TRAVIS_RUST_VERSION" =~ ^(nightly|beta|stable|1.26.0)$ ]]; then
FEATURES="$FEATURES i128"
fi

# num-rational should build and test everywhere.
cargo build --verbose
cargo test --verbose
Expand All @@ -13,7 +18,11 @@ cargo build --no-default-features
cargo test --no-default-features

# Each isolated feature should also work everywhere.
for feature in bigint serde std; do
for feature in $FEATURES; do
cargo build --verbose --no-default-features --features="$feature"
cargo test --verbose --no-default-features --features="$feature"
done

# test all supported features together
cargo build --features="$FEATURES"
cargo test --features="$FEATURES"
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,10 +991,20 @@ impl FromPrimitive for Ratio<BigInt> {
Some(Ratio::from_integer(n.into()))
}

#[cfg(has_i128)]
fn from_i128(n: i128) -> Option<Self> {
Some(Ratio::from_integer(n.into()))
}

fn from_u64(n: u64) -> Option<Self> {
Some(Ratio::from_integer(n.into()))
}

#[cfg(has_i128)]
fn from_u128(n: u128) -> Option<Self> {
Some(Ratio::from_integer(n.into()))
}

fn from_f32(n: f32) -> Option<Self> {
Ratio::from_float(n)
}
Expand All @@ -1011,10 +1021,20 @@ macro_rules! from_primitive_integer {
<$typ as FromPrimitive>::from_i64(n).map(Ratio::from_integer)
}

#[cfg(has_i128)]
fn from_i128(n: i128) -> Option<Self> {
<$typ as FromPrimitive>::from_i128(n).map(Ratio::from_integer)
}

fn from_u64(n: u64) -> Option<Self> {
<$typ as FromPrimitive>::from_u64(n).map(Ratio::from_integer)
}

#[cfg(has_i128)]
fn from_u128(n: u128) -> Option<Self> {
<$typ as FromPrimitive>::from_u128(n).map(Ratio::from_integer)
}

fn from_f32(n: f32) -> Option<Self> {
$approx(n, 10e-20, 30)
}
Expand All @@ -1030,12 +1050,16 @@ from_primitive_integer!(i8, approximate_float);
from_primitive_integer!(i16, approximate_float);
from_primitive_integer!(i32, approximate_float);
from_primitive_integer!(i64, approximate_float);
#[cfg(has_i128)]
from_primitive_integer!(i128, approximate_float);
from_primitive_integer!(isize, approximate_float);

from_primitive_integer!(u8, approximate_float_unsigned);
from_primitive_integer!(u16, approximate_float_unsigned);
from_primitive_integer!(u32, approximate_float_unsigned);
from_primitive_integer!(u64, approximate_float_unsigned);
#[cfg(has_i128)]
from_primitive_integer!(u128, approximate_float_unsigned);
from_primitive_integer!(usize, approximate_float_unsigned);

impl<T: Integer + Signed + Bounded + NumCast + Clone> Ratio<T> {
Expand Down Expand Up @@ -1706,7 +1730,7 @@ mod test {
#[cfg(feature = "bigint")]
#[test]
fn test_from_float_fail() {
use std::{f32, f64};
use core::{f32, f64};

assert_eq!(Ratio::from_float(f32::NAN), None);
assert_eq!(Ratio::from_float(f32::INFINITY), None);
Expand Down