@@ -612,6 +612,47 @@ pub(crate) fn default_read_buf_exact<R: Read + ?Sized>(
612612 Ok ( ( ) )
613613}
614614
615+ pub ( crate ) fn default_write_fmt < W : Write + ?Sized > (
616+ this : & mut W ,
617+ fmt : 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, fmt) {
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+
615656/// The `Read` trait allows for reading bytes from a source.
616657///
617658/// Implementors of the `Read` trait are called 'readers'.
@@ -1867,41 +1908,7 @@ pub trait Write {
18671908 /// ```
18681909 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
18691910 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- }
1904- }
1911+ default_write_fmt ( self , fmt)
19051912 }
19061913
19071914 /// Creates a "by reference" adapter for this instance of `Write`.
0 commit comments