]> git.lizzy.rs Git - rust.git/commitdiff
Wrap io::Result from `FileDescriptor::{read,write,seek}` in InterpResult
authorSamrat Man Singh <samratmansingh@gmail.com>
Mon, 3 Aug 2020 05:31:42 +0000 (11:01 +0530)
committerSamrat Man Singh <samratmansingh@gmail.com>
Mon, 3 Aug 2020 05:31:42 +0000 (11:01 +0530)
The outer InterpResult will be used to indicate that a fn is not
implemented for a struct(eg. `write` for Stdin).

The inner io::Result is just the result from the read/write/seek.

src/shims/posix/fs.rs

index 5415e9e1f77cba57c3e107cb08fa228df166403e..6f46401ece657026a9c212af2dbdea038834db4c 100644 (file)
@@ -25,9 +25,9 @@ struct FileHandle {
 trait FileDescriptor<'tcx> : std::fmt::Debug {
     fn as_file_handle(&self) -> InterpResult<'tcx, &FileHandle>;
 
-    fn read(&mut self, bytes: &mut [u8]) -> Result<usize, io::Error>;
-    fn write(&mut self, bytes: &[u8]) -> Result<usize, io::Error>;
-    fn seek(&mut self, offset: SeekFrom) -> Result<u64, io::Error>;
+    fn read(&mut self, bytes: &mut [u8]) -> InterpResult<'tcx, io::Result<usize>>;
+    fn write(&mut self, bytes: &[u8]) -> InterpResult<'tcx, io::Result<usize>>;
+    fn seek(&mut self, offset: SeekFrom) -> InterpResult<'tcx, io::Result<u64>>;
 }
 
 impl<'tcx> FileDescriptor<'tcx> for FileHandle {
@@ -35,16 +35,16 @@ fn as_file_handle(&self) -> InterpResult<'tcx, &FileHandle> {
         Ok(&self)
     }
 
-    fn read(&mut self, bytes: &mut [u8]) -> Result<usize, io::Error> {
-        self.file.read(bytes)
+    fn read(&mut self, bytes: &mut [u8]) -> InterpResult<'tcx, io::Result<usize>> {
+        Ok(self.file.read(bytes))
     }
 
-    fn write(&mut self, bytes: &[u8]) -> Result<usize, io::Error> {
-        self.file.write(bytes)
+    fn write(&mut self, bytes: &[u8]) -> InterpResult<'tcx, io::Result<usize>> {
+        Ok(self.file.write(bytes))
     }
 
-    fn seek(&mut self, offset: SeekFrom) -> Result<u64, io::Error> {
-        self.file.seek(offset)
+    fn seek(&mut self, offset: SeekFrom) -> InterpResult<'tcx, io::Result<u64>> {
+        Ok(self.file.seek(offset))
     }
 }
 
@@ -509,7 +509,7 @@ fn read(
             // `File::read` never returns a value larger than `count`,
             // so this cannot fail.
             let result = file_descriptor
-                .read(&mut bytes)
+                .read(&mut bytes)?
                 .map(|c| i64::try_from(c).unwrap());
 
             match result {
@@ -554,7 +554,7 @@ fn write(
         if let Some(file_descriptor) = this.machine.file_handler.handles.get_mut(&fd) {
             let bytes = this.memory.read_bytes(buf, Size::from_bytes(count))?;
             let result = file_descriptor
-                .write(&bytes)
+                .write(&bytes)?
                 .map(|c| i64::try_from(c).unwrap());
             this.try_unwrap_io_result(result)
         } else {
@@ -590,7 +590,7 @@ fn lseek64(
 
         if let Some(file_descriptor) = this.machine.file_handler.handles.get_mut(&fd) {
             let result = file_descriptor
-                .seek(seek_from)
+                .seek(seek_from)?
                 .map(|offset| i64::try_from(offset).unwrap());
             this.try_unwrap_io_result(result)
         } else {