@@ -22,67 +22,67 @@ struct FileHandle {
22
22
writable : bool ,
23
23
}
24
24
25
- trait FileDescriptor < ' tcx > : std:: fmt:: Debug {
26
- fn as_file_handle ( & self ) -> InterpResult < ' tcx , & FileHandle > ;
25
+ trait FileDescriptor : std:: fmt:: Debug {
26
+ fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > ;
27
27
28
- fn read ( & mut self , communicate_allowed : bool , bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > ;
29
- fn write ( & mut self , communicate_allowed : bool , bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > ;
30
- fn seek ( & mut self , communicate_allowed : bool , offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > ;
28
+ fn read < ' tcx > ( & mut self , communicate_allowed : bool , bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > ;
29
+ fn write < ' tcx > ( & mut self , communicate_allowed : bool , bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > ;
30
+ fn seek < ' tcx > ( & mut self , communicate_allowed : bool , offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > ;
31
31
}
32
32
33
- impl < ' tcx > FileDescriptor < ' tcx > for FileHandle {
34
- fn as_file_handle ( & self ) -> InterpResult < ' tcx , & FileHandle > {
33
+ impl FileDescriptor for FileHandle {
34
+ fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
35
35
Ok ( & self )
36
36
}
37
37
38
- fn read ( & mut self , communicate_allowed : bool , bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
38
+ fn read < ' tcx > ( & mut self , communicate_allowed : bool , bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
39
39
assert ! ( communicate_allowed, "isolation should have prevented even opening a file" ) ;
40
40
Ok ( self . file . read ( bytes) )
41
41
}
42
42
43
- fn write ( & mut self , communicate_allowed : bool , bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
43
+ fn write < ' tcx > ( & mut self , communicate_allowed : bool , bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
44
44
assert ! ( communicate_allowed, "isolation should have prevented even opening a file" ) ;
45
45
Ok ( self . file . write ( bytes) )
46
46
}
47
47
48
- fn seek ( & mut self , communicate_allowed : bool , offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > {
48
+ fn seek < ' tcx > ( & mut self , communicate_allowed : bool , offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > {
49
49
assert ! ( communicate_allowed, "isolation should have prevented even opening a file" ) ;
50
50
Ok ( self . file . seek ( offset) )
51
51
}
52
52
}
53
53
54
- impl < ' tcx > FileDescriptor < ' tcx > for io:: Stdin {
55
- fn as_file_handle ( & self ) -> InterpResult < ' tcx , & FileHandle > {
54
+ impl FileDescriptor for io:: Stdin {
55
+ fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
56
56
throw_unsup_format ! ( "stdin cannot be used as FileHandle" ) ;
57
57
}
58
58
59
- fn read ( & mut self , communicate_allowed : bool , bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
59
+ fn read < ' tcx > ( & mut self , communicate_allowed : bool , bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
60
60
if !communicate_allowed {
61
61
// We want isolation mode to be deterministic, so we have to disallow all reads, even stdin.
62
62
helpers:: isolation_error ( "read" ) ?;
63
63
}
64
64
Ok ( Read :: read ( self , bytes) )
65
65
}
66
66
67
- fn write ( & mut self , _communicate_allowed : bool , _bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
67
+ fn write < ' tcx > ( & mut self , _communicate_allowed : bool , _bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
68
68
throw_unsup_format ! ( "cannot write to stdin" ) ;
69
69
}
70
70
71
- fn seek ( & mut self , _communicate_allowed : bool , _offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > {
71
+ fn seek < ' tcx > ( & mut self , _communicate_allowed : bool , _offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > {
72
72
throw_unsup_format ! ( "cannot seek on stdin" ) ;
73
73
}
74
74
}
75
75
76
- impl < ' tcx > FileDescriptor < ' tcx > for io:: Stdout {
77
- fn as_file_handle ( & self ) -> InterpResult < ' tcx , & FileHandle > {
76
+ impl FileDescriptor for io:: Stdout {
77
+ fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
78
78
throw_unsup_format ! ( "stdout cannot be used as FileHandle" ) ;
79
79
}
80
80
81
- fn read ( & mut self , _communicate_allowed : bool , _bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
81
+ fn read < ' tcx > ( & mut self , _communicate_allowed : bool , _bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
82
82
throw_unsup_format ! ( "cannot read from stdout" ) ;
83
83
}
84
84
85
- fn write ( & mut self , _communicate_allowed : bool , bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
85
+ fn write < ' tcx > ( & mut self , _communicate_allowed : bool , bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
86
86
// We allow writing to stderr even with isolation enabled.
87
87
let result = Write :: write ( self , bytes) ;
88
88
// Stdout is buffered, flush to make sure it appears on the
@@ -95,41 +95,42 @@ impl<'tcx> FileDescriptor<'tcx> for io::Stdout {
95
95
Ok ( result)
96
96
}
97
97
98
- fn seek ( & mut self , _communicate_allowed : bool , _offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > {
98
+ fn seek < ' tcx > ( & mut self , _communicate_allowed : bool , _offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > {
99
99
throw_unsup_format ! ( "cannot seek on stdout" ) ;
100
100
}
101
101
}
102
102
103
- impl < ' tcx > FileDescriptor < ' tcx > for io:: Stderr {
104
- fn as_file_handle ( & self ) -> InterpResult < ' tcx , & FileHandle > {
103
+ impl FileDescriptor for io:: Stderr {
104
+ fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
105
105
throw_unsup_format ! ( "stdout cannot be used as FileHandle" ) ;
106
106
}
107
107
108
- fn read ( & mut self , _communicate_allowed : bool , _bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
108
+ fn read < ' tcx > ( & mut self , _communicate_allowed : bool , _bytes : & mut [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
109
109
throw_unsup_format ! ( "cannot read from stderr" ) ;
110
110
}
111
111
112
- fn write ( & mut self , _communicate_allowed : bool , bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
112
+ fn write < ' tcx > ( & mut self , _communicate_allowed : bool , bytes : & [ u8 ] ) -> InterpResult < ' tcx , io:: Result < usize > > {
113
113
// We allow writing to stderr even with isolation enabled.
114
+ // No need to flush, stderr is not buffered.
114
115
Ok ( Write :: write ( self , bytes) )
115
116
}
116
117
117
- fn seek ( & mut self , _communicate_allowed : bool , _offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > {
118
+ fn seek < ' tcx > ( & mut self , _communicate_allowed : bool , _offset : SeekFrom ) -> InterpResult < ' tcx , io:: Result < u64 > > {
118
119
throw_unsup_format ! ( "cannot seek on stderr" ) ;
119
120
}
120
121
}
121
122
122
123
#[ derive( Debug ) ]
123
- pub struct FileHandler < ' tcx > {
124
- handles : BTreeMap < i32 , Box < dyn FileDescriptor < ' tcx > > > ,
124
+ pub struct FileHandler {
125
+ handles : BTreeMap < i32 , Box < dyn FileDescriptor > > ,
125
126
}
126
127
127
- impl < ' tcx > Default for FileHandler < ' tcx > {
128
+ impl < ' tcx > Default for FileHandler {
128
129
fn default ( ) -> Self {
129
- let mut handles = BTreeMap :: new ( ) ;
130
- handles. insert ( 0i32 , Box :: new ( io:: stdin ( ) ) as Box < dyn FileDescriptor < ' _ > > ) ;
131
- handles. insert ( 1i32 , Box :: new ( io:: stdout ( ) ) as Box < dyn FileDescriptor < ' _ > > ) ;
132
- handles. insert ( 2i32 , Box :: new ( io:: stderr ( ) ) as Box < dyn FileDescriptor < ' _ > > ) ;
130
+ let mut handles: BTreeMap < _ , Box < dyn FileDescriptor > > = BTreeMap :: new ( ) ;
131
+ handles. insert ( 0i32 , Box :: new ( io:: stdin ( ) ) ) ;
132
+ handles. insert ( 1i32 , Box :: new ( io:: stdout ( ) ) ) ;
133
+ handles. insert ( 2i32 , Box :: new ( io:: stderr ( ) ) ) ;
133
134
FileHandler {
134
135
handles
135
136
}
@@ -140,7 +141,7 @@ impl<'tcx> Default for FileHandler<'tcx> {
140
141
// fd numbers 0, 1, and 2 are reserved for stdin, stdout, and stderr
141
142
const MIN_NORMAL_FILE_FD : i32 = 3 ;
142
143
143
- impl < ' tcx > FileHandler < ' tcx > {
144
+ impl < ' tcx > FileHandler {
144
145
fn insert_fd ( & mut self , file_handle : FileHandle ) -> i32 {
145
146
self . insert_fd_with_min_fd ( file_handle, 0 )
146
147
}
0 commit comments