From 46852d736e896ed9328308115dc06d8fbf84a3ac Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Thu, 13 Aug 2020 15:01:52 +0530 Subject: [PATCH] Remove lifetime from FileDescriptor trait Also: - Remove type annotate `handles` declaration instead of every insert. - Add note about flush being unnecessary when writing to stderr --- src/machine.rs | 2 +- src/shims/posix/fs.rs | 67 ++++++++++++++++++++++--------------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/machine.rs b/src/machine.rs index 9e22825ccac..04bba6c33cd 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -249,7 +249,7 @@ pub struct Evaluator<'mir, 'tcx> { /// Whether to enforce the validity invariant. pub(crate) validate: bool, - pub(crate) file_handler: shims::posix::FileHandler<'tcx>, + pub(crate) file_handler: shims::posix::FileHandler, pub(crate) dir_handler: shims::posix::DirHandler, /// The temporary used for storing the argument of diff --git a/src/shims/posix/fs.rs b/src/shims/posix/fs.rs index cf050b70869..5f31a6375cb 100644 --- a/src/shims/posix/fs.rs +++ b/src/shims/posix/fs.rs @@ -22,41 +22,41 @@ struct FileHandle { writable: bool, } -trait FileDescriptor<'tcx> : std::fmt::Debug { - fn as_file_handle(&self) -> InterpResult<'tcx, &FileHandle>; +trait FileDescriptor : std::fmt::Debug { + fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle>; - fn read(&mut self, communicate_allowed: bool, bytes: &mut [u8]) -> InterpResult<'tcx, io::Result>; - fn write(&mut self, communicate_allowed: bool, bytes: &[u8]) -> InterpResult<'tcx, io::Result>; - fn seek(&mut self, communicate_allowed: bool, offset: SeekFrom) -> InterpResult<'tcx, io::Result>; + fn read<'tcx>(&mut self, communicate_allowed: bool, bytes: &mut [u8]) -> InterpResult<'tcx, io::Result>; + fn write<'tcx>(&mut self, communicate_allowed: bool, bytes: &[u8]) -> InterpResult<'tcx, io::Result>; + fn seek<'tcx>(&mut self, communicate_allowed: bool, offset: SeekFrom) -> InterpResult<'tcx, io::Result>; } -impl<'tcx> FileDescriptor<'tcx> for FileHandle { - fn as_file_handle(&self) -> InterpResult<'tcx, &FileHandle> { +impl FileDescriptor for FileHandle { + fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { Ok(&self) } - fn read(&mut self, communicate_allowed: bool, bytes: &mut [u8]) -> InterpResult<'tcx, io::Result> { + fn read<'tcx>(&mut self, communicate_allowed: bool, bytes: &mut [u8]) -> InterpResult<'tcx, io::Result> { assert!(communicate_allowed, "isolation should have prevented even opening a file"); Ok(self.file.read(bytes)) } - fn write(&mut self, communicate_allowed: bool, bytes: &[u8]) -> InterpResult<'tcx, io::Result> { + fn write<'tcx>(&mut self, communicate_allowed: bool, bytes: &[u8]) -> InterpResult<'tcx, io::Result> { assert!(communicate_allowed, "isolation should have prevented even opening a file"); Ok(self.file.write(bytes)) } - fn seek(&mut self, communicate_allowed: bool, offset: SeekFrom) -> InterpResult<'tcx, io::Result> { + fn seek<'tcx>(&mut self, communicate_allowed: bool, offset: SeekFrom) -> InterpResult<'tcx, io::Result> { assert!(communicate_allowed, "isolation should have prevented even opening a file"); Ok(self.file.seek(offset)) } } -impl<'tcx> FileDescriptor<'tcx> for io::Stdin { - fn as_file_handle(&self) -> InterpResult<'tcx, &FileHandle> { +impl FileDescriptor for io::Stdin { + fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { throw_unsup_format!("stdin cannot be used as FileHandle"); } - fn read(&mut self, communicate_allowed: bool, bytes: &mut [u8]) -> InterpResult<'tcx, io::Result> { + fn read<'tcx>(&mut self, communicate_allowed: bool, bytes: &mut [u8]) -> InterpResult<'tcx, io::Result> { if !communicate_allowed { // We want isolation mode to be deterministic, so we have to disallow all reads, even stdin. helpers::isolation_error("read")?; @@ -64,25 +64,25 @@ fn read(&mut self, communicate_allowed: bool, bytes: &mut [u8]) -> InterpResult< Ok(Read::read(self, bytes)) } - fn write(&mut self, _communicate_allowed: bool, _bytes: &[u8]) -> InterpResult<'tcx, io::Result> { + fn write<'tcx>(&mut self, _communicate_allowed: bool, _bytes: &[u8]) -> InterpResult<'tcx, io::Result> { throw_unsup_format!("cannot write to stdin"); } - fn seek(&mut self, _communicate_allowed: bool, _offset: SeekFrom) -> InterpResult<'tcx, io::Result> { + fn seek<'tcx>(&mut self, _communicate_allowed: bool, _offset: SeekFrom) -> InterpResult<'tcx, io::Result> { throw_unsup_format!("cannot seek on stdin"); } } -impl<'tcx> FileDescriptor<'tcx> for io::Stdout { - fn as_file_handle(&self) -> InterpResult<'tcx, &FileHandle> { +impl FileDescriptor for io::Stdout { + fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { throw_unsup_format!("stdout cannot be used as FileHandle"); } - fn read(&mut self, _communicate_allowed: bool, _bytes: &mut [u8]) -> InterpResult<'tcx, io::Result> { + fn read<'tcx>(&mut self, _communicate_allowed: bool, _bytes: &mut [u8]) -> InterpResult<'tcx, io::Result> { throw_unsup_format!("cannot read from stdout"); } - fn write(&mut self, _communicate_allowed: bool, bytes: &[u8]) -> InterpResult<'tcx, io::Result> { + fn write<'tcx>(&mut self, _communicate_allowed: bool, bytes: &[u8]) -> InterpResult<'tcx, io::Result> { // We allow writing to stderr even with isolation enabled. let result = Write::write(self, bytes); // Stdout is buffered, flush to make sure it appears on the @@ -95,41 +95,42 @@ fn write(&mut self, _communicate_allowed: bool, bytes: &[u8]) -> InterpResult<'t Ok(result) } - fn seek(&mut self, _communicate_allowed: bool, _offset: SeekFrom) -> InterpResult<'tcx, io::Result> { + fn seek<'tcx>(&mut self, _communicate_allowed: bool, _offset: SeekFrom) -> InterpResult<'tcx, io::Result> { throw_unsup_format!("cannot seek on stdout"); } } -impl<'tcx> FileDescriptor<'tcx> for io::Stderr { - fn as_file_handle(&self) -> InterpResult<'tcx, &FileHandle> { +impl FileDescriptor for io::Stderr { + fn as_file_handle<'tcx>(&self) -> InterpResult<'tcx, &FileHandle> { throw_unsup_format!("stdout cannot be used as FileHandle"); } - fn read(&mut self, _communicate_allowed: bool, _bytes: &mut [u8]) -> InterpResult<'tcx, io::Result> { + fn read<'tcx>(&mut self, _communicate_allowed: bool, _bytes: &mut [u8]) -> InterpResult<'tcx, io::Result> { throw_unsup_format!("cannot read from stderr"); } - fn write(&mut self, _communicate_allowed: bool, bytes: &[u8]) -> InterpResult<'tcx, io::Result> { + fn write<'tcx>(&mut self, _communicate_allowed: bool, bytes: &[u8]) -> InterpResult<'tcx, io::Result> { // We allow writing to stderr even with isolation enabled. + // No need to flush, stderr is not buffered. Ok(Write::write(self, bytes)) } - fn seek(&mut self, _communicate_allowed: bool, _offset: SeekFrom) -> InterpResult<'tcx, io::Result> { + fn seek<'tcx>(&mut self, _communicate_allowed: bool, _offset: SeekFrom) -> InterpResult<'tcx, io::Result> { throw_unsup_format!("cannot seek on stderr"); } } #[derive(Debug)] -pub struct FileHandler<'tcx> { - handles: BTreeMap>>, +pub struct FileHandler { + handles: BTreeMap>, } -impl<'tcx> Default for FileHandler<'tcx> { +impl<'tcx> Default for FileHandler { fn default() -> Self { - let mut handles = BTreeMap::new(); - handles.insert(0i32, Box::new(io::stdin()) as Box>); - handles.insert(1i32, Box::new(io::stdout()) as Box>); - handles.insert(2i32, Box::new(io::stderr()) as Box>); + let mut handles : BTreeMap<_, Box> = BTreeMap::new(); + handles.insert(0i32, Box::new(io::stdin())); + handles.insert(1i32, Box::new(io::stdout())); + handles.insert(2i32, Box::new(io::stderr())); FileHandler { handles } @@ -140,7 +141,7 @@ fn default() -> Self { // fd numbers 0, 1, and 2 are reserved for stdin, stdout, and stderr const MIN_NORMAL_FILE_FD: i32 = 3; -impl<'tcx> FileHandler<'tcx> { +impl<'tcx> FileHandler { fn insert_fd(&mut self, file_handle: FileHandle) -> i32 { self.insert_fd_with_min_fd(file_handle, 0) } -- 2.44.0