Use i64 directly

This commit is contained in:
Aviv Palivoda
2019-10-17 18:51:28 +03:00
parent 37f01fd9ae
commit 5cde75bec8
2 changed files with 12 additions and 15 deletions

View File

@@ -9,7 +9,7 @@ use std::io::SeekFrom;
use num_traits::ToPrimitive;
use super::os;
use crate::function::{OptionalArg, PyFuncArgs};
use crate::function::{OptionalArg, OptionalOption, PyFuncArgs};
use crate::obj::objbytearray::PyByteArray;
use crate::obj::objbytes;
use crate::obj::objbytes::PyBytes;
@@ -21,11 +21,8 @@ use crate::pyobject::TypeProtocol;
use crate::pyobject::{BufferProtocol, Either, PyObjectRef, PyRef, PyResult, PyValue};
use crate::vm::VirtualMachine;
fn byte_count(bytes: OptionalArg<Option<PyObjectRef>>) -> i64 {
match bytes {
OptionalArg::Present(Some(ref int)) => objint::get_value(int).to_i64().unwrap(),
_ => (-1 as i64),
}
fn byte_count(bytes: OptionalOption<i64>) -> i64 {
bytes.flat_option().unwrap_or(-1 as i64)
}
#[derive(Debug)]
@@ -134,7 +131,7 @@ impl PyStringIORef {
//Read k bytes from the object and return.
//If k is undefined || k == -1, then we read all bytes until the end of the file.
//This also increments the stream position by the value of k
fn read(self, bytes: OptionalArg<Option<PyObjectRef>>, vm: &VirtualMachine) -> PyResult {
fn read(self, bytes: OptionalOption<i64>, vm: &VirtualMachine) -> PyResult {
let data = match self.buffer.borrow_mut().read(byte_count(bytes)) {
Some(value) => value,
None => Vec::new(),
@@ -193,7 +190,7 @@ impl PyBytesIORef {
//Takes an integer k (bytes) and returns them from the underlying buffer
//If k is undefined || k == -1, then we read all bytes until the end of the file.
//This also increments the stream position by the value of k
fn read(self, bytes: OptionalArg<Option<PyObjectRef>>, vm: &VirtualMachine) -> PyResult {
fn read(self, bytes: OptionalOption<i64>, vm: &VirtualMachine) -> PyResult {
match self.buffer.borrow_mut().read(byte_count(bytes)) {
Some(value) => Ok(vm.ctx.new_bytes(value)),
None => Err(vm.new_value_error("Error Retrieving Value".to_string())),

View File

@@ -334,17 +334,17 @@ fn os_error(message: OptionalArg<PyStringRef>, vm: &VirtualMachine) -> PyResult
Err(vm.new_os_error(msg))
}
fn os_fsync(fd: PyIntRef, vm: &VirtualMachine) -> PyResult<()> {
let file = rust_file(fd.as_bigint().to_i64().unwrap());
fn os_fsync(fd: i64, vm: &VirtualMachine) -> PyResult<()> {
let file = rust_file(fd);
file.sync_all().map_err(|err| convert_io_error(vm, err))?;
// Avoid closing the fd
raw_file_number(file);
Ok(())
}
fn os_read(fd: PyIntRef, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
let mut buffer = vec![0u8; n.as_bigint().to_usize().unwrap()];
let mut file = rust_file(fd.as_bigint().to_i64().unwrap());
fn os_read(fd: i64, n: usize, vm: &VirtualMachine) -> PyResult {
let mut buffer = vec![0u8; n];
let mut file = rust_file(fd);
file.read_exact(&mut buffer)
.map_err(|err| convert_io_error(vm, err))?;
@@ -353,8 +353,8 @@ fn os_read(fd: PyIntRef, n: PyIntRef, vm: &VirtualMachine) -> PyResult {
Ok(vm.ctx.new_bytes(buffer))
}
fn os_write(fd: PyIntRef, data: PyBytesRef, vm: &VirtualMachine) -> PyResult {
let mut file = rust_file(fd.as_bigint().to_i64().unwrap());
fn os_write(fd: i64, data: PyBytesRef, vm: &VirtualMachine) -> PyResult {
let mut file = rust_file(fd);
let written = file.write(&data).map_err(|err| convert_io_error(vm, err))?;
// Avoid closing the fd