Skip to main content

core/fmt/
num_buffer.rs

1use crate::mem::MaybeUninit;
2
3/// Trait used to describe the maximum number of digits in decimal base of the implemented integer.
4#[unstable(feature = "int_format_into", issue = "138215")]
5pub trait NumBufferTrait {
6    /// Maximum number of digits in decimal base of the implemented integer.
7    const BUF_SIZE: usize;
8}
9
10macro_rules! impl_NumBufferTrait {
11    ($($signed:ident, $unsigned:ident,)*) => {
12        $(
13            #[unstable(feature = "int_format_into", issue = "138215")]
14            impl NumBufferTrait for $signed {
15                // `+ 2` and not `+ 1` to include the `-` character.
16                const BUF_SIZE: usize = $signed::MAX.ilog(10) as usize + 2;
17            }
18            #[unstable(feature = "int_format_into", issue = "138215")]
19            impl NumBufferTrait for $unsigned {
20                const BUF_SIZE: usize = $unsigned::MAX.ilog(10) as usize + 1;
21            }
22        )*
23    }
24}
25
26impl_NumBufferTrait! {
27    i8, u8,
28    i16, u16,
29    i32, u32,
30    i64, u64,
31    isize, usize,
32    i128, u128,
33}
34
35/// A buffer wrapper of which the internal size is based on the maximum
36/// number of digits the associated integer can have.
37///
38/// # Examples
39///
40/// ```
41/// #![feature(int_format_into)]
42/// use core::fmt::NumBuffer;
43///
44/// let mut buf = NumBuffer::new();
45/// let n1 = 1972u32;
46/// assert_eq!(n1.format_into(&mut buf), "1972");
47///
48/// // Formatting a negative integer includes the sign.
49/// let mut buf = NumBuffer::new();
50/// let n2 = -1972i32;
51/// assert_eq!(n2.format_into(&mut buf), "-1972");
52/// ```
53#[ferrocene::prevalidated]
54#[unstable(feature = "int_format_into", issue = "138215")]
55pub struct NumBuffer<T: NumBufferTrait> {
56    // FIXME: Once const generics feature is working, use `T::BUF_SIZE` instead of 40.
57    pub(crate) buf: [MaybeUninit<u8>; 40],
58    // FIXME: Remove this field once we can actually use `T`.
59    phantom: core::marker::PhantomData<T>,
60}
61
62#[unstable(feature = "int_format_into", issue = "138215")]
63impl<T: NumBufferTrait> core::fmt::Debug for NumBuffer<T> {
64    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
65        f.debug_struct("NumBuffer").finish()
66    }
67}
68
69#[unstable(feature = "int_format_into", issue = "138215")]
70impl<T: NumBufferTrait> NumBuffer<T> {
71    /// Initializes internal buffer.
72    #[unstable(feature = "int_format_into", issue = "138215")]
73    #[ferrocene::prevalidated]
74    pub const fn new() -> Self {
75        // FIXME: Once const generics feature is working, use `T::BUF_SIZE` instead of 40.
76        NumBuffer { buf: [MaybeUninit::<u8>::uninit(); 40], phantom: core::marker::PhantomData }
77    }
78
79    /// Returns the length of the internal buffer.
80    #[unstable(feature = "int_format_into", issue = "138215")]
81    #[ferrocene::prevalidated]
82    pub const fn capacity(&self) -> usize {
83        self.buf.len()
84    }
85}