Skip to content
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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ categories = ["algorithms", "science", "no-std"]
license = "MIT/Apache-2.0"
repository = "https://github.com/rust-num/num-traits"
name = "num-traits"
version = "0.2.3"
version = "0.2.4"
readme = "README.md"
build = "build.rs"

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

[dependencies]

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ The `Float` and `Real` traits are only available when `std` is enabled. The
and `f64` also require `std`, as do implementations of signed and floating-
point exponents in `Pow`.

Implementations for `i128` and `u128` are only available when `i128` is enabled.
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

Expand Down
10 changes: 10 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# Release 0.2.4

- [Support for 128-bit integers is now automatically detected and enabled.][69]
Setting the `i128` crate feature now causes the build script to panic if such
support is not detected.

**Contributors**: @cuviper

[69]: https://github.com/rust-num/num-traits/pull/69

# Release 0.2.3

- [The new `CheckedNeg` and `CheckedRem` traits][63] perform checked `Neg` and
Expand Down
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()
}
8 changes: 4 additions & 4 deletions src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::{usize, u8, u16, u32, u64};
use core::{isize, i8, i16, i32, i64};
use core::{f32, f64};
use core::num::Wrapping;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
use core::{i128, u128};

/// Numbers which have upper and lower bounds
Expand Down Expand Up @@ -31,15 +31,15 @@ bounded_impl!(u8, u8::MIN, u8::MAX);
bounded_impl!(u16, u16::MIN, u16::MAX);
bounded_impl!(u32, u32::MIN, u32::MAX);
bounded_impl!(u64, u64::MIN, u64::MAX);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
bounded_impl!(u128, u128::MIN, u128::MAX);

bounded_impl!(isize, isize::MIN, isize::MAX);
bounded_impl!(i8, i8::MIN, i8::MAX);
bounded_impl!(i16, i16::MIN, i16::MAX);
bounded_impl!(i32, i32::MIN, i32::MAX);
bounded_impl!(i64, i64::MIN, i64::MAX);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
bounded_impl!(i128, i128::MIN, i128::MAX);

