@@ -294,8 +294,8 @@ pub struct FormattingOptions {
294
294
flags : u32 ,
295
295
fill : char ,
296
296
align : Option < Alignment > ,
297
- width : Option < usize > ,
298
- precision : Option < usize > ,
297
+ width : Option < u16 > ,
298
+ precision : Option < u16 > ,
299
299
}
300
300
301
301
impl FormattingOptions {
@@ -389,7 +389,7 @@ impl FormattingOptions {
389
389
/// the padding specified by [`FormattingOptions::fill`]/[`FormattingOptions::align`]
390
390
/// will be used to take up the required space.
391
391
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
392
- pub fn width ( & mut self , width : Option < usize > ) -> & mut Self {
392
+ pub fn width ( & mut self , width : Option < u16 > ) -> & mut Self {
393
393
self . width = width;
394
394
self
395
395
}
@@ -403,7 +403,7 @@ impl FormattingOptions {
403
403
/// - For floating-point types, this indicates how many digits after the
404
404
/// decimal point should be printed.
405
405
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
406
- pub fn precision ( & mut self , precision : Option < usize > ) -> & mut Self {
406
+ pub fn precision ( & mut self , precision : Option < u16 > ) -> & mut Self {
407
407
self . precision = precision;
408
408
self
409
409
}
@@ -455,12 +455,12 @@ impl FormattingOptions {
455
455
}
456
456
/// Returns the current width.
457
457
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
458
- pub const fn get_width ( & self ) -> Option < usize > {
458
+ pub const fn get_width ( & self ) -> Option < u16 > {
459
459
self . width
460
460
}
461
461
/// Returns the current precision.
462
462
#[ unstable( feature = "formatting_options" , issue = "118117" ) ]
463
- pub const fn get_precision ( & self ) -> Option < usize > {
463
+ pub const fn get_precision ( & self ) -> Option < u16 > {
464
464
self . precision
465
465
}
466
466
/// Returns the current precision.
@@ -1499,15 +1499,18 @@ unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Placeholder, args: &[rt::Argume
1499
1499
unsafe { value. fmt ( fmt) }
1500
1500
}
1501
1501
1502
- unsafe fn getcount ( args : & [ rt:: Argument < ' _ > ] , cnt : & rt:: Count ) -> Option < usize > {
1502
+ unsafe fn getcount ( args : & [ rt:: Argument < ' _ > ] , cnt : & rt:: Count ) -> Option < u16 > {
1503
1503
match * cnt {
1504
+ #[ cfg( bootstrap) ]
1505
+ rt:: Count :: Is ( n) => Some ( n as u16 ) ,
1506
+ #[ cfg( not( bootstrap) ) ]
1504
1507
rt:: Count :: Is ( n) => Some ( n) ,
1505
1508
rt:: Count :: Implied => None ,
1506
1509
rt:: Count :: Param ( i) => {
1507
1510
debug_assert ! ( i < args. len( ) ) ;
1508
1511
// SAFETY: cnt and args come from the same Arguments,
1509
1512
// which guarantees this index is always within bounds.
1510
- unsafe { args. get_unchecked ( i) . as_usize ( ) }
1513
+ unsafe { args. get_unchecked ( i) . as_u16 ( ) }
1511
1514
}
1512
1515
}
1513
1516
}
@@ -1516,11 +1519,11 @@ unsafe fn getcount(args: &[rt::Argument<'_>], cnt: &rt::Count) -> Option<usize>
1516
1519
#[ must_use = "don't forget to write the post padding" ]
1517
1520
pub ( crate ) struct PostPadding {
1518
1521
fill : char ,
1519
- padding : usize ,
1522
+ padding : u16 ,
1520
1523
}
1521
1524
1522
1525
impl PostPadding {
1523
- fn new ( fill : char , padding : usize ) -> PostPadding {
1526
+ fn new ( fill : char , padding : u16 ) -> PostPadding {
1524
1527
PostPadding { fill, padding }
1525
1528
}
1526
1529
@@ -1634,7 +1637,7 @@ impl<'a> Formatter<'a> {
1634
1637
}
1635
1638
// Check if we're over the minimum width, if so then we can also
1636
1639
// just write the bytes.
1637
- Some ( min) if width >= min => {
1640
+ Some ( min) if width >= usize :: from ( min) => {
1638
1641
write_prefix ( self , sign, prefix) ?;
1639
1642
self . buf . write_str ( buf)
1640
1643
}
@@ -1645,7 +1648,7 @@ impl<'a> Formatter<'a> {
1645
1648
let old_align =
1646
1649
crate :: mem:: replace ( & mut self . options . align , Some ( Alignment :: Right ) ) ;
1647
1650
write_prefix ( self , sign, prefix) ?;
1648
- let post_padding = self . padding ( min - width, Alignment :: Right ) ?;
1651
+ let post_padding = self . padding ( min - width as u16 , Alignment :: Right ) ?;
1649
1652
self . buf . write_str ( buf) ?;
1650
1653
post_padding. write ( self ) ?;
1651
1654
self . options . fill = old_fill;
@@ -1654,7 +1657,7 @@ impl<'a> Formatter<'a> {
1654
1657
}
1655
1658
// Otherwise, the sign and prefix goes after the padding
1656
1659
Some ( min) => {
1657
- let post_padding = self . padding ( min - width, Alignment :: Right ) ?;
1660
+ let post_padding = self . padding ( min - width as u16 , Alignment :: Right ) ?;
1658
1661
write_prefix ( self , sign, prefix) ?;
1659
1662
self . buf . write_str ( buf) ?;
1660
1663
post_padding. write ( self )
@@ -1703,7 +1706,7 @@ impl<'a> Formatter<'a> {
1703
1706
// If our string is longer that the precision, then we must have
1704
1707
// truncation. However other flags like `fill`, `width` and `align`
1705
1708
// must act as always.
1706
- if let Some ( ( i, _) ) = s. char_indices ( ) . nth ( max) {
1709
+ if let Some ( ( i, _) ) = s. char_indices ( ) . nth ( usize :: from ( max) ) {
1707
1710
// LLVM here can't prove that `..i` won't panic `&s[..i]`, but
1708
1711
// we know that it can't panic. Use `get` + `unwrap_or` to avoid
1709
1712
// `unsafe` and otherwise don't emit any panic-related code
@@ -1724,14 +1727,14 @@ impl<'a> Formatter<'a> {
1724
1727
let chars_count = s. chars ( ) . count ( ) ;
1725
1728
// If we're under the maximum width, check if we're over the minimum
1726
1729
// width, if so it's as easy as just emitting the string.
1727
- if chars_count >= width {
1730
+ if chars_count >= usize :: from ( width) {
1728
1731
self . buf . write_str ( s)
1729
1732
}
1730
1733
// If we're under both the maximum and the minimum width, then fill
1731
1734
// up the minimum width with the specified string + some alignment.
1732
1735
else {
1733
1736
let align = Alignment :: Left ;
1734
- let post_padding = self . padding ( width - chars_count, align) ?;
1737
+ let post_padding = self . padding ( width - chars_count as u16 , align) ?;
1735
1738
self . buf . write_str ( s) ?;
1736
1739
post_padding. write ( self )
1737
1740
}
@@ -1745,7 +1748,7 @@ impl<'a> Formatter<'a> {
1745
1748
/// thing that is being padded.
1746
1749
pub ( crate ) fn padding (
1747
1750
& mut self ,
1748
- padding : usize ,
1751
+ padding : u16 ,
1749
1752
default : Alignment ,
1750
1753
) -> result:: Result < PostPadding , Error > {
1751
1754
let align = self . align ( ) . unwrap_or ( default) ;
@@ -1785,19 +1788,19 @@ impl<'a> Formatter<'a> {
1785
1788
1786
1789
// remove the sign from the formatted parts
1787
1790
formatted. sign = "" ;
1788
- width = width. saturating_sub ( sign. len ( ) ) ;
1791
+ width = width. saturating_sub ( sign. len ( ) as u16 ) ;
1789
1792
self . options . fill = '0' ;
1790
1793
self . options . align = Some ( Alignment :: Right ) ;
1791
1794
}
1792
1795
1793
1796
// remaining parts go through the ordinary padding process.
1794
1797
let len = formatted. len ( ) ;
1795
- let ret = if width <= len {
1798
+ let ret = if usize :: from ( width) <= len {
1796
1799
// no padding
1797
1800
// SAFETY: Per the precondition.
1798
1801
unsafe { self . write_formatted_parts ( & formatted) }
1799
1802
} else {
1800
- let post_padding = self . padding ( width - len, Alignment :: Right ) ?;
1803
+ let post_padding = self . padding ( width - len as u16 , Alignment :: Right ) ?;
1801
1804
// SAFETY: Per the precondition.
1802
1805
unsafe {
1803
1806
self . write_formatted_parts ( & formatted) ?;
@@ -2029,7 +2032,7 @@ impl<'a> Formatter<'a> {
2029
2032
#[ must_use]
2030
2033
#[ stable( feature = "fmt_flags" , since = "1.5.0" ) ]
2031
2034
pub fn width ( & self ) -> Option < usize > {
2032
- self . options . width
2035
+ self . options . width . map ( |x| x as usize )
2033
2036
}
2034
2037
2035
2038
/// Returns the optionally specified precision for numeric types.
@@ -2060,7 +2063,7 @@ impl<'a> Formatter<'a> {
2060
2063
#[ must_use]
2061
2064
#[ stable( feature = "fmt_flags" , since = "1.5.0" ) ]
2062
2065
pub fn precision ( & self ) -> Option < usize > {
2063
- self . options . precision
2066
+ self . options . precision . map ( |x| x as usize )
2064
2067
}
2065
2068
2066
2069
/// Determines if the `+` flag was specified.
@@ -2800,7 +2803,7 @@ pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Resul
2800
2803
f. options . flags |= 1 << ( rt:: Flag :: SignAwareZeroPad as u32 ) ;
2801
2804
2802
2805
if f. options . width . is_none ( ) {
2803
- f. options . width = Some ( ( usize:: BITS / 4 ) as usize + 2 ) ;
2806
+ f. options . width = Some ( ( usize:: BITS / 4 ) as u16 + 2 ) ;
2804
2807
}
2805
2808
}
2806
2809
f. options . flags |= 1 << ( rt:: Flag :: Alternate as u32 ) ;
0 commit comments