@@ -69,6 +69,16 @@ enum TerminalInner {
6969 TestWriter ( TestWriter , ColorChoice ) ,
7070}
7171
72+ impl TerminalInner {
73+ fn as_write ( & mut self ) -> & mut dyn io:: Write {
74+ match self {
75+ TerminalInner :: StandardStream ( s, _) => s,
76+ #[ cfg( feature = "test" ) ]
77+ TerminalInner :: TestWriter ( w, _) => w,
78+ }
79+ }
80+ }
81+
7282pub struct ColorableTerminalLocked {
7383 // Must drop the lock before the guard, as the guard borrows from inner.
7484 locked : TerminalInnerLocked ,
@@ -83,6 +93,16 @@ enum TerminalInnerLocked {
8393 TestWriter ( TestWriterLock < ' static > ) ,
8494}
8595
96+ impl TerminalInnerLocked {
97+ fn as_write ( & mut self ) -> & mut dyn io:: Write {
98+ match self {
99+ TerminalInnerLocked :: StandardStream ( s) => s,
100+ #[ cfg( feature = "test" ) ]
101+ TerminalInnerLocked :: TestWriter ( w) => w,
102+ }
103+ }
104+ }
105+
86106impl ColorableTerminal {
87107 /// A terminal that supports colorisation of a stream.
88108 /// If `RUSTUP_TERM_COLOR` is set to `always`, or if the stream is a tty and
@@ -202,37 +222,23 @@ pub enum Attr {
202222
203223impl io:: Write for ColorableTerminal {
204224 fn write ( & mut self , buf : & [ u8 ] ) -> std:: result:: Result < usize , io:: Error > {
205- match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
206- TerminalInner :: StandardStream ( s, _) => s. write ( buf) ,
207- #[ cfg( feature = "test" ) ]
208- TerminalInner :: TestWriter ( w, _) => w. write ( buf) ,
209- }
225+ let mut locked = self . inner . lock ( ) . unwrap ( ) ;
226+ locked. deref_mut ( ) . as_write ( ) . write ( buf)
210227 }
211228
212229 fn flush ( & mut self ) -> std:: result:: Result < ( ) , io:: Error > {
213- match self . inner . lock ( ) . unwrap ( ) . deref_mut ( ) {
214- TerminalInner :: StandardStream ( s, _) => s. flush ( ) ,
215- #[ cfg( feature = "test" ) ]
216- TerminalInner :: TestWriter ( w, _) => w. flush ( ) ,
217- }
230+ let mut locked = self . inner . lock ( ) . unwrap ( ) ;
231+ locked. deref_mut ( ) . as_write ( ) . flush ( )
218232 }
219233}
220234
221235impl io:: Write for ColorableTerminalLocked {
222236 fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
223- match & mut self . locked {
224- TerminalInnerLocked :: StandardStream ( s) => s. write ( buf) ,
225- #[ cfg( feature = "test" ) ]
226- TerminalInnerLocked :: TestWriter ( w) => w. write ( buf) ,
227- }
237+ self . locked . as_write ( ) . write ( buf)
228238 }
229239
230240 fn flush ( & mut self ) -> io:: Result < ( ) > {
231- match & mut self . locked {
232- TerminalInnerLocked :: StandardStream ( s) => s. flush ( ) ,
233- #[ cfg( feature = "test" ) ]
234- TerminalInnerLocked :: TestWriter ( w) => w. flush ( ) ,
235- }
241+ self . locked . as_write ( ) . flush ( )
236242 }
237243}
238244
0 commit comments