Make PyNumberMethods static

This commit is contained in:
Jeong YunWon committed 2023-03-10 01:17:28 +09:00
1 parent 1fea829760
commit 788ccffb2b
2 files changed
+32 -55

No files matched your search

+3 -28
View File
@@ -11,7 +11,6 @@ use crate::{
};
use num_bigint::Sign;
use num_traits::Zero;
use once_cell::sync::Lazy;
use std::fmt::{Debug, Formatter};
impl ToPyObject for bool {
@@ -172,28 +171,9 @@ impl PyBool {
}
}
macro_rules! int_method {
($method:ident) => {
PyInt::as_number().$method
};
}
impl AsNumber for PyBool {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
add: int_method!(add),
subtract: int_method!(subtract),
multiply: int_method!(multiply),
remainder: int_method!(remainder),
divmod: int_method!(divmod),
power: int_method!(power),
negative: int_method!(negative),
positive: int_method!(positive),
absolute: int_method!(absolute),
boolean: int_method!(boolean),
invert: int_method!(invert),
lshift: int_method!(lshift),
rshift: int_method!(rshift),
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
and: Some(|number, other, vm| {
PyBool::and(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
}),
@@ -203,13 +183,8 @@ impl AsNumber for PyBool {
or: Some(|number, other, vm| {
PyBool::or(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
}),
int: int_method!(int),
float: int_method!(float),
floor_divide: int_method!(floor_divide),
true_divide: int_method!(true_divide),
index: int_method!(index),
..PyNumberMethods::NOT_IMPLEMENTED
});
..PyInt::AS_NUMBER
};
&AS_NUMBER
}
}
+29 -27
View File
@@ -727,33 +727,7 @@ impl Hashable for PyInt {
impl AsNumber for PyInt {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
add: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a + b, vm)),
subtract: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a - b, vm)),
multiply: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a * b, vm)),
remainder: Some(|num, other, vm| PyInt::number_op(num, other, inner_mod, vm)),
divmod: Some(|num, other, vm| PyInt::number_op(num, other, inner_divmod, vm)),
power: Some(|num, other, vm| PyInt::number_op(num, other, inner_pow, vm)),
negative: Some(|num, vm| (&PyInt::number_downcast(num).value).neg().to_pyresult(vm)),
positive: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm).into())),
absolute: Some(|num, vm| PyInt::number_downcast(num).value.abs().to_pyresult(vm)),
boolean: Some(|num, _vm| Ok(PyInt::number_downcast(num).value.is_zero())),
invert: Some(|num, vm| (&PyInt::number_downcast(num).value).not().to_pyresult(vm)),
lshift: Some(|num, other, vm| PyInt::number_op(num, other, inner_lshift, vm)),
rshift: Some(|num, other, vm| PyInt::number_op(num, other, inner_rshift, vm)),
and: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a & b, vm)),
xor: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a ^ b, vm)),
or: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a | b, vm)),
int: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm))),
float: Some(|num, vm| {
let zelf = PyInt::number_downcast(num);
try_to_float(&zelf.value, vm).map(|x| vm.ctx.new_float(x))
}),
floor_divide: Some(|num, other, vm| PyInt::number_op(num, other, inner_floordiv, vm)),
true_divide: Some(|num, other, vm| PyInt::number_op(num, other, inner_truediv, vm)),
index: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm))),
..PyNumberMethods::NOT_IMPLEMENTED
};
static AS_NUMBER: PyNumberMethods = PyInt::AS_NUMBER;
&AS_NUMBER
}
@@ -764,6 +738,34 @@ impl AsNumber for PyInt {
}
impl PyInt {
pub(super) const AS_NUMBER: PyNumberMethods = PyNumberMethods {
add: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a + b, vm)),
subtract: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a - b, vm)),
multiply: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a * b, vm)),
remainder: Some(|num, other, vm| PyInt::number_op(num, other, inner_mod, vm)),
divmod: Some(|num, other, vm| PyInt::number_op(num, other, inner_divmod, vm)),
power: Some(|num, other, vm| PyInt::number_op(num, other, inner_pow, vm)),
negative: Some(|num, vm| (&PyInt::number_downcast(num).value).neg().to_pyresult(vm)),
positive: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm).into())),
absolute: Some(|num, vm| PyInt::number_downcast(num).value.abs().to_pyresult(vm)),
boolean: Some(|num, _vm| Ok(PyInt::number_downcast(num).value.is_zero())),
invert: Some(|num, vm| (&PyInt::number_downcast(num).value).not().to_pyresult(vm)),
lshift: Some(|num, other, vm| PyInt::number_op(num, other, inner_lshift, vm)),
rshift: Some(|num, other, vm| PyInt::number_op(num, other, inner_rshift, vm)),
and: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a & b, vm)),
xor: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a ^ b, vm)),
or: Some(|num, other, vm| PyInt::number_op(num, other, |a, b, _vm| a | b, vm)),
int: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm))),
float: Some(|num, vm| {
let zelf = PyInt::number_downcast(num);
try_to_float(&zelf.value, vm).map(|x| vm.ctx.new_float(x))
}),
floor_divide: Some(|num, other, vm| PyInt::number_op(num, other, inner_floordiv, vm)),
true_divide: Some(|num, other, vm| PyInt::number_op(num, other, inner_truediv, vm)),
index: Some(|num, vm| Ok(PyInt::number_downcast_exact(num, vm))),
..PyNumberMethods::NOT_IMPLEMENTED
};
fn number_op<F, R>(number: PyNumber, other: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
where
F: FnOnce(&BigInt, &BigInt, &VirtualMachine) -> R,