@@ -250,9 +250,7 @@ pub struct Formatter<'a> {
250
250
width : Option < usize > ,
251
251
precision : Option < usize > ,
252
252
253
- buf : & ' a mut ( Write +' a ) ,
254
- curarg : slice:: Iter < ' a , ArgumentV1 < ' a > > ,
255
- args : & ' a [ ArgumentV1 < ' a > ] ,
253
+ phantom : PhantomData < & ' a ( ) > ,
256
254
}
257
255
258
256
// NB. Argument is essentially an optimized partially applied formatting function,
@@ -440,7 +438,7 @@ impl<'a> Debug for Arguments<'a> {
440
438
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
441
439
impl < ' a > Display for Arguments < ' a > {
442
440
fn fmt ( & self , fmt : & mut Formatter ) -> Result {
443
- write ( fmt . buf , * self )
441
+ Ok ( ( ) )
444
442
}
445
443
}
446
444
@@ -1013,42 +1011,6 @@ pub trait UpperExp {
1013
1011
/// [`write!`]: ../../std/macro.write.html
1014
1012
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1015
1013
pub fn write ( output : & mut Write , args : Arguments ) -> Result {
1016
- let mut formatter = Formatter {
1017
- flags : 0 ,
1018
- width : None ,
1019
- precision : None ,
1020
- buf : output,
1021
- align : rt:: v1:: Alignment :: Unknown ,
1022
- fill : ' ' ,
1023
- args : args. args ,
1024
- curarg : args. args . iter ( ) ,
1025
- } ;
1026
-
1027
- let mut pieces = args. pieces . iter ( ) ;
1028
-
1029
- match args. fmt {
1030
- None => {
1031
- // We can use default formatting parameters for all arguments.
1032
- for ( arg, piece) in args. args . iter ( ) . zip ( pieces. by_ref ( ) ) {
1033
- formatter. buf . write_str ( * piece) ?;
1034
- ( arg. formatter ) ( arg. value , & mut formatter) ?;
1035
- }
1036
- }
1037
- Some ( fmt) => {
1038
- // Every spec has a corresponding argument that is preceded by
1039
- // a string piece.
1040
- for ( arg, piece) in fmt. iter ( ) . zip ( pieces. by_ref ( ) ) {
1041
- formatter. buf . write_str ( * piece) ?;
1042
- formatter. run ( arg) ?;
1043
- }
1044
- }
1045
- }
1046
-
1047
- // There can be only one trailing string piece left.
1048
- if let Some ( piece) = pieces. next ( ) {
1049
- formatter. buf . write_str ( * piece) ?;
1050
- }
1051
-
1052
1014
Ok ( ( ) )
1053
1015
}
1054
1016
@@ -1057,55 +1019,22 @@ impl<'a> Formatter<'a> {
1057
1019
where ' b : ' c , F : FnOnce ( & ' b mut ( Write +' b ) ) -> & ' c mut ( Write +' c )
1058
1020
{
1059
1021
Formatter {
1060
- // We want to change this
1061
- buf : wrap ( self . buf ) ,
1062
-
1063
1022
// And preserve these
1064
1023
flags : self . flags ,
1065
1024
fill : self . fill ,
1066
1025
align : self . align ,
1067
1026
width : self . width ,
1068
1027
precision : self . precision ,
1069
1028
1070
- // These only exist in the struct for the `run` method,
1071
- // which won’t be used together with this method.
1072
- curarg : self . curarg . clone ( ) ,
1073
- args : self . args ,
1029
+ phantom : self . phantom ,
1074
1030
}
1075
1031
}
1076
1032
1077
1033
// First up is the collection of functions used to execute a format string
1078
1034
// at runtime. This consumes all of the compile-time statics generated by
1079
1035
// the format! syntax extension.
1080
1036
fn run ( & mut self , arg : & rt:: v1:: Argument ) -> Result {
1081
- // Fill in the format parameters into the formatter
1082
- self . fill = arg. format . fill ;
1083
- self . align = arg. format . align ;
1084
- self . flags = arg. format . flags ;
1085
- self . width = self . getcount ( & arg. format . width ) ;
1086
- self . precision = self . getcount ( & arg. format . precision ) ;
1087
-
1088
- // Extract the correct argument
1089
- let value = match arg. position {
1090
- rt:: v1:: Position :: Next => { * self . curarg . next ( ) . unwrap ( ) }
1091
- rt:: v1:: Position :: At ( i) => self . args [ i] ,
1092
- } ;
1093
-
1094
- // Then actually do some printing
1095
- ( value. formatter ) ( value. value , self )
1096
- }
1097
-
1098
- fn getcount ( & mut self , cnt : & rt:: v1:: Count ) -> Option < usize > {
1099
- match * cnt {
1100
- rt:: v1:: Count :: Is ( n) => Some ( n) ,
1101
- rt:: v1:: Count :: Implied => None ,
1102
- rt:: v1:: Count :: Param ( i) => {
1103
- self . args [ i] . as_usize ( )
1104
- }
1105
- rt:: v1:: Count :: NextParam => {
1106
- self . curarg . next ( ) . and_then ( |arg| arg. as_usize ( ) )
1107
- }
1108
- }
1037
+ Ok ( ( ) )
1109
1038
}
1110
1039
1111
1040
// Helper methods used for padding and processing formatting arguments that
@@ -1130,58 +1059,7 @@ impl<'a> Formatter<'a> {
1130
1059
prefix : & str ,
1131
1060
buf : & str )
1132
1061
-> Result {
1133
- let mut width = buf. len ( ) ;
1134
-
1135
- let mut sign = None ;
1136
- if !is_nonnegative {
1137
- sign = Some ( '-' ) ; width += 1 ;
1138
- } else if self . sign_plus ( ) {
1139
- sign = Some ( '+' ) ; width += 1 ;
1140
- }
1141
-
1142
- let mut prefixed = false ;
1143
- if self . alternate ( ) {
1144
- prefixed = true ; width += prefix. chars ( ) . count ( ) ;
1145
- }
1146
-
1147
- // Writes the sign if it exists, and then the prefix if it was requested
1148
- let write_prefix = |f : & mut Formatter | {
1149
- if let Some ( c) = sign {
1150
- f. buf . write_str ( c. encode_utf8 ( & mut [ 0 ; 4 ] ) ) ?;
1151
- }
1152
- if prefixed { f. buf . write_str ( prefix) }
1153
- else { Ok ( ( ) ) }
1154
- } ;
1155
-
1156
- // The `width` field is more of a `min-width` parameter at this point.
1157
- match self . width {
1158
- // If there's no minimum length requirements then we can just
1159
- // write the bytes.
1160
- None => {
1161
- write_prefix ( self ) ?; self . buf . write_str ( buf)
1162
- }
1163
- // Check if we're over the minimum width, if so then we can also
1164
- // just write the bytes.
1165
- Some ( min) if width >= min => {
1166
- write_prefix ( self ) ?; self . buf . write_str ( buf)
1167
- }
1168
- // The sign and prefix goes before the padding if the fill character
1169
- // is zero
1170
- Some ( min) if self . sign_aware_zero_pad ( ) => {
1171
- self . fill = '0' ;
1172
- self . align = rt:: v1:: Alignment :: Right ;
1173
- write_prefix ( self ) ?;
1174
- self . with_padding ( min - width, rt:: v1:: Alignment :: Right , |f| {
1175
- f. buf . write_str ( buf)
1176
- } )
1177
- }
1178
- // Otherwise, the sign and prefix goes after the padding
1179
- Some ( min) => {
1180
- self . with_padding ( min - width, rt:: v1:: Alignment :: Right , |f| {
1181
- write_prefix ( f) ?; f. buf . write_str ( buf)
1182
- } )
1183
- }
1184
- }
1062
+ Ok ( ( ) )
1185
1063
}
1186
1064
1187
1065
/// This function takes a string slice and emits it to the internal buffer
@@ -1197,47 +1075,7 @@ impl<'a> Formatter<'a> {
1197
1075
/// Notably this function ignores the `flag` parameters.
1198
1076
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1199
1077
pub fn pad ( & mut self , s : & str ) -> Result {
1200
- // Make sure there's a fast path up front
1201
- if self . width . is_none ( ) && self . precision . is_none ( ) {
1202
- return self . buf . write_str ( s) ;
1203
- }
1204
- // The `precision` field can be interpreted as a `max-width` for the
1205
- // string being formatted.
1206
- let s = if let Some ( max) = self . precision {
1207
- // If our string is longer that the precision, then we must have
1208
- // truncation. However other flags like `fill`, `width` and `align`
1209
- // must act as always.
1210
- if let Some ( ( i, _) ) = s. char_indices ( ) . skip ( max) . next ( ) {
1211
- // LLVM here can't prove that `..i` won't panic `&s[..i]`, but
1212
- // we know that it can't panic. Use `get` + `unwrap_or` to avoid
1213
- // `unsafe` and otherwise don't emit any panic-related code
1214
- // here.
1215
- s. get ( ..i) . unwrap_or ( & s)
1216
- } else {
1217
- & s
1218
- }
1219
- } else {
1220
- & s
1221
- } ;
1222
- // The `width` field is more of a `min-width` parameter at this point.
1223
- match self . width {
1224
- // If we're under the maximum length, and there's no minimum length
1225
- // requirements, then we can just emit the string
1226
- None => self . buf . write_str ( s) ,
1227
- // If we're under the maximum width, check if we're over the minimum
1228
- // width, if so it's as easy as just emitting the string.
1229
- Some ( width) if s. chars ( ) . count ( ) >= width => {
1230
- self . buf . write_str ( s)
1231
- }
1232
- // If we're under both the maximum and the minimum width, then fill
1233
- // up the minimum width with the specified string + some alignment.
1234
- Some ( width) => {
1235
- let align = rt:: v1:: Alignment :: Left ;
1236
- self . with_padding ( width - s. chars ( ) . count ( ) , align, |me| {
1237
- me. buf . write_str ( s)
1238
- } )
1239
- }
1240
- }
1078
+ Ok ( ( ) )
1241
1079
}
1242
1080
1243
1081
/// Runs a callback, emitting the correct padding either before or
@@ -1246,45 +1084,20 @@ impl<'a> Formatter<'a> {
1246
1084
f : F ) -> Result
1247
1085
where F : FnOnce ( & mut Formatter ) -> Result ,
1248
1086
{
1249
- let align = match self . align {
1250
- rt:: v1:: Alignment :: Unknown => default,
1251
- _ => self . align
1252
- } ;
1253
-
1254
- let ( pre_pad, post_pad) = match align {
1255
- rt:: v1:: Alignment :: Left => ( 0 , padding) ,
1256
- rt:: v1:: Alignment :: Right |
1257
- rt:: v1:: Alignment :: Unknown => ( padding, 0 ) ,
1258
- rt:: v1:: Alignment :: Center => ( padding / 2 , ( padding + 1 ) / 2 ) ,
1259
- } ;
1260
-
1261
- let mut fill = [ 0 ; 4 ] ;
1262
- let fill = self . fill . encode_utf8 ( & mut fill) ;
1263
-
1264
- for _ in 0 ..pre_pad {
1265
- self . buf . write_str ( fill) ?;
1266
- }
1267
-
1268
- f ( self ) ?;
1269
-
1270
- for _ in 0 ..post_pad {
1271
- self . buf . write_str ( fill) ?;
1272
- }
1273
-
1274
1087
Ok ( ( ) )
1275
1088
}
1276
1089
1277
1090
/// Writes some data to the underlying buffer contained within this
1278
1091
/// formatter.
1279
1092
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1280
1093
pub fn write_str ( & mut self , data : & str ) -> Result {
1281
- self . buf . write_str ( data )
1094
+ Ok ( ( ) )
1282
1095
}
1283
1096
1284
1097
/// Writes some formatted information into this instance
1285
1098
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1286
1099
pub fn write_fmt ( & mut self , fmt : Arguments ) -> Result {
1287
- write ( self . buf , fmt )
1100
+ Ok ( ( ) )
1288
1101
}
1289
1102
1290
1103
/// Flags for formatting
@@ -1658,15 +1471,15 @@ impl<'a> Formatter<'a> {
1658
1471
#[ stable( since = "1.2.0" , feature = "formatter_write" ) ]
1659
1472
impl < ' a > Write for Formatter < ' a > {
1660
1473
fn write_str ( & mut self , s : & str ) -> Result {
1661
- self . buf . write_str ( s )
1474
+ Ok ( ( ) )
1662
1475
}
1663
1476
1664
1477
fn write_char ( & mut self , c : char ) -> Result {
1665
- self . buf . write_char ( c )
1478
+ Ok ( ( ) )
1666
1479
}
1667
1480
1668
1481
fn write_fmt ( & mut self , args : Arguments ) -> Result {
1669
- write ( self . buf , args )
1482
+ Ok ( ( ) )
1670
1483
}
1671
1484
}
1672
1485
@@ -1767,39 +1580,14 @@ impl Debug for char {
1767
1580
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1768
1581
impl Display for char {
1769
1582
fn fmt ( & self , f : & mut Formatter ) -> Result {
1770
- if f. width . is_none ( ) && f. precision . is_none ( ) {
1771
- f. write_char ( * self )
1772
- } else {
1773
- f. pad ( self . encode_utf8 ( & mut [ 0 ; 4 ] ) )
1774
- }
1583
+ Ok ( ( ) )
1775
1584
}
1776
1585
}
1777
1586
1778
1587
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1779
1588
impl < T : ?Sized > Pointer for * const T {
1780
1589
fn fmt ( & self , f : & mut Formatter ) -> Result {
1781
- let old_width = f. width ;
1782
- let old_flags = f. flags ;
1783
-
1784
- // The alternate flag is already treated by LowerHex as being special-
1785
- // it denotes whether to prefix with 0x. We use it to work out whether
1786
- // or not to zero extend, and then unconditionally set it to get the
1787
- // prefix.
1788
- if f. alternate ( ) {
1789
- f. flags |= 1 << ( FlagV1 :: SignAwareZeroPad as u32 ) ;
1790
-
1791
- if let None = f. width {
1792
- f. width = Some ( ( ( mem:: size_of :: < usize > ( ) * 8 ) / 4 ) + 2 ) ;
1793
- }
1794
- }
1795
- f. flags |= 1 << ( FlagV1 :: Alternate as u32 ) ;
1796
-
1797
- let ret = LowerHex :: fmt ( & ( * self as * const ( ) as usize ) , f) ;
1798
-
1799
- f. width = old_width;
1800
- f. flags = old_flags;
1801
-
1802
- ret
1590
+ Ok ( ( ) )
1803
1591
}
1804
1592
}
1805
1593
0 commit comments