Skip to content

Commit f412562

Browse files
bors[bot]cuviper
andcommitted
Merge #69
69: Automatically detect support for i128/u128 r=cuviper a=cuviper Co-authored-by: Josh Stone <[email protected]>
2 parents 4e136dd + c00ae20 commit f412562

File tree

16 files changed

+130
-82
lines changed

16 files changed

+130
-82
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ categories = ["algorithms", "science", "no-std"]
88
license = "MIT/Apache-2.0"
99
repository = "https://github.com/rust-num/num-traits"
1010
name = "num-traits"
11-
version = "0.2.3"
11+
version = "0.2.4"
1212
readme = "README.md"
13+
build = "build.rs"
1314

1415
[package.metadata.docs.rs]
15-
all-features = true
16+
features = ["std"]
1617

1718
[dependencies]
1819

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ The `Float` and `Real` traits are only available when `std` is enabled. The
3838
and `f64` also require `std`, as do implementations of signed and floating-
3939
point exponents in `Pow`.
4040

41-
Implementations for `i128` and `u128` are only available when `i128` is enabled.
41+
Implementations for `i128` and `u128` are only available with Rust 1.26 and
42+
later. The build script automatically detects this, but you can make it
43+
mandatory by enabling the `i128` crate feature.
4244

4345
## Releases
4446

RELEASES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# Release 0.2.4
2+
3+
- [Support for 128-bit integers is now automatically detected and enabled.][69]
4+
Setting the `i128` crate feature now causes the build script to panic if such
5+
support is not detected.
6+
7+
**Contributors**: @cuviper
8+
9+
[69]: https://github.com/rust-num/num-traits/pull/69
10+
111
# Release 0.2.3
212

313
- [The new `CheckedNeg` and `CheckedRem` traits][63] perform checked `Neg` and

build.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::env;
2+
use std::io::Write;
3+
use std::process::{Command, Stdio};
4+
5+
fn main() {
6+
if probe("fn main() { 0i128; }") {
7+
println!("cargo:rustc-cfg=has_i128");
8+
} else if env::var_os("CARGO_FEATURE_I128").is_some() {
9+
panic!("i128 support was not detected!");
10+
}
11+
}
12+
13+
/// Test if a code snippet can be compiled
14+
fn probe(code: &str) -> bool {
15+
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
16+
let out_dir = env::var_os("OUT_DIR").expect("environment variable OUT_DIR");
17+
18+
let mut child = Command::new(rustc)
19+
.arg("--out-dir")
20+
.arg(out_dir)
21+
.arg("--emit=obj")
22+
.arg("-")
23+
.stdin(Stdio::piped())
24+
.spawn()
25+
.expect("rustc probe");
26+
27+
child
28+
.stdin
29+
.as_mut()
30+
.expect("rustc stdin")
31+
.write_all(code.as_bytes())
32+
.expect("write rustc stdin");
33+
34+
child.wait().expect("rustc probe").success()
35+
}

src/bounds.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::{usize, u8, u16, u32, u64};
22
use core::{isize, i8, i16, i32, i64};
33
use core::{f32, f64};
44
use core::num::Wrapping;
5-
#[cfg(feature = "i128")]
5+
#[cfg(has_i128)]
66
use core::{i128, u128};
77

88
/// Numbers which have upper and lower bounds
@@ -31,15 +31,15 @@ bounded_impl!(u8, u8::MIN, u8::MAX);
3131
bounded_impl!(u16, u16::MIN, u16::MAX);
3232
bounded_impl!(u32, u32::MIN, u32::MAX);
3333
bounded_impl!(u64, u64::MIN, u64::MAX);
34-
#[cfg(feature = "i128")]
34+
#[cfg(has_i128)]
3535
bounded_impl!(u128, u128::MIN, u128::MAX);
3636

3737
bounded_impl!(isize, isize::MIN, isize::MAX);
3838
bounded_impl!(i8, i8::MIN, i8::MAX);
3939
bounded_impl!(i16, i16::MIN, i16::MAX);
4040
bounded_impl!(i32, i32::MIN, i32::MAX);
4141
bounded_impl!(i64, i64::MIN, i64::MAX);
42-
#[cfg(feature = "i128")]
42+
#[cfg(has_i128)]
4343
bounded_impl!(i128, i128::MIN, i128::MAX);
4444

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

