@@ -192,10 +192,6 @@ impl FileDescription for NullOutput {
192192pub struct FileDescriptor ( Rc < RefCell < Box < dyn FileDescription > > > ) ;
193193
194194impl FileDescriptor {
195- pub fn new < T : FileDescription > ( fd : T ) -> Self {
196- FileDescriptor ( Rc :: new ( RefCell :: new ( Box :: new ( fd) ) ) )
197- }
198-
199195 pub fn borrow ( & self ) -> Ref < ' _ , dyn FileDescription > {
200196 Ref :: map ( self . 0 . borrow ( ) , |fd| fd. as_ref ( ) )
201197 }
@@ -227,20 +223,25 @@ impl VisitProvenance for FdTable {
227223}
228224
229225impl FdTable {
230- pub ( crate ) fn new ( mute_stdout_stderr : bool ) -> FdTable {
231- let mut fds: BTreeMap < _ , FileDescriptor > = BTreeMap :: new ( ) ;
232- fds. insert ( 0i32 , FileDescriptor :: new ( io:: stdin ( ) ) ) ;
226+ fn new ( ) -> Self {
227+ FdTable { fds : BTreeMap :: new ( ) }
228+ }
229+ pub ( crate ) fn init ( mute_stdout_stderr : bool ) -> FdTable {
230+ let mut fds = FdTable :: new ( ) ;
231+ fds. insert_fd ( io:: stdin ( ) ) ;
233232 if mute_stdout_stderr {
234- fds. insert ( 1i32 , FileDescriptor :: new ( NullOutput ) ) ;
235- fds. insert ( 2i32 , FileDescriptor :: new ( NullOutput ) ) ;
233+ assert_eq ! ( fds. insert_fd ( NullOutput ) , 1 ) ;
234+ assert_eq ! ( fds. insert_fd ( NullOutput ) , 2 ) ;
236235 } else {
237- fds. insert ( 1i32 , FileDescriptor :: new ( io:: stdout ( ) ) ) ;
238- fds. insert ( 2i32 , FileDescriptor :: new ( io:: stderr ( ) ) ) ;
236+ assert_eq ! ( fds. insert_fd ( io:: stdout( ) ) , 1 ) ;
237+ assert_eq ! ( fds. insert_fd ( io:: stderr( ) ) , 2 ) ;
239238 }
240- FdTable { fds }
239+ fds
241240 }
242241
243- pub fn insert_fd ( & mut self , file_handle : FileDescriptor ) -> i32 {
242+ /// Insert a file descriptor to the FdTable.
243+ pub fn insert_fd < T : FileDescription > ( & mut self , fd : T ) -> i32 {
244+ let file_handle = FileDescriptor ( Rc :: new ( RefCell :: new ( Box :: new ( fd) ) ) ) ;
244245 self . insert_fd_with_min_fd ( file_handle, 0 )
245246 }
246247
0 commit comments