@@ -132,18 +132,19 @@ const DEC_DIGITS_LUT: [u8; 200] = *b"\
132132// Adaptation of the original implementation at
133133// https://github.com/rust-lang/rust/blob/b8214dc6c6fc20d0a660fb5700dca9ebf51ebe89/src/libcore/fmt/num.rs#L188-L266
134134macro_rules! impl_Integer {
135- ( $t: ty[ len = $max_len : expr ] as $large_unsigned: ty) => {
135+ ( $t: ty as $large_unsigned: ty) => {
136136 impl Integer for $t {
137- const MAX_STR_LEN : usize = $max_len;
137+ #[ allow( unused_comparisons) ]
138+ const MAX_STR_LEN : usize = <$t>:: MAX . ilog10( ) as usize + 1 + ( <$t>:: MIN < 0 ) as usize ;
138139 }
139140
140141 impl private:: Sealed for $t {
141- type Buffer = [ MaybeUninit <u8 >; $max_len ] ;
142+ type Buffer = [ MaybeUninit <u8 >; Self :: MAX_STR_LEN ] ;
142143
143144 #[ allow( unused_comparisons) ]
144145 #[ inline]
145146 #[ cfg_attr( feature = "no-panic" , no_panic) ]
146- fn write( self , buf: & mut [ MaybeUninit < u8 > ; $max_len ] ) -> & str {
147+ fn write( self , buf: & mut Self :: Buffer ) -> & str {
147148 let is_nonnegative = self >= 0 ;
148149 let mut n = if is_nonnegative {
149150 self as $large_unsigned
@@ -208,14 +209,14 @@ macro_rules! impl_Integer {
208209 } ;
209210}
210211
211- impl_Integer ! ( i8 [ len = 4 ] as u32 ) ;
212- impl_Integer ! ( u8 [ len = 3 ] as u32 ) ;
213- impl_Integer ! ( i16 [ len = 6 ] as u32 ) ;
214- impl_Integer ! ( u16 [ len = 5 ] as u32 ) ;
215- impl_Integer ! ( i32 [ len = 11 ] as u32 ) ;
216- impl_Integer ! ( u32 [ len = 10 ] as u32 ) ;
217- impl_Integer ! ( i64 [ len = 20 ] as u64 ) ;
218- impl_Integer ! ( u64 [ len = 20 ] as u64 ) ;
212+ impl_Integer ! ( i8 as u32 ) ;
213+ impl_Integer ! ( u8 as u32 ) ;
214+ impl_Integer ! ( i16 as u32 ) ;
215+ impl_Integer ! ( u16 as u32 ) ;
216+ impl_Integer ! ( i32 as u32 ) ;
217+ impl_Integer ! ( u32 as u32 ) ;
218+ impl_Integer ! ( i64 as u64 ) ;
219+ impl_Integer ! ( u64 as u64 ) ;
219220
220221macro_rules! impl_Integer_size {
221222 ( $t: ty as $primitive: ident #[ cfg( target_pointer_width = $width: literal) ] ) => {
@@ -245,18 +246,19 @@ impl_Integer_size!(isize as i64 #[cfg(target_pointer_width = "64")]);
245246impl_Integer_size ! ( usize as u64 #[ cfg( target_pointer_width = "64" ) ] ) ;
246247
247248macro_rules! impl_Integer128 {
248- ( $t: ty[ len = $max_len : expr ] ) => {
249+ ( $t: ty) => {
249250 impl Integer for $t {
250- const MAX_STR_LEN : usize = $max_len;
251+ #[ allow( unused_comparisons) ]
252+ const MAX_STR_LEN : usize = <$t>:: MAX . ilog10( ) as usize + 1 + ( <$t>:: MIN < 0 ) as usize ;
251253 }
252254
253255 impl private:: Sealed for $t {
254- type Buffer = [ MaybeUninit <u8 >; $max_len ] ;
256+ type Buffer = [ MaybeUninit <u8 >; Self :: MAX_STR_LEN ] ;
255257
256258 #[ allow( unused_comparisons) ]
257259 #[ inline]
258260 #[ cfg_attr( feature = "no-panic" , no_panic) ]
259- fn write( self , buf: & mut [ MaybeUninit < u8 > ; $max_len ] ) -> & str {
261+ fn write( self , buf: & mut Self :: Buffer ) -> & str {
260262 let is_nonnegative = self >= 0 ;
261263 let n = if is_nonnegative {
262264 self as u128
@@ -325,5 +327,5 @@ macro_rules! impl_Integer128 {
325327 } ;
326328}
327329
328- impl_Integer128 ! ( i128 [ len = 40 ] ) ;
329- impl_Integer128 ! ( u128 [ len = 39 ] ) ;
330+ impl_Integer128 ! ( i128 ) ;
331+ impl_Integer128 ! ( u128 ) ;
0 commit comments