Skip to content

Commit be4e10d

Browse files
committed
Rename consume_result
1 parent 72268da commit be4e10d

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

src/helpers.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,23 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
373373
Size::from_bits(32),
374374
))
375375
}
376+
377+
/// Helper function that consumes an `std::io::Result<T>` and returns an
378+
/// `InterpResult<'tcx,T>::Ok` instead. It is expected that the result can be converted to an
379+
/// OS error using `std::io::Error::raw_os_error`.
380+
///
381+
/// This function uses `T: From<i32>` instead of `i32` directly because some IO related
382+
/// functions return different integer types (like `read`, that returns an `i64`)
383+
fn set_last_error_from_io_result<T: From<i32>>(
384+
&mut self,
385+
result: std::io::Result<T>,
386+
) -> InterpResult<'tcx, T> {
387+
match result {
388+
Ok(ok) => Ok(ok),
389+
Err(e) => {
390+
self.eval_context_mut().set_last_error_from_io_error(e)?;
391+
Ok((-1).into())
392+
}
393+
}
394+
}
376395
}

src/shims/fs.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
107107
fh.low
108108
});
109109

110-
this.consume_result(fd)
110+
this.set_last_error_from_io_result(fd)
111111
}
112112

113113
fn fcntl(
@@ -143,7 +143,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
143143
let fd = this.read_scalar(fd_op)?.to_i32()?;
144144

145145
this.remove_handle_and(fd, |handle, this| {
146-
this.consume_result(handle.file.sync_all().map(|_| 0i32))
146+
this.set_last_error_from_io_result(handle.file.sync_all().map(|_| 0i32))
147147
})
148148
}
149149

@@ -178,7 +178,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
178178
});
179179
// Reinsert the file handle
180180
this.machine.file_handler.handles.insert(fd, handle);
181-
this.consume_result(bytes?.map(|bytes| bytes as i64))
181+
this.set_last_error_from_io_result(bytes?.map(|bytes| bytes as i64))
182182
})
183183
}
184184

@@ -209,7 +209,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
209209
.map(|bytes| handle.file.write(bytes).map(|bytes| bytes as i64))
210210
});
211211
this.machine.file_handler.handles.insert(fd, handle);
212-
this.consume_result(bytes?)
212+
this.set_last_error_from_io_result(bytes?)
213213
})
214214
}
215215

@@ -226,7 +226,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
226226

227227
let result = remove_file(path).map(|_| 0);
228228

229-
this.consume_result(result)
229+
this.set_last_error_from_io_result(result)
230230
}
231231

232232
/// Helper function that gets a `FileHandle` immutable reference and allows to manipulate it
@@ -274,23 +274,4 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
274274
Ok((-1).into())
275275
}
276276
}
277-
278-
/// Helper function that consumes an `std::io::Result<T>` and returns an
279-
/// `InterpResult<'tcx,T>::Ok` instead. It is expected that the result can be converted to an
280-
/// OS error using `std::io::Error::raw_os_error`.
281-
///
282-
/// This function uses `T: From<i32>` instead of `i32` directly because some IO related
283-
/// functions return different integer types (like `read`, that returns an `i64`)
284-
fn consume_result<T: From<i32>>(
285-
&mut self,
286-
result: std::io::Result<T>,
287-
) -> InterpResult<'tcx, T> {
288-
match result {
289-
Ok(ok) => Ok(ok),
290-
Err(e) => {
291-
self.eval_context_mut().set_last_error_from_io_error(e)?;
292-
Ok((-1).into())
293-
}
294-
}
295-
}
296277
}

0 commit comments

Comments
 (0)