Skip to content

Commit d41f046

Browse files
authored
Rollup merge of #142098 - GuillaumeGomez:int_format_into, r=Amanieu
Implement `int_format_into` feature I took over #138338 with `@madhav-madhusoodanan's` approval. Since #136264, a lot of changes happened so I made use of them to reduce the number of changes. ACP approval: rust-lang/libs-team#546 (comment) ## Associated Issue - #138215 r? `@hanna-kruppe`
2 parents a2d45f7 + 9943c19 commit d41f046

File tree

6 files changed

+325
-82
lines changed

6 files changed

+325
-82
lines changed

library/alloc/src/string.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,7 +2875,8 @@ macro_rules! impl_to_string {
28752875
out = String::with_capacity(SIZE);
28762876
}
28772877

2878-
out.push_str(self.unsigned_abs()._fmt(&mut buf));
2878+
// SAFETY: `buf` is always big enough to contain all the digits.
2879+
unsafe { out.push_str(self.unsigned_abs()._fmt(&mut buf)); }
28792880
out
28802881
}
28812882
}
@@ -2887,7 +2888,8 @@ macro_rules! impl_to_string {
28872888
const SIZE: usize = $unsigned::MAX.ilog10() as usize + 1;
28882889
let mut buf = [core::mem::MaybeUninit::<u8>::uninit(); SIZE];
28892890

2890-
self._fmt(&mut buf).to_string()
2891+
// SAFETY: `buf` is always big enough to contain all the digits.
2892+
unsafe { self._fmt(&mut buf).to_string() }
28912893
}
28922894
}
28932895
)*

library/alloctests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(downcast_unchecked)]
1010
#![feature(exact_size_is_empty)]
1111
#![feature(hashmap_internals)]
12+
#![feature(int_format_into)]
1213
#![feature(linked_list_cursors)]
1314
#![feature(map_try_insert)]
1415
#![feature(pattern)]

library/alloctests/tests/num.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
use std::fmt::{Debug, Display};
1+
use core::fmt::NumBuffer;
22
use std::str::FromStr;
33

4-
fn assert_nb<Int: ToString + FromStr + Debug + Display + Eq>(value: Int) {
5-
let s = value.to_string();
6-
let s2 = format!("s: {}.", value);
4+
macro_rules! assert_nb {
5+
($int:ident, $value:expr) => {
6+
let value: $int = $value;
7+
let s = value.to_string();
8+
let s2 = format!("s: {}.", value);
79

8-
assert_eq!(format!("s: {s}."), s2);
9-
let Ok(ret) = Int::from_str(&s) else {
10-
panic!("failed to convert into to string");
10+
assert_eq!(format!("s: {s}."), s2);
11+
let Ok(ret) = $int::from_str(&s) else {
12+
panic!("failed to convert into to string");
13+
};
14+
assert_eq!(ret, value);
15+
16+
let mut buffer = NumBuffer::<$int>::new();
17+
assert_eq!(value.format_into(&mut buffer), s.as_str());
1118
};
12-
assert_eq!(ret, value);
1319
}
1420

1521
macro_rules! uint_to_s {
1622
($($fn_name:ident, $int:ident,)+) => {
1723
$(
1824
#[test]
1925
fn $fn_name() {
20-
assert_nb::<$int>($int::MIN);
21-
assert_nb::<$int>($int::MAX);
22-
assert_nb::<$int>(1);
23-
assert_nb::<$int>($int::MIN / 2);
24-
assert_nb::<$int>($int::MAX / 2);
26+
assert_nb!($int, $int::MIN);
27+
assert_nb!($int, $int::MAX);
28+
assert_nb!($int, 1);
29+
assert_nb!($int, $int::MIN / 2);
30+
assert_nb!($int, $int::MAX / 2);
2531
}
2632
)+
2733
}
@@ -31,13 +37,13 @@ macro_rules! int_to_s {
3137
$(
3238
#[test]
3339
fn $fn_name() {
34-
assert_nb::<$int>($int::MIN);
35-
assert_nb::<$int>($int::MAX);
36-
assert_nb::<$int>(1);
37-
assert_nb::<$int>(0);
38-
assert_nb::<$int>(-1);
39-
assert_nb::<$int>($int::MIN / 2);
40-
assert_nb::<$int>($int::MAX / 2);
40+
assert_nb!($int, $int::MIN);
41+
assert_nb!($int, $int::MAX);
42+
assert_nb!($int, 1);
43+
assert_nb!($int, 0);
44+
assert_nb!($int, -1);
45+
assert_nb!($int, $int::MIN / 2);
46+
assert_nb!($int, $int::MAX / 2);
4147
}
4248
)+
4349
}

library/core/src/fmt/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod float;
1515
#[cfg(no_fp_fmt_parse)]
1616
mod nofloat;
1717
mod num;
18+
mod num_buffer;
1819
mod rt;
1920

2021
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
@@ -33,6 +34,9 @@ pub enum Alignment {
3334
Center,
3435
}
3536

37+
#[unstable(feature = "int_format_into", issue = "138215")]
38+
pub use num_buffer::{NumBuffer, NumBufferTrait};
39+
3640
#[stable(feature = "debug_builders", since = "1.2.0")]
3741
pub use self::builders::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple};
3842
#[unstable(feature = "debug_closure_helpers", issue = "117729")]

0 commit comments

Comments
 (0)