Skip to content

Commit 99a57a3

Browse files
committed
Auto merge of #1509 - samrat:fd-trait-fixes, r=RalfJung
Remove lifetime from FileDescriptor trait Also: - Type annotate `handles` declaration, instead of annotating every insert. - Add note about flush being unnecessary when writing to stderr Addresses comments in #1495 and #1497
2 parents 1bfb26d + 0c25064 commit 99a57a3

File tree

2 files changed

+35
-34
lines changed

2 files changed

+35
-34
lines changed

src/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ pub struct Evaluator<'mir, 'tcx> {
249249
/// Whether to enforce the validity invariant.
250250
pub(crate) validate: bool,
251251

252-
pub(crate) file_handler: shims::posix::FileHandler<'tcx>,
252+
pub(crate) file_handler: shims::posix::FileHandler,
253253
pub(crate) dir_handler: shims::posix::DirHandler,
254254

255255
/// The temporary used for storing the argument of

src/shims/posix/fs.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,67 +22,67 @@ struct FileHandle {
2222
writable: bool,
2323
}
2424

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>;
2727

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>>;
3131
}
3232

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> {
3535
Ok(&self)
3636
}
3737

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>> {
3939
assert!(communicate_allowed, "isolation should have prevented even opening a file");
4040
Ok(self.file.read(bytes))
4141
}
4242

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>> {
4444
assert!(communicate_allowed, "isolation should have prevented even opening a file");
4545
Ok(self.file.write(bytes))
4646
}
4747

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>> {
4949
assert!(communicate_allowed, "isolation should have prevented even opening a file");
5050
Ok(self.file.seek(offset))
5151
}
5252
}
5353

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> {
5656
throw_unsup_format!("stdin cannot be used as FileHandle");
5757
}
5858

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>> {
6060
if !communicate_allowed {
6161
// We want isolation mode to be deterministic, so we have to disallow all reads, even stdin.
6262
helpers::isolation_error("read")?;
6363
}
6464
Ok(Read::read(self, bytes))
6565
}
6666

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>> {
6868
throw_unsup_format!("cannot write to stdin");
6969
}
7070

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>> {
7272
throw_unsup_format!("cannot seek on stdin");
7373
}
7474
}
7575

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> {
7878
throw_unsup_format!("stdout cannot be used as FileHandle");
7979
}
8080

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>> {
8282
throw_unsup_format!("cannot read from stdout");
8383
}
8484

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>> {
8686
// We allow writing to stderr even with isolation enabled.
8787
let result = Write::write(self, bytes);
8888
// Stdout is buffered, flush to make sure it appears on the
@@ -95,41 +95,42 @@ impl<'tcx> FileDescriptor<'tcx> for io::Stdout {
9595
Ok(result)
9696
}
9797

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>> {
9999
throw_unsup_format!("cannot seek on stdout");
100100
}
101101
}
102102

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> {
105105
throw_unsup_format!("stdout cannot be used as FileHandle");
106106
}
107107

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>> {
109109
throw_unsup_format!("cannot read from stderr");
110110
}
111111

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>> {
113113
// We allow writing to stderr even with isolation enabled.
114+
// No need to flush, stderr is not buffered.
114115
Ok(Write::write(self, bytes))
115116
}
116117

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>> {
118119
throw_unsup_format!("cannot seek on stderr");
119120
}
120121
}
121122

122123
#[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>>,
125126
}
126127

127-
impl<'tcx> Default for FileHandler<'tcx> {
128+
impl<'tcx> Default for FileHandler {
128129
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()));
133134
FileHandler {
134135
handles
135136
}
@@ -140,7 +141,7 @@ impl<'tcx> Default for FileHandler<'tcx> {
140141
// fd numbers 0, 1, and 2 are reserved for stdin, stdout, and stderr
141142
const MIN_NORMAL_FILE_FD: i32 = 3;
142143

143-
impl<'tcx> FileHandler<'tcx> {
144+
impl<'tcx> FileHandler {
144145
fn insert_fd(&mut self, file_handle: FileHandle) -> i32 {
145146
self.insert_fd_with_min_fd(file_handle, 0)
146147
}

0 commit comments

Comments
 (0)