Skip to content

Commit c16138b

Browse files
committed
Add no_iu128_fmt config to disable 128 bin integer formatting.
1 parent aa4cfd0 commit c16138b

File tree

6 files changed

+29
-9
lines changed

6 files changed

+29
-9
lines changed

library/core/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ level = "warn"
2929
check-cfg = [
3030
'cfg(bootstrap)',
3131
'cfg(no_fp_fmt_parse)',
32+
'cfg(no_iu128_fmt)',
3233
'cfg(stdarch_intel_sde)',
3334
'cfg(target_arch, values("xtensa"))',
3435
# core use #[path] imports to portable-simd `core_simd` crate

library/core/src/fmt/num.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ mod imp {
546546
impl_Exp!(i8, u8, i16, u16, i32, u32, isize, usize as u32 via to_u32 named exp_u32);
547547
impl_Exp!(i64, u64 as u64 via to_u64 named exp_u64);
548548
}
549+
#[cfg(not(no_iu128_fmt))]
549550
impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
550551

551552
/// Helper function for writing a u64 into `buf` going from last to first, with `curr`.
@@ -638,21 +639,29 @@ fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], cu
638639
#[stable(feature = "rust1", since = "1.0.0")]
639640
impl fmt::Display for u128 {
640641
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
641-
fmt_u128(*self, true, f)
642+
if cfg!(no_iu128_fmt) {
643+
panic!("u128 formatting support is turned off")
644+
} else {
645+
fmt_u128(*self, true, f)
646+
}
642647
}
643648
}
644649

645650
#[stable(feature = "rust1", since = "1.0.0")]
646651
impl fmt::Display for i128 {
647652
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
648-
let is_nonnegative = *self >= 0;
649-
let n = if is_nonnegative {
650-
self.to_u128()
653+
if cfg!(no_iu128_fmt) {
654+
panic!("u128 formatting support is turned off")
651655
} else {
652-
// convert the negative num to positive by summing 1 to its 2s complement
653-
(!self.to_u128()).wrapping_add(1)
654-
};
655-
fmt_u128(n, is_nonnegative, f)
656+
let is_nonnegative = *self >= 0;
657+
let n = if is_nonnegative {
658+
self.to_u128()
659+
} else {
660+
// convert the negative num to positive by summing 1 to its 2s complement
661+
(!self.to_u128()).wrapping_add(1)
662+
};
663+
fmt_u128(n, is_nonnegative, f)
664+
}
656665
}
657666
}
658667

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#![doc(cfg_hide(
6767
not(test),
6868
no_fp_fmt_parse,
69+
no_iu128_fmt,
6970
target_pointer_width = "16",
7071
target_pointer_width = "32",
7172
target_pointer_width = "64",

library/coretests/Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ test = true
2525
[dev-dependencies]
2626
rand = { version = "0.8.5", default-features = false }
2727
rand_xorshift = { version = "0.3.0", default-features = false }
28+
29+
30+
[lints.rust.unexpected_cfgs]
31+
level = "warn"
32+
check-cfg = ['cfg(no_iu128_fmt)']

library/coretests/benches/fmt.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ fn write_str_macro_debug_ascii(bh: &mut Bencher) {
122122
}
123123

124124
#[bench]
125+
#[cfg(not(no_iu128_fmt))]
125126
fn write_u128_max(bh: &mut Bencher) {
126127
bh.iter(|| {
127128
test::black_box(format!("{}", u128::MAX));

library/coretests/tests/num/int_sqrt.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -244,5 +244,8 @@ macro_rules! unsigned_check {
244244
};
245245
}
246246

247-
tests!(signed_check: i8 i16 i32 i64 i128);
247+
tests!(signed_check: i8 i16 i32 i64);
248+
#[cfg(not(no_iu128_fmt))]
249+
tests!(signed_check: i128);
250+
248251
tests!(unsigned_check: u8 u16 u32 u64 u128);

0 commit comments

Comments
 (0)