@@ -69,6 +69,16 @@ enum TerminalInner {
69
69
TestWriter ( TestWriter , ColorChoice ) ,
70
70
}
71
71
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
+
72
82
pub struct ColorableTerminalLocked {
73
83
// Must drop the lock before the guard, as the guard borrows from inner.
74
84
locked : TerminalInnerLocked ,
@@ -83,6 +93,16 @@ enum TerminalInnerLocked {
83
93
TestWriter ( TestWriterLock < ' static > ) ,
84
94
}
85
95
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
+
86
106
impl ColorableTerminal {
87
107
/// A terminal that supports colorisation of a stream.
88
108
/// If `RUSTUP_TERM_COLOR` is set to `always`, or if the stream is a tty and
@@ -202,37 +222,23 @@ pub enum Attr {
202
222
203
223
impl io:: Write for ColorableTerminal {
204
224
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)
210
227
}
211
228
212
229
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 ( )
218
232
}
219
233
}
220
234
221
235
impl io:: Write for ColorableTerminalLocked {
222
236
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)
228
238
}
229
239
230
240
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 ( )
236
242
}
237
243
}
238
244
0 commit comments