@@ -612,6 +612,47 @@ pub(crate) fn default_read_buf_exact<R: Read + ?Sized>(
612
612
Ok ( ( ) )
613
613
}
614
614
615
+ pub ( crate ) fn default_write_fmt < W : Write + ?Sized > (
616
+ this : & mut W ,
617
+ args : fmt:: Arguments < ' _ > ,
618
+ ) -> Result < ( ) > {
619
+ // Create a shim which translates a `Write` to a `fmt::Write` and saves off
620
+ // I/O errors, instead of discarding them.
621
+ struct Adapter < ' a , T : ?Sized + ' a > {
622
+ inner : & ' a mut T ,
623
+ error : Result < ( ) > ,
624
+ }
625
+
626
+ impl < T : Write + ?Sized > fmt:: Write for Adapter < ' _ , T > {
627
+ fn write_str ( & mut self , s : & str ) -> fmt:: Result {
628
+ match self . inner . write_all ( s. as_bytes ( ) ) {
629
+ Ok ( ( ) ) => Ok ( ( ) ) ,
630
+ Err ( e) => {
631
+ self . error = Err ( e) ;
632
+ Err ( fmt:: Error )
633
+ }
634
+ }
635
+ }
636
+ }
637
+
638
+ let mut output = Adapter { inner : this, error : Ok ( ( ) ) } ;
639
+ match fmt:: write ( & mut output, args) {
640
+ Ok ( ( ) ) => Ok ( ( ) ) ,
641
+ Err ( ..) => {
642
+ // Check whether the error came from the underlying `Write`.
643
+ if output. error . is_err ( ) {
644
+ output. error
645
+ } else {
646
+ // This shouldn't happen: the underlying stream did not error,
647
+ // but somehow the formatter still errored?
648
+ panic ! (
649
+ "a formatting trait implementation returned an error when the underlying stream did not"
650
+ ) ;
651
+ }
652
+ }
653
+ }
654
+ }
655
+
615
656
/// The `Read` trait allows for reading bytes from a source.
616
657
///
617
658
/// Implementors of the `Read` trait are called 'readers'.
@@ -1866,41 +1907,11 @@ pub trait Write {
1866
1907
/// }
1867
1908
/// ```
1868
1909
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1869
- fn write_fmt ( & mut self , fmt : fmt:: Arguments < ' _ > ) -> Result < ( ) > {
1870
- // Create a shim which translates a Write to a fmt::Write and saves
1871
- // off I/O errors. instead of discarding them
1872
- struct Adapter < ' a , T : ?Sized + ' a > {
1873
- inner : & ' a mut T ,
1874
- error : Result < ( ) > ,
1875
- }
1876
-
1877
- impl < T : Write + ?Sized > fmt:: Write for Adapter < ' _ , T > {
1878
- fn write_str ( & mut self , s : & str ) -> fmt:: Result {
1879
- match self . inner . write_all ( s. as_bytes ( ) ) {
1880
- Ok ( ( ) ) => Ok ( ( ) ) ,
1881
- Err ( e) => {
1882
- self . error = Err ( e) ;
1883
- Err ( fmt:: Error )
1884
- }
1885
- }
1886
- }
1887
- }
1888
-
1889
- let mut output = Adapter { inner : self , error : Ok ( ( ) ) } ;
1890
- match fmt:: write ( & mut output, fmt) {
1891
- Ok ( ( ) ) => Ok ( ( ) ) ,
1892
- Err ( ..) => {
1893
- // check if the error came from the underlying `Write` or not
1894
- if output. error . is_err ( ) {
1895
- output. error
1896
- } else {
1897
- // This shouldn't happen: the underlying stream did not error, but somehow
1898
- // the formatter still errored?
1899
- panic ! (
1900
- "a formatting trait implementation returned an error when the underlying stream did not"
1901
- ) ;
1902
- }
1903
- }
1910
+ fn write_fmt ( & mut self , args : fmt:: Arguments < ' _ > ) -> Result < ( ) > {
1911
+ if let Some ( s) = args. as_statically_known_str ( ) {
1912
+ self . write_all ( s. as_bytes ( ) )
1913
+ } else {
1914
+ default_write_fmt ( self , args)
1904
1915
}
1905
1916
}
1906
1917
0 commit comments