@@ -201,14 +201,22 @@ pub trait Write {
201201 impl < W : Write + ?Sized > SpecWriteFmt for & mut W {
202202 #[ inline]
203203 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+ }
205209 }
206210 }
207211
208212 impl < W : Write > SpecWriteFmt for & mut W {
209213 #[ inline]
210214 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+ }
212220 }
213221 }
214222
@@ -431,17 +439,12 @@ impl<'a> Arguments<'a> {
431439 }
432440 }
433441
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]
435444 #[ inline]
436445 const fn as_const_str ( & self ) -> Option < & ' static str > {
437446 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 }
445448 }
446449}
447450
@@ -1597,7 +1600,7 @@ impl<'a> Formatter<'a> {
15971600 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
15981601 #[ inline]
15991602 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) }
16011604 }
16021605
16031606 /// Flags for formatting
@@ -2288,7 +2291,11 @@ impl Write for Formatter<'_> {
22882291
22892292 #[ inline]
22902293 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+ }
22922299 }
22932300}
22942301
0 commit comments