Skip to content

Commit 5cde75b

Browse files
committed
Use i64 directly
1 parent 37f01fd commit 5cde75b

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

vm/src/stdlib/io.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::io::SeekFrom;
99
use num_traits::ToPrimitive;
1010

1111
use super::os;
12-
use crate::function::{OptionalArg, PyFuncArgs};
12+
use crate::function::{OptionalArg, OptionalOption, PyFuncArgs};
1313
use crate::obj::objbytearray::PyByteArray;
1414
use crate::obj::objbytes;
1515
use crate::obj::objbytes::PyBytes;
@@ -21,11 +21,8 @@ use crate::pyobject::TypeProtocol;
2121
use crate::pyobject::{BufferProtocol, Either, PyObjectRef, PyRef, PyResult, PyValue};
2222
use crate::vm::VirtualMachine;
2323

24-
fn byte_count(bytes: OptionalArg<Option<PyObjectRef>>) -> i64 {
25-
match bytes {
26-
OptionalArg::Present(Some(ref int)) => objint::get_value(int).to_i64().unwrap(),
27-
_ => (-1 as i64),
28-
}
24+
fn byte_count(bytes: OptionalOption<i64>) -> i64 {
25+
bytes.flat_option().unwrap_or(-1 as i64)
2926
}
3027

3128
#[derive(Debug)]
@@ -134,7 +131,7 @@ impl PyStringIORef {
134131
//Read k bytes from the object and return.
135132
//If k is undefined || k == -1, then we read all bytes until the end of the file.
136133
//This also increments the stream position by the value of k
137-
fn read(self, bytes: OptionalArg<Option<PyObjectRef>>, vm: &VirtualMachine) -> PyResult {
134+
fn read(self, bytes: OptionalOption<i64>, vm: &VirtualMachine) -> PyResult {
138135
let data = match self.buffer.borrow_mut().read(byte_count(bytes)) {
139136
Some(value) => value,
140137
None => Vec::new(),
@@ -193,7 +190,7 @@ impl PyBytesIORef {
193190
//Takes an integer k (bytes) and returns them from the underlying buffer
194191
//If k is undefined || k == -1, then we read all bytes until the end of the file.
195192
//This also increments the stream position by the value of k
196-
fn read(self, bytes: OptionalArg<Option<PyObjectRef>>, vm: &VirtualMachine) -> PyResult {
193+
fn read(self, bytes: OptionalOption<i64>, vm: &VirtualMachine) -> PyResult {
197194
match self.buffer.borrow_mut().read(byte_count(bytes)) {
198195
Some(value) => Ok(vm.ctx.new_bytes(value)),
199196
None => Err(vm.new_value_error("Error Retrieving Value".to_string())),

vm/src/stdlib/os.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,17 @@ fn os_error(message: OptionalArg<PyStringRef>, vm: &VirtualMachine) -> PyResult
334334
Err(vm.new_os_error(msg))
335335
}
336336

337-
fn os_fsync(fd: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
338-
let file = rust_file(fd.as_bigint().to_i64().unwrap());
337+
fn os_fsync(fd: i64, vm: &VirtualMachine) -> PyResult<()> {
338+
let file = rust_file(fd);
339339
file.sync_all().map_err(|err| convert_io_error(vm, err))?;
340340
// Avoid closing the fd
341341
raw_file_number(file);
342342
Ok(())
343343
}
344344

345-
fn os_read(fd: PyIntRef, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
346-
let mut buffer = vec![0u8; n.as_bigint().to_usize().unwrap()];
347-
let mut file = rust_file(fd.as_bigint().to_i64().unwrap());
345+
fn os_read(fd: i64, n: usize, vm: &VirtualMachine) -> PyResult {
346+
let mut buffer = vec![0u8; n];
347+
let mut file = rust_file(fd);
348348
file.read_exact(&mut buffer)
349349
.map_err(|err| convert_io_error(vm, err))?;
350350

@@ -353,8 +353,8 @@ fn os_read(fd: PyIntRef, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
353353
Ok(vm.ctx.new_bytes(buffer))
354354
}
355355

356-
fn os_write(fd: PyIntRef, data: PyBytesRef, vm: &VirtualMachine) -> PyResult {
357-
let mut file = rust_file(fd.as_bigint().to_i64().unwrap());
356+
fn os_write(fd: i64, data: PyBytesRef, vm: &VirtualMachine) -> PyResult {
357+
let mut file = rust_file(fd);
358358
let written = file.write(&data).map_err(|err| convert_io_error(vm, err))?;
359359

360360
// Avoid closing the fd

0 commit comments

Comments
 (0)