Skip to content

Commit a62f954

Browse files
committed
Auto merge of rust-lang#2444 - oli-obk:file_descriptor_defaults, r=RalfJung
Add default impls for `FileDescriptor` methods I felt like it was just noisy to have to write the "can't do this here" defaults
2 parents c0cbddc + cd6b723 commit a62f954

File tree

3 files changed

+41
-120
lines changed

3 files changed

+41
-120
lines changed

src/shims/unix/fs.rs

Lines changed: 38 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,56 @@ struct FileHandle {
2424
}
2525

2626
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+
}
2832

2933
fn read<'tcx>(
3034
&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+
}
3440

3541
fn write<'tcx>(
3642
&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+
}
4048

4149
fn seek<'tcx>(
4250
&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+
}
4656

4757
fn close<'tcx>(
4858
self: Box<Self>,
4959
_communicate_allowed: bool,
50-
) -> InterpResult<'tcx, io::Result<i32>>;
60+
) -> InterpResult<'tcx, io::Result<i32>> {
61+
throw_unsup_format!("cannot close {}", self.name());
62+
}
5163

5264
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>>;
5365

5466
#[cfg(unix)]
55-
fn as_unix_host_fd(&self) -> Option<i32>;
67+
fn as_unix_host_fd(&self) -> Option<i32> {
68+
None
69+
}
5670
}
5771

5872
impl FileDescriptor for FileHandle {
73+
fn name(&self) -> &'static str {
74+
"FILE"
75+
}
76+
5977
fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> {
6078
Ok(self)
6179
}
@@ -126,8 +144,8 @@ impl FileDescriptor for FileHandle {
126144
}
127145

128146
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"
131149
}
132150

133151
fn read<'tcx>(
@@ -142,29 +160,6 @@ impl FileDescriptor for io::Stdin {
142160
Ok(Read::read(self, bytes))
143161
}
144162

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-
168163
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
169164
Ok(Box::new(io::stdin()))
170165
}
@@ -176,16 +171,8 @@ impl FileDescriptor for io::Stdin {
176171
}
177172

178173
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"
189176
}
190177

191178
fn write<'tcx>(
@@ -205,21 +192,6 @@ impl FileDescriptor for io::Stdout {
205192
Ok(result)
206193
}
207194

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-
223195
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
224196
Ok(Box::new(io::stdout()))
225197
}
@@ -231,16 +203,8 @@ impl FileDescriptor for io::Stdout {
231203
}
232204

233205
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"
244208
}
245209

246210
fn write<'tcx>(
@@ -253,21 +217,6 @@ impl FileDescriptor for io::Stderr {
253217
Ok(Write::write(&mut { self }, bytes))
254218
}
255219

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-
271220
fn dup(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
272221
Ok(Box::new(io::stderr()))
273222
}
@@ -282,16 +231,8 @@ impl FileDescriptor for io::Stderr {
282231
struct DummyOutput;
283232

284233
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"
295236
}
296237

297238
fn write<'tcx>(
@@ -303,29 +244,9 @@ impl FileDescriptor for DummyOutput {
303244
Ok(Ok(bytes.len()))
304245
}
305246

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-
321247
fn dup<'tcx>(&mut self) -> io::Result<Box<dyn FileDescriptor>> {
322248
Ok(Box::new(DummyOutput))
323249
}
324-
325-
#[cfg(unix)]
326-
fn as_unix_host_fd(&self) -> Option<i32> {
327-
None
328-
}
329250
}
330251

331252
#[derive(Debug)]

tests/fail/fs/close_stdout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
fn main() {
99
unsafe {
10-
libc::close(1); //~ ERROR: stdout cannot be closed
10+
libc::close(1); //~ ERROR: cannot close stdout
1111
}
1212
}

tests/fail/fs/close_stdout.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: unsupported operation: stdout cannot be closed
1+
error: unsupported operation: cannot close stdout
22
--> $DIR/close_stdout.rs:LL:CC
33
|
44
LL | libc::close(1);
5-
| ^^^^^^^^^^^^^^ stdout cannot be closed
5+
| ^^^^^^^^^^^^^^ cannot close stdout
66
|
77
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
88
= note: backtrace:

0 commit comments

Comments
 (0)