Refactor AsNumber for PyFloat

This commit is contained in:
Jeong YunWon
2023-03-05 22:27:45 +09:00
parent f0a29de87d
commit c17951e3f6

View File

@@ -546,41 +546,34 @@ impl Hashable for PyFloat {
impl AsNumber for PyFloat {
fn as_number() -> &'static PyNumberMethods {
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
add: atomic_func!(|num, other, vm| PyFloat::number_float_op(
add: atomic_func!(|num, other, vm| PyFloat::number_op(
num,
other,
|a, b| a + b,
|a, b, _vm| a + b,
vm
)),
subtract: atomic_func!(|num, other, vm| PyFloat::number_float_op(
subtract: atomic_func!(|num, other, vm| PyFloat::number_op(
num,
other,
|a, b| a - b,
|a, b, _vm| a - b,
vm
)),
multiply: atomic_func!(|num, other, vm| PyFloat::number_float_op(
multiply: atomic_func!(|num, other, vm| PyFloat::number_op(
num,
other,
|a, b| a * b,
|a, b, _vm| 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
)),
remainder: atomic_func!(|num, other, vm| PyFloat::number_op(num, other, inner_mod, vm)),
divmod: atomic_func!(|num, other, vm| PyFloat::number_op(num, other, inner_divmod, vm)),
power: atomic_func!(|num, other, vm| PyFloat::number_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)),
positive: atomic_func!(
|num, vm| PyFloat::number_downcast_exact(num, vm).to_pyresult(vm)
),
absolute: atomic_func!(|num, vm| {
let value = PyFloat::number_downcast(num).value;
value.abs().to_pyresult(vm)
@@ -590,26 +583,26 @@ impl AsNumber for PyFloat {
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))),
float: atomic_func!(|num, vm| Ok(PyFloat::number_downcast_exact(num, vm))),
floor_divide: atomic_func!(|num, other, vm| {
PyFloat::number_general_op(num, other, inner_floordiv, vm)
PyFloat::number_op(num, other, inner_floordiv, vm)
}),
true_divide: atomic_func!(|num, other, vm| {
PyFloat::number_general_op(num, other, inner_div, vm)
PyFloat::number_op(num, other, inner_div, vm)
}),
..PyNumberMethods::NOT_IMPLEMENTED
});
&AS_NUMBER
}
#[inline]
fn clone_exact(zelf: &Py<Self>, vm: &VirtualMachine) -> PyRef<Self> {
vm.ctx.new_float(zelf.value)
}
}
impl PyFloat {
fn number_general_op<F, R>(
number: PyNumber,
other: &PyObject,
op: F,
vm: &VirtualMachine,
) -> PyResult
fn number_op<F, R>(number: PyNumber, other: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
where
F: FnOnce(f64, f64, &VirtualMachine) -> R,
R: ToPyResult,
@@ -620,26 +613,6 @@ impl PyFloat {
Ok(vm.ctx.not_implemented())
}
}
fn number_float_op<F>(
number: PyNumber,
other: &PyObject,
op: F,
vm: &VirtualMachine,
) -> PyResult
where
F: FnOnce(f64, f64) -> f64,
{
Self::number_general_op(number, other, |a, b, _vm| op(a, b), vm)
}
fn number_float(number: PyNumber, vm: &VirtualMachine) -> PyRef<PyFloat> {
if let Some(zelf) = number.obj.downcast_ref_if_exact::<Self>(vm) {
zelf.to_owned()
} else {
vm.ctx.new_float(Self::number_downcast(number).value)
}
}
}
// Retrieve inner float value: