@@ -201,14 +201,22 @@ pub trait Write {
201
201
impl < W : Write + ?Sized > SpecWriteFmt for & mut W {
202
202
#[ inline]
203
203
default fn spec_write_fmt ( mut self , args : Arguments < ' _ > ) -> Result {
204
- if let Some ( s) = args. as_str ( ) { self . write_str ( s) } else { write ( & mut self , args) }
204
+ if let Some ( s) = args. as_const_str ( ) {
205
+ self . write_str ( s)
206
+ } else {
207
+ write ( & mut self , args)
208
+ }
205
209
}
206
210
}
207
211
208
212
impl < W : Write > SpecWriteFmt for & mut W {
209
213
#[ inline]
210
214
fn spec_write_fmt ( self , args : Arguments < ' _ > ) -> Result {
211
- if let Some ( s) = args. as_str ( ) { self . write_str ( s) } else { write ( self , args) }
215
+ if let Some ( s) = args. as_const_str ( ) {
216
+ self . write_str ( s)
217
+ } else {
218
+ write ( self , args)
219
+ }
212
220
}
213
221
}
214
222
@@ -431,17 +439,12 @@ impl<'a> Arguments<'a> {
431
439
}
432
440
}
433
441
434
- /// Same as `as_str`, but will only return a `Some` value if it can be determined at compile time.
442
+ /// Same as [`Arguments::as_str`], but will only return `Some(s)` if it can be determined at compile time.
443
+ #[ must_use]
435
444
#[ inline]
436
445
const fn as_const_str ( & self ) -> Option < & ' static str > {
437
446
let s = self . as_str ( ) ;
438
- // if unsafe { core::intrinsics::is_val_statically_known(matches!((self.pieces, self.args), ([], []) | ([_], []))) } {
439
- if unsafe { core:: intrinsics:: is_val_statically_known ( s) } {
440
- s
441
- } else {
442
- None
443
- }
444
-
447
+ if unsafe { core:: intrinsics:: is_val_statically_known ( s. is_some ( ) ) } { s } else { None }
445
448
}
446
449
}
447
450
@@ -1597,7 +1600,7 @@ impl<'a> Formatter<'a> {
1597
1600
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1598
1601
#[ inline]
1599
1602
pub fn write_fmt ( & mut self , fmt : Arguments < ' _ > ) -> Result {
1600
- if let Some ( s) = fmt. as_str ( ) { self . buf . write_str ( s) } else { write ( self . buf , fmt) }
1603
+ if let Some ( s) = fmt. as_const_str ( ) { self . buf . write_str ( s) } else { write ( self . buf , fmt) }
1601
1604
}
1602
1605
1603
1606
/// Flags for formatting
@@ -2288,7 +2291,11 @@ impl Write for Formatter<'_> {
2288
2291
2289
2292
#[ inline]
2290
2293
fn write_fmt ( & mut self , args : Arguments < ' _ > ) -> Result {
2291
- if let Some ( s) = args. as_str ( ) { self . buf . write_str ( s) } else { write ( self . buf , args) }
2294
+ if let Some ( s) = args. as_const_str ( ) {
2295
+ self . buf . write_str ( s)
2296
+ } else {
2297
+ write ( self . buf , args)
2298
+ }
2292
2299
}
2293
2300
}
2294
2301
0 commit comments