100-
#[cfg(feature = "i128")]
100+
#[cfg(has_i128)]
101101
#[test]
102102
fn wrapping_bounded_i128() {
103103
macro_rules! test_wrapping_bounded {

src/cast.rs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::{u8, u16, u32, u64, usize};
33
use core::{f32, f64};
44
use core::mem::size_of;
55
use core::num::Wrapping;
6-
#[cfg(feature = "i128")]
6+
#[cfg(has_i128)]
77
use core::{i128, u128};
88

99
use float::FloatCore;
@@ -44,7 +44,7 @@ pub trait ToPrimitive {
4444
/// The default implementation converts through `to_i64()`. Types implementing
4545
/// this trait should override this method if they can represent a greater range.
4646
#[inline]
47-
#[cfg(feature = "i128")]
47+
#[cfg(has_i128)]
4848
fn to_i128(&self) -> Option<i128> {
4949
self.to_i64().map(From::from)
5050
}
@@ -84,7 +84,7 @@ pub trait ToPrimitive {
8484
/// The default implementation converts through `to_u64()`. Types implementing
8585
/// this trait should override this method if they can represent a greater range.
8686
#[inline]
87-
#[cfg(feature = "i128")]
87+
#[cfg(has_i128)]
8888
fn to_u128(&self) -> Option<u128> {
8989
self.to_u64().map(From::from)
9090
}
@@ -142,7 +142,7 @@ macro_rules! impl_to_primitive_int {
142142
fn to_i16 -> i16;
143143
fn to_i32 -> i32;
144144
fn to_i64 -> i64;
145-
#[cfg(feature = "i128")]
145+
#[cfg(has_i128)]
146146
fn to_i128 -> i128;
147147
}
148148

@@ -152,7 +152,7 @@ macro_rules! impl_to_primitive_int {
152152
fn to_u16 -> u16;
153153
fn to_u32 -> u32;
154154
fn to_u64 -> u64;
155-
#[cfg(feature = "i128")]
155+
#[cfg(has_i128)]
156156
fn to_u128 -> u128;
157157
}
158158

@@ -169,7 +169,7 @@ impl_to_primitive_int!(i8);
169169
impl_to_primitive_int!(i16);
170170
impl_to_primitive_int!(i32);
171171
impl_to_primitive_int!(i64);
172-
#[cfg(feature = "i128")]
172+
#[cfg(has_i128)]
173173
impl_to_primitive_int!(i128);
174174

175175
macro_rules! impl_to_primitive_uint_to_int {
@@ -211,7 +211,7 @@ macro_rules! impl_to_primitive_uint {
211211
fn to_i16 -> i16;
212212
fn to_i32 -> i32;
213213
fn to_i64 -> i64;
214-
#[cfg(feature = "i128")]
214+
#[cfg(has_i128)]
215215
fn to_i128 -> i128;
216216
}
217217

@@ -221,7 +221,7 @@ macro_rules! impl_to_primitive_uint {
221221
fn to_u16 -> u16;
222222
fn to_u32 -> u32;
223223
fn to_u64 -> u64;
224-
#[cfg(feature = "i128")]
224+
#[cfg(has_i128)]
225225
fn to_u128 -> u128;
226226
}
227227

@@ -238,7 +238,7 @@ impl_to_primitive_uint!(u8);
238238
impl_to_primitive_uint!(u16);
239239
impl_to_primitive_uint!(u32);
240240
impl_to_primitive_uint!(u64);
241-
#[cfg(feature = "i128")]
241+
#[cfg(has_i128)]
242242
impl_to_primitive_uint!(u128);
243243

244244
macro_rules! impl_to_primitive_float_to_float {
@@ -324,7 +324,7 @@ macro_rules! impl_to_primitive_float {
324324
fn to_i16 -> i16;
325325
fn to_i32 -> i32;
326326
fn to_i64 -> i64;
327-
#[cfg(feature = "i128")]
327+
#[cfg(has_i128)]
328328
fn to_i128 -> i128;
329329
}
330330

@@ -334,7 +334,7 @@ macro_rules! impl_to_primitive_float {
334334
fn to_u16 -> u16;
335335
fn to_u32 -> u32;
336336
fn to_u64 -> u64;
337-
#[cfg(feature = "i128")]
337+
#[cfg(has_i128)]
338338
fn to_u128 -> u128;
339339
}
340340

@@ -391,7 +391,7 @@ pub trait FromPrimitive: Sized {
391391
/// The default implementation converts through `from_i64()`. Types implementing
392392
/// this trait should override this method if they can represent a greater range.
393393
#[inline]
394-
#[cfg(feature = "i128")]
394+
#[cfg(has_i128)]
395395
fn from_i128(n: i128) -> Option<Self> {
396396
n.to_i64().and_then(FromPrimitive::from_i64)
397397
}
@@ -436,7 +436,7 @@ pub trait FromPrimitive: Sized {
436436
/// The default implementation converts through `from_u64()`. Types implementing
437437
/// this trait should override this method if they can represent a greater range.
438438
#[inline]
439-
#[cfg(feature = "i128")]
439+
#[cfg(has_i128)]
440440
fn from_u128(n: u128) -> Option<Self> {
441441
n.to_u64().and_then(FromPrimitive::from_u64)
442442
}
@@ -464,14 +464,14 @@ macro_rules! impl_from_primitive {
464464
#[inline] fn from_i16(n: i16) -> Option<$T> { n.$to_ty() }
465465
#[inline] fn from_i32(n: i32) -> Option<$T> { n.$to_ty() }
466466
#[inline] fn from_i64(n: i64) -> Option<$T> { n.$to_ty() }
467-
#[cfg(feature = "i128")]
467+
#[cfg(has_i128)]
468468
#[inline] fn from_i128(n: i128) -> Option<$T> { n.$to_ty() }
469469

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

477477
#[inline] fn from_f32(n: f32) -> Option<$T> { n.$to_ty() }
@@ -485,14 +485,14 @@ impl_from_primitive!(i8, to_i8);
485485
impl_from_primitive!(i16, to_i16);
486486
impl_from_primitive!(i32, to_i32);
487487
impl_from_primitive!(i64, to_i64);
488-
#[cfg(feature = "i128")]
488+
#[cfg(has_i128)]
489489
impl_from_primitive!(i128, to_i128);
490490
impl_from_primitive!(usize, to_usize);
491491
impl_from_primitive!(u8, to_u8);
492492
impl_from_primitive!(u16, to_u16);
493493
impl_from_primitive!(u32, to_u32);
494494
impl_from_primitive!(u64, to_u64);
495-
#[cfg(feature = "i128")]
495+
#[cfg(has_i128)]
496496
impl_from_primitive!(u128, to_u128);
497497
impl_from_primitive!(f32, to_f32);
498498
impl_from_primitive!(f64, to_f64);
@@ -515,15 +515,15 @@ impl<T: ToPrimitive> ToPrimitive for Wrapping<T> {
515515
fn to_i16 -> i16;
516516
fn to_i32 -> i32;
517517
fn to_i64 -> i64;
518-
#[cfg(feature = "i128")]
518+
#[cfg(has_i128)]
519519
fn to_i128 -> i128;
520520

521521
fn to_usize -> usize;
522522
fn to_u8 -> u8;
523523
fn to_u16 -> u16;
524524
fn to_u32 -> u32;
525525
fn to_u64 -> u64;
526-
#[cfg(feature = "i128")]
526+
#[cfg(has_i128)]
527527
fn to_u128 -> u128;
528528

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

554554
fn from_usize(usize);
555555
fn from_u8(u8);
556556
fn from_u16(u16);
557557
fn from_u32(u32);
558558
fn from_u64(u64);
559-
#[cfg(feature = "i128")]
559+
#[cfg(has_i128)]
560560
fn from_u128(u128);
561561

562562
fn from_f32(f32);
@@ -605,14 +605,14 @@ impl_num_cast!(u8, to_u8);
605605
impl_num_cast!(u16, to_u16);
606606
impl_num_cast!(u32, to_u32);
607607
impl_num_cast!(u64, to_u64);
608-
#[cfg(feature = "i128")]
608+
#[cfg(has_i128)]
609609
impl_num_cast!(u128, to_u128);
610610
impl_num_cast!(usize, to_usize);
611611
impl_num_cast!(i8, to_i8);
612612
impl_num_cast!(i16, to_i16);
613613
impl_num_cast!(i32, to_i32);
614614
impl_num_cast!(i64, to_i64);
615-
#[cfg(feature = "i128")]
615+
#[cfg(has_i128)]
616616
impl_num_cast!(i128, to_i128);
617617
impl_num_cast!(isize, to_isize);
618618
impl_num_cast!(f32, to_f32);
@@ -680,9 +680,9 @@ macro_rules! impl_as_primitive {
680680
($T: ty => { $( $U: ty ),* } ) => {
681681
impl_as_primitive!(@ $T => { $( $U ),* });
682682
impl_as_primitive!(@ $T => { u8, u16, u32, u64, usize });
683-
impl_as_primitive!(@ $T => #[cfg(feature = "i128")] impl u128);
683+
impl_as_primitive!(@ $T => #[cfg(has_i128)] impl u128);
684684
impl_as_primitive!(@ $T => { i8, i16, i32, i64, isize });
685-
impl_as_primitive!(@ $T => #[cfg(feature = "i128")] impl i128);
685+
impl_as_primitive!(@ $T => #[cfg(has_i128)] impl i128);
686686
};
687687
}
688688

@@ -694,9 +694,9 @@ impl_as_primitive!(u32 => { f32, f64 });
694694
impl_as_primitive!(i32 => { f32, f64 });
695695
impl_as_primitive!(u64 => { f32, f64 });
696696
impl_as_primitive!(i64 => { f32, f64 });
697-
#[cfg(feature = "i128")]
697+
#[cfg(has_i128)]
698698
impl_as_primitive!(u128 => { f32, f64 });
699-
#[cfg(feature = "i128")]
699+
#[cfg(has_i128)]
700700
impl_as_primitive!(i128 => { f32, f64 });
701701
impl_as_primitive!(usize => { f32, f64 });
702702
impl_as_primitive!(isize => { f32, f64 });

src/identities.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ zero_impl!(u8, 0);
4141
zero_impl!(u16, 0);
4242
zero_impl!(u32, 0);
4343
zero_impl!(u64, 0);
44-
#[cfg(feature = "i128")]
44+
#[cfg(has_i128)]
4545
zero_impl!(u128, 0);
4646

4747
zero_impl!(isize, 0);
4848
zero_impl!(i8, 0);
4949
zero_impl!(i16, 0);
5050
zero_impl!(i32, 0);
5151
zero_impl!(i64, 0);
52-
#[cfg(feature = "i128")]
52+
#[cfg(has_i128)]
5353
zero_impl!(i128, 0);
5454

5555
zero_impl!(f32, 0.0);
@@ -109,15 +109,15 @@ one_impl!(u8, 1);
109109
one_impl!(u16, 1);
110110
one_impl!(u32, 1);
111111
one_impl!(u64, 1);
112-
#[cfg(feature = "i128")]
112+
#[cfg(has_i128)]
113113
one_impl!(u128, 1);
114114

115115
one_impl!(isize, 1);
116116
one_impl!(i8, 1);
117117
one_impl!(i16, 1);
118118
one_impl!(i32, 1);
119119
one_impl!(i64, 1);
120-
#[cfg(feature = "i128")]
120+
#[cfg(has_i128)]
121121
one_impl!(i128, 1);
122122

123123
one_impl!(f32, 1.0);

src/int.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,13 @@ prim_int_impl!(u8, i8, u8);
368368
prim_int_impl!(u16, i16, u16);
369369
prim_int_impl!(u32, i32, u32);
370370
prim_int_impl!(u64, i64, u64);
371-
#[cfg(feature = "i128")]
371+
#[cfg(has_i128)]
372372
prim_int_impl!(u128, i128, u128);
373373
prim_int_impl!(usize, isize, usize);
374374
prim_int_impl!(i8, i8, u8);
375375
prim_int_impl!(i16, i16, u16);
376376
prim_int_impl!(i32, i32, u32);
377377
prim_int_impl!(i64, i64, u64);
378-
#[cfg(feature = "i128")]
378+
#[cfg(has_i128)]
379379
prim_int_impl!(i128, i128, u128);
380380
prim_int_impl!(isize, isize, usize);

0 commit comments

Comments
 (0)