@@ -122,6 +122,7 @@ The current mapping of types to traits is:
122
122
* `s` => String
123
123
* `p` => Pointer
124
124
* `t` => Binary
125
+ * `f` => Float
125
126
126
127
What this means is that any type of argument which implements the
127
128
`std::fmt::Binary` trait can then be formatted with `{:t}`. Implementations are
@@ -377,6 +378,8 @@ pub trait String { fn fmt(&Self, &mut Formatter); }
377
378
pub trait Poly { fn fmt ( & Self , & mut Formatter ) ; }
378
379
#[ allow( missing_doc) ]
379
380
pub trait Pointer { fn fmt ( & Self , & mut Formatter ) ; }
381
+ #[ allow( missing_doc) ]
382
+ pub trait Float { fn fmt ( & Self , & mut Formatter ) ; }
380
383
381
384
/// The sprintf function takes a precompiled format string and a list of
382
385
/// arguments, to return the resulting formatted string.
@@ -549,7 +552,20 @@ impl<'self> Formatter<'self> {
549
552
// Helper methods used for padding and processing formatting arguments that
550
553
// all formatting traits can use.
551
554
552
- /// TODO: dox
555
+ /// Performs the correct padding for an integer which has already been
556
+ /// emitted into a byte-array. The byte-array should *not* contain the sign
557
+ /// for the integer, that will be added by this method.
558
+ ///
559
+ /// # Arguments
560
+ ///
561
+ /// * s - the byte array that the number has been formatted into
562
+ /// * alternate_prefix - if the '#' character (FlagAlternate) is
563
+ /// provided, this is the prefix to put in front of the number.
564
+ /// Currently this is 0x/0o/0b/etc.
565
+ /// * positive - whether the original integer was positive or not.
566
+ ///
567
+ /// This function will correctly account for the flags provided as well as
568
+ /// the minimum width. It will not take precision into account.
553
569
pub fn pad_integral ( & mut self , s : & [ u8 ] , alternate_prefix : & str ,
554
570
positive : bool ) {
555
571
use fmt:: parse:: { FlagAlternate , FlagSignPlus , FlagSignAwareZeroPad } ;
@@ -791,6 +807,22 @@ integer!(i16, u16)
791
807
integer ! ( i32 , u32 )
792
808
integer ! ( i64 , u64 )
793
809
810
+ macro_rules! floating( ( $ty: ident) => {
811
+ impl Float for $ty {
812
+ fn fmt( f: & $ty, fmt: & mut Formatter ) {
813
+ // XXX: this shouldn't perform an allocation
814
+ let s = match fmt. precision {
815
+ Some ( i) => :: $ty:: to_str_exact( f. abs( ) , i) ,
816
+ None => :: $ty:: to_str_digits( f. abs( ) , 6 )
817
+ } ;
818
+ fmt. pad_integral( s. as_bytes( ) , "" , * f >= 0.0 ) ;
819
+ }
820
+ }
821
+ } )
822
+ floating ! ( float)
823
+ floating ! ( f32 )
824
+ floating ! ( f64 )
825
+
794
826
impl < T > Poly for T {
795
827
fn fmt ( t : & T , f : & mut Formatter ) {
796
828
match ( f. width , f. precision ) {
0 commit comments