Skip to content

Commit 36204f2

Browse files
Merge pull request RustPython#676 from adrian17/master
Use Into<BigInt> instead of ToBigInt to avoid copies
2 parents b50ecf1 + 6b1598d commit 36204f2

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

vm/src/obj/objint.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ pub struct PyInt {
2424
pub type PyIntRef = PyRef<PyInt>;
2525

2626
impl PyInt {
27-
pub fn new<T: ToBigInt>(i: T) -> Self {
28-
PyInt {
29-
// TODO: this .clone()s a BigInt, which is not what we want.
30-
value: i.to_bigint().unwrap(),
31-
}
27+
pub fn new<T: Into<BigInt>>(i: T) -> Self {
28+
PyInt { value: i.into() }
29+
}
30+
}
31+
32+
impl IntoPyObject for BigInt {
33+
fn into_pyobject(self, ctx: &PyContext) -> PyResult {
34+
Ok(ctx.new_int(self))
3235
}
3336
}
3437

@@ -315,8 +318,8 @@ impl PyIntRef {
315318
}
316319
}
317320

318-
fn neg(self, vm: &mut VirtualMachine) -> PyObjectRef {
319-
vm.ctx.new_int(-(&self.value))
321+
fn neg(self, _vm: &mut VirtualMachine) -> BigInt {
322+
-(&self.value)
320323
}
321324

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

328-
fn abs(self, vm: &mut VirtualMachine) -> PyObjectRef {
329-
vm.ctx.new_int(self.value.abs())
331+
fn abs(self, _vm: &mut VirtualMachine) -> BigInt {
332+
self.value.abs()
330333
}
331334

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

340-
fn invert(self, vm: &mut VirtualMachine) -> PyObjectRef {
341-
vm.ctx.new_int(!(&self.value))
343+
fn invert(self, _vm: &mut VirtualMachine) -> BigInt {
344+
!(&self.value)
342345
}
343346

344347
fn repr(self, _vm: &mut VirtualMachine) -> String {

vm/src/pyobject.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::ptr;
1010
use std::rc::Rc;
1111

1212
use num_bigint::BigInt;
13-
use num_bigint::ToBigInt;
1413
use num_complex::Complex64;
1514
use num_traits::{One, Zero};
1615

@@ -508,7 +507,7 @@ impl PyContext {
508507
self.new_instance(self.object(), None)
509508
}
510509

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

vm/src/vm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::pyobject::{
3232
};
3333
use crate::stdlib;
3434
use crate::sysmodule;
35-
use num_bigint::ToBigInt;
35+
use num_bigint::BigInt;
3636

3737
// use objects::objects;
3838

@@ -109,7 +109,7 @@ impl VirtualMachine {
109109
}
110110

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

0 commit comments

Comments
 (0)