Skip to content

Use Into<BigInt> instead of ToBigInt to avoid copies #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions vm/src/obj/objint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ pub struct PyInt {
pub type PyIntRef = PyRef<PyInt>;

impl PyInt {
pub fn new<T: ToBigInt>(i: T) -> Self {
PyInt {
// TODO: this .clone()s a BigInt, which is not what we want.
value: i.to_bigint().unwrap(),
}
pub fn new<T: Into<BigInt>>(i: T) -> Self {
PyInt { value: i.into() }
}
}

impl IntoPyObject for BigInt {
fn into_pyobject(self, ctx: &PyContext) -> PyResult {
Ok(ctx.new_int(self))
}
}

Expand Down Expand Up @@ -315,8 +318,8 @@ impl PyIntRef {
}
}

fn neg(self, vm: &mut VirtualMachine) -> PyObjectRef {
vm.ctx.new_int(-(&self.value))
fn neg(self, _vm: &mut VirtualMachine) -> BigInt {
-(&self.value)
}

fn hash(self, _vm: &mut VirtualMachine) -> u64 {
Expand All @@ -325,8 +328,8 @@ impl PyIntRef {
hasher.finish()
}

fn abs(self, vm: &mut VirtualMachine) -> PyObjectRef {
vm.ctx.new_int(self.value.abs())
fn abs(self, _vm: &mut VirtualMachine) -> BigInt {
self.value.abs()
}

fn round(self, _precision: OptionalArg<PyObjectRef>, _vm: &mut VirtualMachine) -> Self {
Expand All @@ -337,8 +340,8 @@ impl PyIntRef {
self.value.to_f64().unwrap()
}

fn invert(self, vm: &mut VirtualMachine) -> PyObjectRef {
vm.ctx.new_int(!(&self.value))
fn invert(self, _vm: &mut VirtualMachine) -> BigInt {
!(&self.value)
}

fn repr(self, _vm: &mut VirtualMachine) -> String {
Expand Down
3 changes: 1 addition & 2 deletions vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::ptr;
use std::rc::Rc;

use num_bigint::BigInt;
use num_bigint::ToBigInt;
use num_complex::Complex64;
use num_traits::{One, Zero};

Expand Down Expand Up @@ -508,7 +507,7 @@ impl PyContext {
self.new_instance(self.object(), None)
}

pub fn new_int<T: ToBigInt>(&self, i: T) -> PyObjectRef {
pub fn new_int<T: Into<BigInt>>(&self, i: T) -> PyObjectRef {
PyObject::new(PyInt::new(i), self.int_type())
}

Expand Down
4 changes: 2 additions & 2 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::pyobject::{
};
use crate::stdlib;
use crate::sysmodule;
use num_bigint::ToBigInt;
use num_bigint::BigInt;

// use objects::objects;

Expand Down Expand Up @@ -109,7 +109,7 @@ impl VirtualMachine {
}

/// Create a new python int object.
pub fn new_int<T: ToBigInt>(&self, i: T) -> PyObjectRef {
pub fn new_int<T: Into<BigInt>>(&self, i: T) -> PyObjectRef {
self.ctx.new_int(i)
}

Expand Down