diff --git a/vm/src/obj/objint.rs b/vm/src/obj/objint.rs index dbabe79907..107ec7e309 100644 --- a/vm/src/obj/objint.rs +++ b/vm/src/obj/objint.rs @@ -24,11 +24,14 @@ pub struct PyInt { pub type PyIntRef = PyRef; impl PyInt { - pub fn new(i: T) -> Self { - PyInt { - // TODO: this .clone()s a BigInt, which is not what we want. - value: i.to_bigint().unwrap(), - } + pub fn new>(i: T) -> Self { + PyInt { value: i.into() } + } +} + +impl IntoPyObject for BigInt { + fn into_pyobject(self, ctx: &PyContext) -> PyResult { + Ok(ctx.new_int(self)) } } @@ -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 { @@ -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, _vm: &mut VirtualMachine) -> Self { @@ -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 { diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 0535779487..303d0cc695 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -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}; @@ -508,7 +507,7 @@ impl PyContext { self.new_instance(self.object(), None) } - pub fn new_int(&self, i: T) -> PyObjectRef { + pub fn new_int>(&self, i: T) -> PyObjectRef { PyObject::new(PyInt::new(i), self.int_type()) } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index d57d0e4388..26e6e57bae 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -32,7 +32,7 @@ use crate::pyobject::{ }; use crate::stdlib; use crate::sysmodule; -use num_bigint::ToBigInt; +use num_bigint::BigInt; // use objects::objects; @@ -109,7 +109,7 @@ impl VirtualMachine { } /// Create a new python int object. - pub fn new_int(&self, i: T) -> PyObjectRef { + pub fn new_int>(&self, i: T) -> PyObjectRef { self.ctx.new_int(i) }