1use crate::mem::MaybeUninit;
2
3#[unstable(feature = "fmt_internals", issue = "none")]
5pub trait NumBufferTrait {
6 #[unstable(feature = "fmt_internals", issue = "none")]
8 const DEFAULT: Self::Buf;
9 #[unstable(feature = "fmt_internals", issue = "none")]
11 type Buf: AsRef<[MaybeUninit<u8>]> + AsMut<[MaybeUninit<u8>]>;
12}
13
14macro_rules! impl_NumBufferTrait {
15 ($($signed:ident, $unsigned:ident,)*) => {
16 $(
17 #[stable(feature = "int_format_into", since = "CURRENT_RUSTC_VERSION")]
18 impl NumBufferTrait for $signed {
19 const DEFAULT: Self::Buf = [MaybeUninit::<u8>::uninit(); $signed::MAX.ilog(10) as usize + 2];
21 type Buf = [MaybeUninit<u8>; $signed::MAX.ilog(10) as usize + 2];
22 }
23 #[stable(feature = "int_format_into", since = "CURRENT_RUSTC_VERSION")]
24 impl NumBufferTrait for $unsigned {
25 const DEFAULT: Self::Buf = [MaybeUninit::<u8>::uninit(); $unsigned::MAX.ilog(10) as usize + 1];
26 type Buf = [MaybeUninit<u8>; $unsigned::MAX.ilog(10) as usize + 1];
27 }
28 )*
29 }
30}
31
32impl_NumBufferTrait! {
33 i8, u8,
34 i16, u16,
35 i32, u32,
36 i64, u64,
37 isize, usize,
38 i128, u128,
39}
40
41#[ferrocene::prevalidated]
59#[stable(feature = "int_format_into", since = "CURRENT_RUSTC_VERSION")]
60pub struct NumBuffer<T: NumBufferTrait> {
61 pub(crate) buf: T::Buf,
62 phantom: core::marker::PhantomData<T>,
63}
64
65#[stable(feature = "int_format_into", since = "CURRENT_RUSTC_VERSION")]
66impl<T: NumBufferTrait> core::fmt::Debug for NumBuffer<T> {
67 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
68 f.debug_struct("NumBuffer").finish()
69 }
70}
71
72#[stable(feature = "int_format_into", since = "CURRENT_RUSTC_VERSION")]
73impl<T: NumBufferTrait> NumBuffer<T> {
74 #[ferrocene::prevalidated]
76 #[stable(feature = "int_format_into", since = "CURRENT_RUSTC_VERSION")]
77 #[rustc_const_stable(feature = "int_format_into", since = "CURRENT_RUSTC_VERSION")]
78 pub const fn new() -> Self {
79 NumBuffer { buf: T::DEFAULT, phantom: core::marker::PhantomData }
81 }
82}