@@ -15,7 +15,7 @@ use crate::{
15
15
} ,
16
16
protocol:: { PyNumber , PyNumberMethods } ,
17
17
types:: { AsNumber , Comparable , Constructor , Hashable , PyComparisonOp , Representable } ,
18
- AsObject , Context , Py , PyObject , PyObjectRef , PyPayload , PyRef , PyResult ,
18
+ AsObject , Context , Py , PyObject , PyObjectRef , PyPayload , PyRef , PyRefExact , PyResult ,
19
19
TryFromBorrowedObject , VirtualMachine ,
20
20
} ;
21
21
use num_bigint:: { BigInt , Sign } ;
@@ -321,7 +321,7 @@ impl PyInt {
321
321
322
322
#[ pyclass(
323
323
flags( BASETYPE ) ,
324
- with( Comparable , Hashable , Constructor , AsNumber , Representable )
324
+ with( PyRef , Comparable , Hashable , Constructor , AsNumber , Representable )
325
325
) ]
326
326
impl PyInt {
327
327
#[ pymethod( name = "__radd__" ) ]
@@ -521,11 +521,6 @@ impl PyInt {
521
521
Ok ( zelf)
522
522
}
523
523
524
- #[ pymethod( magic) ]
525
- fn int ( zelf : PyRef < Self > ) -> PyRef < Self > {
526
- zelf
527
- }
528
-
529
524
#[ pymethod( magic) ]
530
525
fn pos ( & self ) -> BigInt {
531
526
self . value . clone ( )
@@ -537,23 +532,23 @@ impl PyInt {
537
532
}
538
533
539
534
#[ pymethod( magic) ]
540
- fn trunc ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRef < Self > {
541
- Self :: clone_if_subclass ( zelf, vm)
535
+ fn trunc ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRefExact < Self > {
536
+ zelf. int ( vm)
542
537
}
543
538
544
539
#[ pymethod( magic) ]
545
- fn floor ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRef < Self > {
546
- Self :: clone_if_subclass ( zelf, vm)
540
+ fn floor ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRefExact < Self > {
541
+ zelf. int ( vm)
547
542
}
548
543
549
544
#[ pymethod( magic) ]
550
- fn ceil ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRef < Self > {
551
- Self :: clone_if_subclass ( zelf, vm)
545
+ fn ceil ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRefExact < Self > {
546
+ zelf. int ( vm)
552
547
}
553
548
554
549
#[ pymethod( magic) ]
555
- fn index ( zelf : PyRef < Self > ) -> PyRef < Self > {
556
- zelf
550
+ fn index ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRefExact < Self > {
551
+ zelf. int ( vm )
557
552
}
558
553
559
554
#[ pymethod( magic) ]
@@ -589,8 +584,8 @@ impl PyInt {
589
584
}
590
585
591
586
#[ pymethod]
592
- fn conjugate ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRef < Self > {
593
- Self :: clone_if_subclass ( zelf, vm)
587
+ fn conjugate ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRefExact < Self > {
588
+ zelf. int ( vm)
594
589
}
595
590
596
591
#[ pyclassmethod]
@@ -659,18 +654,9 @@ impl PyInt {
659
654
Ok ( bytes. into ( ) )
660
655
}
661
656
662
- #[ inline]
663
- fn clone_if_subclass ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRef < Self > {
664
- if zelf. class ( ) . is ( vm. ctx . types . int_type ) {
665
- return zelf;
666
- }
667
-
668
- vm. ctx . new_bigint ( & zelf. value )
669
- }
670
-
671
657
#[ pygetset]
672
- fn real ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRef < Self > {
673
- Self :: clone_if_subclass ( zelf, vm)
658
+ fn real ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRefExact < Self > {
659
+ zelf. int ( vm)
674
660
}
675
661
676
662
#[ pygetset]
@@ -679,8 +665,8 @@ impl PyInt {
679
665
}
680
666
681
667
#[ pygetset]
682
- fn numerator ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRef < Self > {
683
- Self :: clone_if_subclass ( zelf, vm)
668
+ fn numerator ( zelf : PyRef < Self > , vm : & VirtualMachine ) -> PyRefExact < Self > {
669
+ zelf. int ( vm)
684
670
}
685
671
686
672
#[ pygetset]
@@ -701,6 +687,17 @@ impl PyInt {
701
687
}
702
688
}
703
689
690
+ #[ pyclass]
691
+ impl PyRef < PyInt > {
692
+ #[ pymethod( magic) ]
693
+ fn int ( self , vm : & VirtualMachine ) -> PyRefExact < PyInt > {
694
+ self . into_exact_or ( & vm. ctx , |zelf| unsafe {
695
+ // TODO: this is actually safe. we need better interface
696
+ PyRefExact :: new_unchecked ( vm. ctx . new_bigint ( & zelf. value ) )
697
+ } )
698
+ }
699
+ }
700
+
704
701
impl Comparable for PyInt {
705
702
fn cmp (
706
703
zelf : & Py < Self > ,
0 commit comments