Skip to content

Commit 381fec2

Browse files
Make int_format_into API more flexible
1 parent 2783fc4 commit 381fec2

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

library/alloctests/tests/num.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ macro_rules! assert_nb {
1515

1616
let mut buffer = NumBuffer::<$int>::new();
1717
assert_eq!(value.format_into(&mut buffer), s.as_str());
18+
19+
// We check that bigger buffers can be used in `format_into`.
20+
let mut buffer = NumBuffer::<i128>::new();
21+
assert_eq!(value.format_into(&mut buffer), s.as_str());
1822
};
1923
}
2024

library/core/src/fmt/num.rs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Integer and floating-point number formatting
22
3-
use crate::fmt::NumBuffer;
3+
use crate::fmt::{NumBuffer, NumBufferTrait};
44
use crate::mem::MaybeUninit;
55
use crate::num::fmt as numfmt;
66
use crate::ops::{Div, Rem, Sub};
@@ -338,7 +338,7 @@ macro_rules! impl_Display {
338338
/// use core::fmt::NumBuffer;
339339
///
340340
#[doc = concat!("let n = 0", stringify!($signed), ";")]
341-
/// let mut buf = NumBuffer::new();
341+
#[doc = concat!("let mut buf = NumBuffer::<", stringify!($signed), ">::new();")]
342342
/// assert_eq!(n.format_into(&mut buf), "0");
343343
///
344344
#[doc = concat!("let n1 = 32", stringify!($signed), ";")]
@@ -347,8 +347,23 @@ macro_rules! impl_Display {
347347
#[doc = concat!("let n2 = ", stringify!($signed::MAX), ";")]
348348
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($signed::MAX), ".to_string());")]
349349
/// ```
350+
///
351+
/// You can also use a [`NumBuffer`] used to store a bigger integer:
352+
///
353+
/// ```
354+
/// #![feature(int_format_into)]
355+
/// use core::fmt::NumBuffer;
356+
///
357+
#[doc = concat!("let n = 32", stringify!($signed), ";")]
358+
/// // We use a `NumBuffer` used to store a bigger integer.
359+
/// let mut buf = NumBuffer::<u128>::new();
360+
/// assert_eq!(n.format_into(&mut buf), "32");
361+
/// ```
350362
#[unstable(feature = "int_format_into", issue = "138215")]
351-
pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
363+
pub fn format_into<T: NumBufferTrait>(self, buf: &mut NumBuffer<T>) -> &str {
364+
const {
365+
assert!(Self::BUF_SIZE <= T::BUF_SIZE, "buffer is not big enough");
366+
}
352367
let mut offset;
353368

354369
#[cfg(not(feature = "optimize_for_size"))]
@@ -381,7 +396,7 @@ macro_rules! impl_Display {
381396
/// use core::fmt::NumBuffer;
382397
///
383398
#[doc = concat!("let n = 0", stringify!($unsigned), ";")]
384-
/// let mut buf = NumBuffer::new();
399+
#[doc = concat!("let mut buf = NumBuffer::<", stringify!($unsigned), ">::new();")]
385400
/// assert_eq!(n.format_into(&mut buf), "0");
386401
///
387402
#[doc = concat!("let n1 = 32", stringify!($unsigned), ";")]
@@ -390,8 +405,23 @@ macro_rules! impl_Display {
390405
#[doc = concat!("let n2 = ", stringify!($unsigned::MAX), ";")]
391406
#[doc = concat!("assert_eq!(n2.format_into(&mut buf), ", stringify!($unsigned::MAX), ".to_string());")]
392407
/// ```
408+
///
409+
/// You can also use a [`NumBuffer`] used to store a bigger integer:
410+
///
411+
/// ```
412+
/// #![feature(int_format_into)]
413+
/// use core::fmt::NumBuffer;
414+
///
415+
#[doc = concat!("let n = 32", stringify!($unsigned), ";")]
416+
/// // We use a `NumBuffer` used to store `u128`.
417+
/// let mut buf = NumBuffer::<u128>::new();
418+
/// assert_eq!(n.format_into(&mut buf), "32");
419+
/// ```
393420
#[unstable(feature = "int_format_into", issue = "138215")]
394-
pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
421+
pub fn format_into<T: NumBufferTrait>(self, buf: &mut NumBuffer<T>) -> &str {
422+
const {
423+
assert!(Self::BUF_SIZE <= T::BUF_SIZE, "buffer is not big enough");
424+
}
395425
let offset;
396426

397427
#[cfg(not(feature = "optimize_for_size"))]
@@ -790,19 +820,20 @@ impl u128 {
790820
/// use core::fmt::NumBuffer;
791821
///
792822
/// let n = 0u128;
793-
/// let mut buf = NumBuffer::new();
823+
/// let mut buf = NumBuffer::<u128>::new();
794824
/// assert_eq!(n.format_into(&mut buf), "0");
795825
///
796826
/// let n1 = 32u128;
797-
/// let mut buf1 = NumBuffer::new();
798-
/// assert_eq!(n1.format_into(&mut buf1), "32");
827+
/// assert_eq!(n1.format_into(&mut buf), "32");
799828
///
800829
/// let n2 = u128::MAX;
801-
/// let mut buf2 = NumBuffer::new();
802-
/// assert_eq!(n2.format_into(&mut buf2), u128::MAX.to_string());
830+
/// assert_eq!(n2.format_into(&mut buf), u128::MAX.to_string());
803831
/// ```
804832
#[unstable(feature = "int_format_into", issue = "138215")]
805-
pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
833+
pub fn format_into<T: NumBufferTrait>(self, buf: &mut NumBuffer<T>) -> &str {
834+
const {
835+
assert!(Self::BUF_SIZE <= T::BUF_SIZE, "buffer is not big enough");
836+
}
806837
let diff = buf.capacity() - U128_MAX_DEC_N;
807838
// FIXME: Once const generics are better, use `NumberBufferTrait::BUF_SIZE` as generic const
808839
// for `fmt_u128_inner`.
@@ -825,7 +856,7 @@ impl i128 {
825856
/// use core::fmt::NumBuffer;
826857
///
827858
/// let n = 0i128;
828-
/// let mut buf = NumBuffer::new();
859+
/// let mut buf = NumBuffer::<i128>::new();
829860
/// assert_eq!(n.format_into(&mut buf), "0");
830861
///
831862
/// let n1 = i128::MIN;
@@ -835,7 +866,10 @@ impl i128 {
835866
/// assert_eq!(n2.format_into(&mut buf), i128::MAX.to_string());
836867
/// ```
837868
#[unstable(feature = "int_format_into", issue = "138215")]
838-
pub fn format_into(self, buf: &mut NumBuffer<Self>) -> &str {
869+
pub fn format_into<T: NumBufferTrait>(self, buf: &mut NumBuffer<T>) -> &str {
870+
const {
871+
assert!(Self::BUF_SIZE <= T::BUF_SIZE, "buffer is not big enough");
872+
}
839873
let diff = buf.capacity() - U128_MAX_DEC_N;
840874
// FIXME: Once const generics are better, use `NumberBufferTrait::BUF_SIZE` as generic const
841875
// for `fmt_u128_inner`.

0 commit comments

Comments
 (0)