diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index e796efb472..29b8af28e9 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -560,55 +560,42 @@ macro_rules! atomic_func { } impl AsNumber for PyFloat { - const AS_NUMBER: PyNumberMethods = PyNumberMethods { - add: atomic_func!(|num, other, vm| Self::number_float_op(num, other, |a, b| a + b, vm)), - subtract: atomic_func!(|num, other, vm| Self::number_float_op( - num, - other, - |a, b| a - b, - vm - )), - multiply: atomic_func!(|num, other, vm| Self::number_float_op( - num, - other, - |a, b| a * b, - vm - )), - remainder: atomic_func!(|num, other, vm| Self::number_general_op( - num, other, inner_mod, vm - )), - divmod: atomic_func!(|num, other, vm| Self::number_general_op( - num, - other, - inner_divmod, - vm - )), - power: atomic_func!(|num, other, vm| Self::number_general_op(num, other, float_pow, vm)), - negative: atomic_func!(|num, vm| { - let value = Self::number_downcast(num).value; - (-value).to_pyresult(vm) - }), - positive: atomic_func!(|num, vm| Self::number_float(num, vm).to_pyresult(vm)), - absolute: atomic_func!(|num, vm| { - let value = Self::number_downcast(num).value; - value.abs().to_pyresult(vm) - }), - boolean: atomic_func!(|num, _vm| Ok(Self::number_downcast(num).value.is_zero())), - int: atomic_func!(|num, vm| { - let value = Self::number_downcast(num).value; - try_to_bigint(value, vm).map(|x| vm.ctx.new_int(x)) - }), - float: atomic_func!(|num, vm| Ok(Self::number_float(num, vm))), - floor_divide: atomic_func!(|num, other, vm| { - Self::number_general_op(num, other, inner_floordiv, vm) - }), - true_divide: atomic_func!(|num, other, vm| { - Self::number_general_op(num, other, inner_div, vm) - }), - ..PyNumberMethods::NOT_IMPLEMENTED - }; + fn as_number() -> &'static PyNumberMethods { + &AS_NUMBER + } } +static AS_NUMBER: PyNumberMethods = PyNumberMethods { + add: atomic_func!(|num, other, vm| PyFloat::number_float_op(num, other, |a, b| a + b, vm)), + subtract: atomic_func!(|num, other, vm| PyFloat::number_float_op(num, other, |a, b| a - b, vm)), + multiply: atomic_func!(|num, other, vm| PyFloat::number_float_op(num, other, |a, b| a * b, vm)), + remainder: atomic_func!(|num, other, vm| PyFloat::number_general_op(num, other, inner_mod, vm)), + divmod: atomic_func!(|num, other, vm| PyFloat::number_general_op(num, other, inner_divmod, vm)), + power: atomic_func!(|num, other, vm| PyFloat::number_general_op(num, other, float_pow, vm)), + negative: atomic_func!(|num, vm| { + let value = PyFloat::number_downcast(num).value; + (-value).to_pyresult(vm) + }), + positive: atomic_func!(|num, vm| PyFloat::number_float(num, vm).to_pyresult(vm)), + absolute: atomic_func!(|num, vm| { + let value = PyFloat::number_downcast(num).value; + value.abs().to_pyresult(vm) + }), + boolean: atomic_func!(|num, _vm| Ok(PyFloat::number_downcast(num).value.is_zero())), + int: atomic_func!(|num, vm| { + let value = PyFloat::number_downcast(num).value; + try_to_bigint(value, vm).map(|x| vm.ctx.new_int(x)) + }), + float: atomic_func!(|num, vm| Ok(PyFloat::number_float(num, vm))), + floor_divide: atomic_func!(|num, other, vm| { + PyFloat::number_general_op(num, other, inner_floordiv, vm) + }), + true_divide: atomic_func!(|num, other, vm| { + PyFloat::number_general_op(num, other, inner_div, vm) + }), + ..PyNumberMethods::NOT_IMPLEMENTED +}; + impl PyFloat { fn number_general_op( number: &PyNumber, diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index 5cecce848c..2b98155e4f 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -763,56 +763,43 @@ macro_rules! atomic_func { } impl AsNumber for PyInt { - const AS_NUMBER: PyNumberMethods = PyNumberMethods { - add: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a + b, vm)), - subtract: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a - b, vm)), - multiply: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a * b, vm)), - remainder: atomic_func!(|num, other, vm| Self::number_general_op( - num, other, inner_mod, vm - )), - divmod: atomic_func!(|num, other, vm| Self::number_general_op( - num, - other, - inner_divmod, - vm - )), - power: atomic_func!(|num, other, vm| Self::number_general_op(num, other, inner_pow, vm)), - negative: atomic_func!(|num, vm| (&Self::number_downcast(num).value).neg().to_pyresult(vm)), - positive: atomic_func!(|num, vm| Ok(Self::number_int(num, vm).into())), - absolute: atomic_func!(|num, vm| Self::number_downcast(num).value.abs().to_pyresult(vm)), - boolean: atomic_func!(|num, _vm| Ok(Self::number_downcast(num).value.is_zero())), - invert: atomic_func!(|num, vm| (&Self::number_downcast(num).value).not().to_pyresult(vm)), - lshift: atomic_func!(|num, other, vm| Self::number_general_op( - num, - other, - inner_lshift, - vm - )), - rshift: atomic_func!(|num, other, vm| Self::number_general_op( - num, - other, - inner_rshift, - vm - )), - and: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a & b, vm)), - xor: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a ^ b, vm)), - or: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a | b, vm)), - int: atomic_func!(|num, other| Ok(Self::number_int(num, other))), - float: atomic_func!(|num, vm| { - let zelf = Self::number_downcast(num); - try_to_float(&zelf.value, vm).map(|x| vm.ctx.new_float(x)) - }), - floor_divide: atomic_func!(|num, other, vm| { - Self::number_general_op(num, other, inner_floordiv, vm) - }), - true_divide: atomic_func!(|num, other, vm| { - Self::number_general_op(num, other, inner_truediv, vm) - }), - index: atomic_func!(|num, vm| Ok(Self::number_int(num, vm))), - ..PyNumberMethods::NOT_IMPLEMENTED - }; + fn as_number() -> &'static PyNumberMethods { + &AS_NUMBER + } } +static AS_NUMBER: PyNumberMethods = PyNumberMethods { + add: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a + b, vm)), + subtract: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a - b, vm)), + multiply: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a * b, vm)), + remainder: atomic_func!(|num, other, vm| PyInt::number_general_op(num, other, inner_mod, vm)), + divmod: atomic_func!(|num, other, vm| PyInt::number_general_op(num, other, inner_divmod, vm)), + power: atomic_func!(|num, other, vm| PyInt::number_general_op(num, other, inner_pow, vm)), + negative: atomic_func!(|num, vm| (&PyInt::number_downcast(num).value).neg().to_pyresult(vm)), + positive: atomic_func!(|num, vm| Ok(PyInt::number_int(num, vm).into())), + absolute: atomic_func!(|num, vm| PyInt::number_downcast(num).value.abs().to_pyresult(vm)), + boolean: atomic_func!(|num, _vm| Ok(PyInt::number_downcast(num).value.is_zero())), + invert: atomic_func!(|num, vm| (&PyInt::number_downcast(num).value).not().to_pyresult(vm)), + lshift: atomic_func!(|num, other, vm| PyInt::number_general_op(num, other, inner_lshift, vm)), + rshift: atomic_func!(|num, other, vm| PyInt::number_general_op(num, other, inner_rshift, vm)), + and: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a & b, vm)), + xor: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a ^ b, vm)), + or: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a | b, vm)), + int: atomic_func!(|num, other| Ok(PyInt::number_int(num, other))), + float: atomic_func!(|num, vm| { + let zelf = PyInt::number_downcast(num); + try_to_float(&zelf.value, vm).map(|x| vm.ctx.new_float(x)) + }), + floor_divide: atomic_func!(|num, other, vm| { + PyInt::number_general_op(num, other, inner_floordiv, vm) + }), + true_divide: atomic_func!(|num, other, vm| { + PyInt::number_general_op(num, other, inner_truediv, vm) + }), + index: atomic_func!(|num, vm| Ok(PyInt::number_int(num, vm))), + ..PyNumberMethods::NOT_IMPLEMENTED +}; + impl PyInt { fn number_general_op( number: &PyNumber, diff --git a/vm/src/types/slot.rs b/vm/src/types/slot.rs index 85df5b4854..8a0c31b891 100644 --- a/vm/src/types/slot.rs +++ b/vm/src/types/slot.rs @@ -871,13 +871,15 @@ pub trait AsSequence: PyPayload { #[pyimpl] pub trait AsNumber: PyPayload { - const AS_NUMBER: PyNumberMethods; + // const AS_NUMBER: PyNumberMethods; - #[inline] #[pyslot] - fn as_number() -> &'static PyNumberMethods { - &Self::AS_NUMBER - } + fn as_number() -> &'static PyNumberMethods; + // #[inline] + // #[pyslot] + // fn as_number() -> &'static PyNumberMethods { + // &Self::AS_NUMBER + // } fn number_downcast<'a>(number: &'a PyNumber) -> &'a Py { unsafe { number.obj.downcast_unchecked_ref() }