@@ -24,38 +24,56 @@ struct FileHandle {
24
24
}
25
25
26
26
trait FileDescriptor : std:: fmt:: Debug {
27
- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > ;
27
+ fn name ( & self ) -> & ' static str ;
28
+
29
+ fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
30
+ throw_unsup_format ! ( "{} cannot be used as FileHandle" , self . name( ) ) ;
31
+ }
28
32
29
33
fn read < ' tcx > (
30
34
& mut self ,
31
- communicate_allowed : bool ,
32
- bytes : & mut [ u8 ] ,
33
- ) -> InterpResult < ' tcx , io:: Result < usize > > ;
35
+ _communicate_allowed : bool ,
36
+ _bytes : & mut [ u8 ] ,
37
+ ) -> InterpResult < ' tcx , io:: Result < usize > > {
38
+ throw_unsup_format ! ( "cannot read from {}" , self . name( ) ) ;
39
+ }
34
40
35
41
fn write < ' tcx > (
36
42
& self ,
37
- communicate_allowed : bool ,
38
- bytes : & [ u8 ] ,
39
- ) -> InterpResult < ' tcx , io:: Result < usize > > ;
43
+ _communicate_allowed : bool ,
44
+ _bytes : & [ u8 ] ,
45
+ ) -> InterpResult < ' tcx , io:: Result < usize > > {
46
+ throw_unsup_format ! ( "cannot write to {}" , self . name( ) ) ;
47
+ }
40
48
41
49
fn seek < ' tcx > (
42
50
& mut self ,
43
- communicate_allowed : bool ,
44
- offset : SeekFrom ,
45
- ) -> InterpResult < ' tcx , io:: Result < u64 > > ;
51
+ _communicate_allowed : bool ,
52
+ _offset : SeekFrom ,
53
+ ) -> InterpResult < ' tcx , io:: Result < u64 > > {
54
+ throw_unsup_format ! ( "cannot seek on {}" , self . name( ) ) ;
55
+ }
46
56
47
57
fn close < ' tcx > (
48
58
self : Box < Self > ,
49
59
_communicate_allowed : bool ,
50
- ) -> InterpResult < ' tcx , io:: Result < i32 > > ;
60
+ ) -> InterpResult < ' tcx , io:: Result < i32 > > {
61
+ throw_unsup_format ! ( "cannot close {}" , self . name( ) ) ;
62
+ }
51
63
52
64
fn dup ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > ;
53
65
54
66
#[ cfg( unix) ]
55
- fn as_unix_host_fd ( & self ) -> Option < i32 > ;
67
+ fn as_unix_host_fd ( & self ) -> Option < i32 > {
68
+ None
69
+ }
56
70
}
57
71
58
72
impl FileDescriptor for FileHandle {
73
+ fn name ( & self ) -> & ' static str {
74
+ "FILE"
75
+ }
76
+
59
77
fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
60
78
Ok ( self )
61
79
}
@@ -126,8 +144,8 @@ impl FileDescriptor for FileHandle {
126
144
}
127
145
128
146
impl FileDescriptor for io:: Stdin {
129
- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
130
- throw_unsup_format ! ( "stdin cannot be used as FileHandle" ) ;
147
+ fn name ( & self ) -> & ' static str {
148
+ "stdin"
131
149
}
132
150
133
151
fn read < ' tcx > (
@@ -142,29 +160,6 @@ impl FileDescriptor for io::Stdin {
142
160
Ok ( Read :: read ( self , bytes) )
143
161
}
144
162
145
- fn write < ' tcx > (
146
- & self ,
147
- _communicate_allowed : bool ,
148
- _bytes : & [ u8 ] ,
149
- ) -> InterpResult < ' tcx , io:: Result < usize > > {
150
- throw_unsup_format ! ( "cannot write to stdin" ) ;
151
- }
152
-
153
- fn seek < ' tcx > (
154
- & mut self ,
155
- _communicate_allowed : bool ,
156
- _offset : SeekFrom ,
157
- ) -> InterpResult < ' tcx , io:: Result < u64 > > {
158
- throw_unsup_format ! ( "cannot seek on stdin" ) ;
159
- }
160
-
161
- fn close < ' tcx > (
162
- self : Box < Self > ,
163
- _communicate_allowed : bool ,
164
- ) -> InterpResult < ' tcx , io:: Result < i32 > > {
165
- throw_unsup_format ! ( "stdin cannot be closed" ) ;
166
- }
167
-
168
163
fn dup ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > {
169
164
Ok ( Box :: new ( io:: stdin ( ) ) )
170
165
}
@@ -176,16 +171,8 @@ impl FileDescriptor for io::Stdin {
176
171
}
177
172
178
173
impl FileDescriptor for io:: Stdout {
179
- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
180
- throw_unsup_format ! ( "stdout cannot be used as FileHandle" ) ;
181
- }
182
-
183
- fn read < ' tcx > (
184
- & mut self ,
185
- _communicate_allowed : bool ,
186
- _bytes : & mut [ u8 ] ,
187
- ) -> InterpResult < ' tcx , io:: Result < usize > > {
188
- throw_unsup_format ! ( "cannot read from stdout" ) ;
174
+ fn name ( & self ) -> & ' static str {
175
+ "stdout"
189
176
}
190
177
191
178
fn write < ' tcx > (
@@ -205,21 +192,6 @@ impl FileDescriptor for io::Stdout {
205
192
Ok ( result)
206
193
}
207
194
208
- fn seek < ' tcx > (
209
- & mut self ,
210
- _communicate_allowed : bool ,
211
- _offset : SeekFrom ,
212
- ) -> InterpResult < ' tcx , io:: Result < u64 > > {
213
- throw_unsup_format ! ( "cannot seek on stdout" ) ;
214
- }
215
-
216
- fn close < ' tcx > (
217
- self : Box < Self > ,
218
- _communicate_allowed : bool ,
219
- ) -> InterpResult < ' tcx , io:: Result < i32 > > {
220
- throw_unsup_format ! ( "stdout cannot be closed" ) ;
221
- }
222
-
223
195
fn dup ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > {
224
196
Ok ( Box :: new ( io:: stdout ( ) ) )
225
197
}
@@ -231,16 +203,8 @@ impl FileDescriptor for io::Stdout {
231
203
}
232
204
233
205
impl FileDescriptor for io:: Stderr {
234
- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
235
- throw_unsup_format ! ( "stderr cannot be used as FileHandle" ) ;
236
- }
237
-
238
- fn read < ' tcx > (
239
- & mut self ,
240
- _communicate_allowed : bool ,
241
- _bytes : & mut [ u8 ] ,
242
- ) -> InterpResult < ' tcx , io:: Result < usize > > {
243
- throw_unsup_format ! ( "cannot read from stderr" ) ;
206
+ fn name ( & self ) -> & ' static str {
207
+ "stderr"
244
208
}
245
209
246
210
fn write < ' tcx > (
@@ -253,21 +217,6 @@ impl FileDescriptor for io::Stderr {
253
217
Ok ( Write :: write ( & mut { self } , bytes) )
254
218
}
255
219
256
- fn seek < ' tcx > (
257
- & mut self ,
258
- _communicate_allowed : bool ,
259
- _offset : SeekFrom ,
260
- ) -> InterpResult < ' tcx , io:: Result < u64 > > {
261
- throw_unsup_format ! ( "cannot seek on stderr" ) ;
262
- }
263
-
264
- fn close < ' tcx > (
265
- self : Box < Self > ,
266
- _communicate_allowed : bool ,
267
- ) -> InterpResult < ' tcx , io:: Result < i32 > > {
268
- throw_unsup_format ! ( "stderr cannot be closed" ) ;
269
- }
270
-
271
220
fn dup ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > {
272
221
Ok ( Box :: new ( io:: stderr ( ) ) )
273
222
}
@@ -282,16 +231,8 @@ impl FileDescriptor for io::Stderr {
282
231
struct DummyOutput ;
283
232
284
233
impl FileDescriptor for DummyOutput {
285
- fn as_file_handle < ' tcx > ( & self ) -> InterpResult < ' tcx , & FileHandle > {
286
- throw_unsup_format ! ( "stderr and stdout cannot be used as FileHandle" ) ;
287
- }
288
-
289
- fn read < ' tcx > (
290
- & mut self ,
291
- _communicate_allowed : bool ,
292
- _bytes : & mut [ u8 ] ,
293
- ) -> InterpResult < ' tcx , io:: Result < usize > > {
294
- throw_unsup_format ! ( "cannot read from stderr or stdout" ) ;
234
+ fn name ( & self ) -> & ' static str {
235
+ "stderr and stdout"
295
236
}
296
237
297
238
fn write < ' tcx > (
@@ -303,29 +244,9 @@ impl FileDescriptor for DummyOutput {
303
244
Ok ( Ok ( bytes. len ( ) ) )
304
245
}
305
246
306
- fn seek < ' tcx > (
307
- & mut self ,
308
- _communicate_allowed : bool ,
309
- _offset : SeekFrom ,
310
- ) -> InterpResult < ' tcx , io:: Result < u64 > > {
311
- throw_unsup_format ! ( "cannot seek on stderr or stdout" ) ;
312
- }
313
-
314
- fn close < ' tcx > (
315
- self : Box < Self > ,
316
- _communicate_allowed : bool ,
317
- ) -> InterpResult < ' tcx , io:: Result < i32 > > {
318
- throw_unsup_format ! ( "stderr and stdout cannot be closed" ) ;
319
- }
320
-
321
247
fn dup < ' tcx > ( & mut self ) -> io:: Result < Box < dyn FileDescriptor > > {
322
248
Ok ( Box :: new ( DummyOutput ) )
323
249
}
324
-
325
- #[ cfg( unix) ]
326
- fn as_unix_host_fd ( & self ) -> Option < i32 > {
327
- None
328
- }
329
250
}
330
251
331
252
#[ derive( Debug ) ]
0 commit comments