impl<T: Bounded> Bounded for Wrapping<T> {
Expand Down Expand Up @@ -97,7 +97,7 @@ fn wrapping_bounded() {
test_wrapping_bounded!(usize u8 u16 u32 u64 isize i8 i16 i32 i64);
}

#[cfg(feature = "i128")]
#[cfg(has_i128)]
#[test]
fn wrapping_bounded_i128() {
macro_rules! test_wrapping_bounded {
Expand Down
54 changes: 27 additions & 27 deletions src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::{u8, u16, u32, u64, usize};
use core::{f32, f64};
use core::mem::size_of;
use core::num::Wrapping;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
use core::{i128, u128};

use float::FloatCore;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub trait ToPrimitive {
/// The default implementation converts through `to_i64()`. Types implementing
/// this trait should override this method if they can represent a greater range.
#[inline]
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_i128(&self) -> Option<i128> {
self.to_i64().map(From::from)
}
Expand Down Expand Up @@ -84,7 +84,7 @@ pub trait ToPrimitive {
/// The default implementation converts through `to_u64()`. Types implementing
/// this trait should override this method if they can represent a greater range.
#[inline]
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_u128(&self) -> Option<u128> {
self.to_u64().map(From::from)
}
Expand Down Expand Up @@ -142,7 +142,7 @@ macro_rules! impl_to_primitive_int {
fn to_i16 -> i16;
fn to_i32 -> i32;
fn to_i64 -> i64;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_i128 -> i128;
}

Expand All @@ -152,7 +152,7 @@ macro_rules! impl_to_primitive_int {
fn to_u16 -> u16;
fn to_u32 -> u32;
fn to_u64 -> u64;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_u128 -> u128;
}

Expand All @@ -169,7 +169,7 @@ impl_to_primitive_int!(i8);
impl_to_primitive_int!(i16);
impl_to_primitive_int!(i32);
impl_to_primitive_int!(i64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_to_primitive_int!(i128);

macro_rules! impl_to_primitive_uint_to_int {
Expand Down Expand Up @@ -211,7 +211,7 @@ macro_rules! impl_to_primitive_uint {
fn to_i16 -> i16;
fn to_i32 -> i32;
fn to_i64 -> i64;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_i128 -> i128;
}

Expand All @@ -221,7 +221,7 @@ macro_rules! impl_to_primitive_uint {
fn to_u16 -> u16;
fn to_u32 -> u32;
fn to_u64 -> u64;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_u128 -> u128;
}

Expand All @@ -238,7 +238,7 @@ impl_to_primitive_uint!(u8);
impl_to_primitive_uint!(u16);
impl_to_primitive_uint!(u32);
impl_to_primitive_uint!(u64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_to_primitive_uint!(u128);

macro_rules! impl_to_primitive_float_to_float {
Expand Down Expand Up @@ -324,7 +324,7 @@ macro_rules! impl_to_primitive_float {
fn to_i16 -> i16;
fn to_i32 -> i32;
fn to_i64 -> i64;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_i128 -> i128;
}

Expand All @@ -334,7 +334,7 @@ macro_rules! impl_to_primitive_float {
fn to_u16 -> u16;
fn to_u32 -> u32;
fn to_u64 -> u64;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_u128 -> u128;
}

Expand Down Expand Up @@ -391,7 +391,7 @@ pub trait FromPrimitive: Sized {
/// The default implementation converts through `from_i64()`. Types implementing
/// this trait should override this method if they can represent a greater range.
#[inline]
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn from_i128(n: i128) -> Option<Self> {
n.to_i64().and_then(FromPrimitive::from_i64)
}
Expand Down Expand Up @@ -436,7 +436,7 @@ pub trait FromPrimitive: Sized {
/// The default implementation converts through `from_u64()`. Types implementing
/// this trait should override this method if they can represent a greater range.
#[inline]
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn from_u128(n: u128) -> Option<Self> {
n.to_u64().and_then(FromPrimitive::from_u64)
}
Expand Down Expand Up @@ -464,14 +464,14 @@ macro_rules! impl_from_primitive {
#[inline] fn from_i16(n: i16) -> Option<$T> { n.$to_ty() }
#[inline] fn from_i32(n: i32) -> Option<$T> { n.$to_ty() }
#[inline] fn from_i64(n: i64) -> Option<$T> { n.$to_ty() }
#[cfg(feature = "i128")]
#[cfg(has_i128)]
#[inline] fn from_i128(n: i128) -> Option<$T> { n.$to_ty() }

#[inline] fn from_u8(n: u8) -> Option<$T> { n.$to_ty() }
#[inline] fn from_u16(n: u16) -> Option<$T> { n.$to_ty() }
#[inline] fn from_u32(n: u32) -> Option<$T> { n.$to_ty() }
#[inline] fn from_u64(n: u64) -> Option<$T> { n.$to_ty() }
#[cfg(feature = "i128")]
#[cfg(has_i128)]
#[inline] fn from_u128(n: u128) -> Option<$T> { n.$to_ty() }

#[inline] fn from_f32(n: f32) -> Option<$T> { n.$to_ty() }
Expand All @@ -485,14 +485,14 @@ impl_from_primitive!(i8, to_i8);
impl_from_primitive!(i16, to_i16);
impl_from_primitive!(i32, to_i32);
impl_from_primitive!(i64, to_i64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_from_primitive!(i128, to_i128);
impl_from_primitive!(usize, to_usize);
impl_from_primitive!(u8, to_u8);
impl_from_primitive!(u16, to_u16);
impl_from_primitive!(u32, to_u32);
impl_from_primitive!(u64, to_u64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_from_primitive!(u128, to_u128);
impl_from_primitive!(f32, to_f32);
impl_from_primitive!(f64, to_f64);
Expand All @@ -515,15 +515,15 @@ impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
fn to_i16 -> i16;
fn to_i32 -> i32;
fn to_i64 -> i64;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_i128 -> i128;

fn to_usize -> usize;
fn to_u8 -> u8;
fn to_u16 -> u16;
fn to_u32 -> u32;
fn to_u64 -> u64;
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn to_u128 -> u128;

fn to_f32 -> f32;
Expand All @@ -548,15 +548,15 @@ impl<T: FromPrimitive> FromPrimitive for Wrapping<T> {
fn from_i16(i16);
fn from_i32(i32);
fn from_i64(i64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn from_i128(i128);

fn from_usize(usize);
fn from_u8(u8);
fn from_u16(u16);
fn from_u32(u32);
fn from_u64(u64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
fn from_u128(u128);

fn from_f32(f32);
Expand Down Expand Up @@ -605,14 +605,14 @@ impl_num_cast!(u8, to_u8);
impl_num_cast!(u16, to_u16);
impl_num_cast!(u32, to_u32);
impl_num_cast!(u64, to_u64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_num_cast!(u128, to_u128);
impl_num_cast!(usize, to_usize);
impl_num_cast!(i8, to_i8);
impl_num_cast!(i16, to_i16);
impl_num_cast!(i32, to_i32);
impl_num_cast!(i64, to_i64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_num_cast!(i128, to_i128);
impl_num_cast!(isize, to_isize);
impl_num_cast!(f32, to_f32);
Expand Down Expand Up @@ -680,9 +680,9 @@ macro_rules! impl_as_primitive {
($T: ty => { $( $U: ty ),* } ) => {
impl_as_primitive!(@ $T => { $( $U ),* });
impl_as_primitive!(@ $T => { u8, u16, u32, u64, usize });
impl_as_primitive!(@ $T => #[cfg(feature = "i128")] impl u128);
impl_as_primitive!(@ $T => #[cfg(has_i128)] impl u128);
impl_as_primitive!(@ $T => { i8, i16, i32, i64, isize });
impl_as_primitive!(@ $T => #[cfg(feature = "i128")] impl i128);
impl_as_primitive!(@ $T => #[cfg(has_i128)] impl i128);
};
}

Expand All @@ -694,9 +694,9 @@ impl_as_primitive!(u32 => { f32, f64 });
impl_as_primitive!(i32 => { f32, f64 });
impl_as_primitive!(u64 => { f32, f64 });
impl_as_primitive!(i64 => { f32, f64 });
#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_as_primitive!(u128 => { f32, f64 });
#[cfg(feature = "i128")]
#[cfg(has_i128)]
impl_as_primitive!(i128 => { f32, f64 });
impl_as_primitive!(usize => { f32, f64 });
impl_as_primitive!(isize => { f32, f64 });
Expand Down
8 changes: 4 additions & 4 deletions src/identities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ zero_impl!(u8, 0);
zero_impl!(u16, 0);
zero_impl!(u32, 0);
zero_impl!(u64, 0);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
zero_impl!(u128, 0);

zero_impl!(isize, 0);
zero_impl!(i8, 0);
zero_impl!(i16, 0);
zero_impl!(i32, 0);
zero_impl!(i64, 0);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
zero_impl!(i128, 0);

zero_impl!(f32, 0.0);
Expand Down Expand Up @@ -109,15 +109,15 @@ one_impl!(u8, 1);
one_impl!(u16, 1);
one_impl!(u32, 1);
one_impl!(u64, 1);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
one_impl!(u128, 1);

one_impl!(isize, 1);
one_impl!(i8, 1);
one_impl!(i16, 1);
one_impl!(i32, 1);
one_impl!(i64, 1);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
one_impl!(i128, 1);

one_impl!(f32, 1.0);
Expand Down
4 changes: 2 additions & 2 deletions src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,13 @@ prim_int_impl!(u8, i8, u8);
prim_int_impl!(u16, i16, u16);
prim_int_impl!(u32, i32, u32);
prim_int_impl!(u64, i64, u64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
prim_int_impl!(u128, i128, u128);
prim_int_impl!(usize, isize, usize);
prim_int_impl!(i8, i8, u8);
prim_int_impl!(i16, i16, u16);
prim_int_impl!(i32, i32, u32);
prim_int_impl!(i64, i64, u64);
#[cfg(feature = "i128")]
#[cfg(has_i128)]
prim_int_impl!(i128, i128, u128);
prim_int_impl!(isize, isize, usize);
Loading