11use super :: { float, PyByteArray , PyBytes , PyStr , PyStrRef , PyType , PyTypeRef } ;
22use crate :: {
3+ atomic_func,
34 bytesinner:: PyBytesInner ,
45 class:: PyClassImpl ,
56 common:: hash,
@@ -15,7 +16,6 @@ use crate::{
1516 TryFromBorrowedObject , VirtualMachine ,
1617} ;
1718use bstr:: ByteSlice ;
18- use crossbeam_utils:: atomic:: AtomicCell ;
1919use num_bigint:: { BigInt , BigUint , Sign } ;
2020use num_integer:: Integer ;
2121use num_traits:: { One , Pow , PrimInt , Signed , ToPrimitive , Zero } ;
@@ -756,50 +756,79 @@ impl Hashable for PyInt {
756756 }
757757}
758758
759- macro_rules! atomic_func {
760- ( $x: expr) => {
761- AtomicCell :: new( Some ( $x) )
762- } ;
763- }
764-
765759impl AsNumber for PyInt {
766760 fn as_number ( ) -> & ' static PyNumberMethods {
761+ static AS_NUMBER : PyNumberMethods = PyNumberMethods {
762+ add : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a + b, vm) ) ,
763+ subtract : atomic_func ! ( |num, other, vm| PyInt :: number_int_op(
764+ num,
765+ other,
766+ |a, b| a - b,
767+ vm
768+ ) ) ,
769+ multiply : atomic_func ! ( |num, other, vm| PyInt :: number_int_op(
770+ num,
771+ other,
772+ |a, b| a * b,
773+ vm
774+ ) ) ,
775+ remainder : atomic_func ! ( |num, other, vm| PyInt :: number_general_op(
776+ num, other, inner_mod, vm
777+ ) ) ,
778+ divmod : atomic_func ! ( |num, other, vm| PyInt :: number_general_op(
779+ num,
780+ other,
781+ inner_divmod,
782+ vm
783+ ) ) ,
784+ power : atomic_func ! ( |num, other, vm| PyInt :: number_general_op(
785+ num, other, inner_pow, vm
786+ ) ) ,
787+ negative : atomic_func ! ( |num, vm| ( & PyInt :: number_downcast( num) . value)
788+ . neg( )
789+ . to_pyresult( vm) ) ,
790+ positive : atomic_func ! ( |num, vm| Ok ( PyInt :: number_int( num, vm) . into( ) ) ) ,
791+ absolute : atomic_func ! ( |num, vm| PyInt :: number_downcast( num)
792+ . value
793+ . abs( )
794+ . to_pyresult( vm) ) ,
795+ boolean : atomic_func ! ( |num, _vm| Ok ( PyInt :: number_downcast( num) . value. is_zero( ) ) ) ,
796+ invert : atomic_func ! ( |num, vm| ( & PyInt :: number_downcast( num) . value)
797+ . not( )
798+ . to_pyresult( vm) ) ,
799+ lshift : atomic_func ! ( |num, other, vm| PyInt :: number_general_op(
800+ num,
801+ other,
802+ inner_lshift,
803+ vm
804+ ) ) ,
805+ rshift : atomic_func ! ( |num, other, vm| PyInt :: number_general_op(
806+ num,
807+ other,
808+ inner_rshift,
809+ vm
810+ ) ) ,
811+ and : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a & b, vm) ) ,
812+ xor : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a ^ b, vm) ) ,
813+ or : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a | b, vm) ) ,
814+ int : atomic_func ! ( |num, other| Ok ( PyInt :: number_int( num, other) ) ) ,
815+ float : atomic_func ! ( |num, vm| {
816+ let zelf = PyInt :: number_downcast( num) ;
817+ try_to_float( & zelf. value, vm) . map( |x| vm. ctx. new_float( x) )
818+ } ) ,
819+ floor_divide : atomic_func ! ( |num, other, vm| {
820+ PyInt :: number_general_op( num, other, inner_floordiv, vm)
821+ } ) ,
822+ true_divide : atomic_func ! ( |num, other, vm| {
823+ PyInt :: number_general_op( num, other, inner_truediv, vm)
824+ } ) ,
825+ index : atomic_func ! ( |num, vm| Ok ( PyInt :: number_int( num, vm) ) ) ,
826+ ..PyNumberMethods :: NOT_IMPLEMENTED
827+ } ;
767828 & AS_NUMBER
768829 }
769830}
770831
771- static AS_NUMBER : PyNumberMethods = PyNumberMethods {
772- add : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a + b, vm) ) ,
773- subtract : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a - b, vm) ) ,
774- multiply : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a * b, vm) ) ,
775- remainder : atomic_func ! ( |num, other, vm| PyInt :: number_general_op( num, other, inner_mod, vm) ) ,
776- divmod : atomic_func ! ( |num, other, vm| PyInt :: number_general_op( num, other, inner_divmod, vm) ) ,
777- power : atomic_func ! ( |num, other, vm| PyInt :: number_general_op( num, other, inner_pow, vm) ) ,
778- negative : atomic_func ! ( |num, vm| ( & PyInt :: number_downcast( num) . value) . neg( ) . to_pyresult( vm) ) ,
779- positive : atomic_func ! ( |num, vm| Ok ( PyInt :: number_int( num, vm) . into( ) ) ) ,
780- absolute : atomic_func ! ( |num, vm| PyInt :: number_downcast( num) . value. abs( ) . to_pyresult( vm) ) ,
781- boolean : atomic_func ! ( |num, _vm| Ok ( PyInt :: number_downcast( num) . value. is_zero( ) ) ) ,
782- invert : atomic_func ! ( |num, vm| ( & PyInt :: number_downcast( num) . value) . not( ) . to_pyresult( vm) ) ,
783- lshift : atomic_func ! ( |num, other, vm| PyInt :: number_general_op( num, other, inner_lshift, vm) ) ,
784- rshift : atomic_func ! ( |num, other, vm| PyInt :: number_general_op( num, other, inner_rshift, vm) ) ,
785- and : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a & b, vm) ) ,
786- xor : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a ^ b, vm) ) ,
787- or : atomic_func ! ( |num, other, vm| PyInt :: number_int_op( num, other, |a, b| a | b, vm) ) ,
788- int : atomic_func ! ( |num, other| Ok ( PyInt :: number_int( num, other) ) ) ,
789- float : atomic_func ! ( |num, vm| {
790- let zelf = PyInt :: number_downcast( num) ;
791- try_to_float( & zelf. value, vm) . map( |x| vm. ctx. new_float( x) )
792- } ) ,
793- floor_divide : atomic_func ! ( |num, other, vm| {
794- PyInt :: number_general_op( num, other, inner_floordiv, vm)
795- } ) ,
796- true_divide : atomic_func ! ( |num, other, vm| {
797- PyInt :: number_general_op( num, other, inner_truediv, vm)
798- } ) ,
799- index : atomic_func ! ( |num, vm| Ok ( PyInt :: number_int( num, vm) ) ) ,
800- ..PyNumberMethods :: NOT_IMPLEMENTED
801- } ;
802-
803832impl PyInt {
804833 fn number_general_op < F > (
805834 number : & PyNumber ,
0 commit comments