@@ -251,22 +251,70 @@ impl FileDescriptor for io::Stderr {
251251 }
252252}
253253
254+ #[ derive( Debug ) ]
255+ struct DummyOutput ;
256+
257+ impl FileDescriptor for DummyOutput {
258+ fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
259+ throw_unsup_format ! ( "stderr and stdout cannot be used as FileHandle" ) ;
260+ }
261+
262+ fn read < ' tcx > (
263+ & mut self ,
264+ _communicate_allowed : bool ,
265+ _bytes : & mut [ u8 ] ,
266+ ) -> InterpResult < ' tcx , io:: Result < usize > > {
267+ throw_unsup_format ! ( "cannot read from stderr or stdout" ) ;
268+ }
269+
270+ fn write < ' tcx > (
271+ & self ,
272+ _communicate_allowed : bool ,
273+ bytes : & [ u8 ] ,
274+ ) -> InterpResult < ' tcx , io:: Result < usize > > {
275+ // We just don't write anything, but report to the user that we did.
276+ Ok ( Ok ( bytes. len ( ) ) )
277+ }
278+
279+ fn seek < ' tcx > (
280+ & mut self ,
281+ _communicate_allowed : bool ,
282+ _offset : SeekFrom ,
283+ ) -> InterpResult < ' tcx , io:: Result < u64 > > {
284+ throw_unsup_format ! ( "cannot seek on stderr or stdout" ) ;
285+ }
286+
287+ fn close < ' tcx > (
288+ self : Box < Self > ,
289+ _communicate_allowed : bool ,
290+ ) -> InterpResult < ' tcx , io:: Result < i32 > > {
291+ throw_unsup_format ! ( "stderr and stdout cannot be closed" ) ;
292+ }
293+
294+ fn dup < ' tcx > ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > {
295+ Ok ( Box :: new ( DummyOutput ) )
296+ }
297+ }
298+
254299#[ derive( Debug ) ]
255300pub struct FileHandler {
256301 handles : BTreeMap < i32 , Box < dyn FileDescriptor > > ,
257302}
258303
259- impl < ' tcx > Default for FileHandler {
260- fn default ( ) -> Self {
304+ impl < ' tcx > FileHandler {
305+ pub ( crate ) fn new ( mute_stdout_stderr : bool ) -> FileHandler {
261306 let mut handles: BTreeMap < _ , Box < dyn FileDescriptor > > = BTreeMap :: new ( ) ;
262- handles. insert ( 0i32 , Box :: new ( io:: stdin ( ) ) ) ;
263- handles. insert ( 1i32 , Box :: new ( io:: stdout ( ) ) ) ;
307+ if mute_stdout_stderr {
308+ handles. insert ( 0i32 , Box :: new ( DummyOutput ) ) ;
309+ handles. insert ( 1i32 , Box :: new ( DummyOutput ) ) ;
310+ } else {
311+ handles. insert ( 0i32 , Box :: new ( io:: stdin ( ) ) ) ;
312+ handles. insert ( 1i32 , Box :: new ( io:: stdout ( ) ) ) ;
313+ }
264314 handles. insert ( 2i32 , Box :: new ( io:: stderr ( ) ) ) ;
265315 FileHandler { handles }
266316 }
267- }
268317
269- impl < ' tcx > FileHandler {
270318 fn insert_fd ( & mut self , file_handle : Box < dyn FileDescriptor > ) -> i32 {
271319 self . insert_fd_with_min_fd ( file_handle, 0 )
272320 }
0 commit comments