1
1
//! Integer and floating-point number formatting
2
2
3
- use crate :: fmt:: NumBuffer ;
3
+ use crate :: fmt:: { NumBuffer , NumBufferTrait } ;
4
4
use crate :: mem:: MaybeUninit ;
5
5
use crate :: num:: fmt as numfmt;
6
6
use crate :: ops:: { Div , Rem , Sub } ;
@@ -338,7 +338,7 @@ macro_rules! impl_Display {
338
338
/// use core::fmt::NumBuffer;
339
339
///
340
340
#[ doc = concat!( "let n = 0" , stringify!( $signed) , ";" ) ]
341
- /// let mut buf = NumBuffer::new();
341
+ # [ doc = concat! ( " let mut buf = NumBuffer::<" , stringify! ( $signed ) , ">:: new();" ) ]
342
342
/// assert_eq!(n.format_into(&mut buf), "0");
343
343
///
344
344
#[ doc = concat!( "let n1 = 32" , stringify!( $signed) , ";" ) ]
@@ -347,8 +347,23 @@ macro_rules! impl_Display {
347
347
#[ doc = concat!( "let n2 = " , stringify!( $signed:: MAX ) , ";" ) ]
348
348
#[ doc = concat!( "assert_eq!(n2.format_into(&mut buf), " , stringify!( $signed:: MAX ) , ".to_string());" ) ]
349
349
/// ```
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
+ /// ```
350
362
#[ 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
+ }
352
367
let mut offset;
353
368
354
369
#[ cfg( not( feature = "optimize_for_size" ) ) ]
@@ -381,7 +396,7 @@ macro_rules! impl_Display {
381
396
/// use core::fmt::NumBuffer;
382
397
///
383
398
#[ doc = concat!( "let n = 0" , stringify!( $unsigned) , ";" ) ]
384
- /// let mut buf = NumBuffer::new();
399
+ # [ doc = concat! ( " let mut buf = NumBuffer::<" , stringify! ( $unsigned ) , ">:: new();" ) ]
385
400
/// assert_eq!(n.format_into(&mut buf), "0");
386
401
///
387
402
#[ doc = concat!( "let n1 = 32" , stringify!( $unsigned) , ";" ) ]
@@ -390,8 +405,23 @@ macro_rules! impl_Display {
390
405
#[ doc = concat!( "let n2 = " , stringify!( $unsigned:: MAX ) , ";" ) ]
391
406
#[ doc = concat!( "assert_eq!(n2.format_into(&mut buf), " , stringify!( $unsigned:: MAX ) , ".to_string());" ) ]
392
407
/// ```
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
+ /// ```
393
420
#[ 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
+ }
395
425
let offset;
396
426
397
427
#[ cfg( not( feature = "optimize_for_size" ) ) ]
@@ -790,19 +820,20 @@ impl u128 {
790
820
/// use core::fmt::NumBuffer;
791
821
///
792
822
/// let n = 0u128;
793
- /// let mut buf = NumBuffer::new();
823
+ /// let mut buf = NumBuffer::<u128>:: new();
794
824
/// assert_eq!(n.format_into(&mut buf), "0");
795
825
///
796
826
/// 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");
799
828
///
800
829
/// 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());
803
831
/// ```
804
832
#[ 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
+ }
806
837
let diff = buf. capacity ( ) - U128_MAX_DEC_N ;
807
838
// FIXME: Once const generics are better, use `NumberBufferTrait::BUF_SIZE` as generic const
808
839
// for `fmt_u128_inner`.
@@ -825,7 +856,7 @@ impl i128 {
825
856
/// use core::fmt::NumBuffer;
826
857
///
827
858
/// let n = 0i128;
828
- /// let mut buf = NumBuffer::new();
859
+ /// let mut buf = NumBuffer::<i128>:: new();
829
860
/// assert_eq!(n.format_into(&mut buf), "0");
830
861
///
831
862
/// let n1 = i128::MIN;
@@ -835,7 +866,10 @@ impl i128 {
835
866
/// assert_eq!(n2.format_into(&mut buf), i128::MAX.to_string());
836
867
/// ```
837
868
#[ 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
+ }
839
873
let diff = buf. capacity ( ) - U128_MAX_DEC_N ;
840
874
// FIXME: Once const generics are better, use `NumberBufferTrait::BUF_SIZE` as generic const
841
875
// for `fmt_u128_inner`.
0 commit comments