Skip to content

Commit 2f5a89a

Browse files
committed
Rename consume_result
1 parent 2b5b3cd commit 2f5a89a

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
@@ -370,4 +370,23 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
370370
Size::from_bits(32),
371371
))
372372
}
373+
374+
/// Helper function that consumes an `std::io::Result<T>` and returns an
375+
/// `InterpResult<'tcx,T>::Ok` instead. It is expected that the result can be converted to an
376+
/// OS error using `std::io::Error::raw_os_error`.
377+
///
378+
/// This function uses `T: From<i32>` instead of `i32` directly because some IO related
379+
/// functions return different integer types (like `read`, that returns an `i64`)
380+
fn set_last_error_from_io_result<T: From<i32>>(
381+
&mut self,
382+
result: std::io::Result<T>,
383+
) -> InterpResult<'tcx, T> {
384+
match result {
385+
Ok(ok) => Ok(ok),
386+
Err(e) => {
387+
self.eval_context_mut().set_last_error_from_io_error(e)?;
388+
Ok((-1).into())
389+
}
390+
}
391+
}
373392
}

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

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

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

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

223223
let result = remove_file(path).map(|_| 0);
224224

225-
this.consume_result(result)
225+
this.set_last_error_from_io_result(result)
226226
}
227227

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

0 commit comments

Comments
 (0)