Skip to content

Commit c17951e

Browse files
committed
Refactor AsNumber for PyFloat
1 parent f0a29de commit c17951e

File tree

1 file changed

+21
-48
lines changed

1 file changed

+21
-48
lines changed

vm/src/builtins/float.rs

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -546,41 +546,34 @@ impl Hashable for PyFloat {
546546
impl AsNumber for PyFloat {
547547
fn as_number() -> &'static PyNumberMethods {
548548
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
549-
add: atomic_func!(|num, other, vm| PyFloat::number_float_op(
549+
add: atomic_func!(|num, other, vm| PyFloat::number_op(
550550
num,
551551
other,
552-
|a, b| a + b,
552+
|a, b, _vm| a + b,
553553
vm
554554
)),
555-
subtract: atomic_func!(|num, other, vm| PyFloat::number_float_op(
555+
subtract: atomic_func!(|num, other, vm| PyFloat::number_op(
556556
num,
557557
other,
558-
|a, b| a - b,
558+
|a, b, _vm| a - b,
559559
vm
560560
)),
561-
multiply: atomic_func!(|num, other, vm| PyFloat::number_float_op(
561+
multiply: atomic_func!(|num, other, vm| PyFloat::number_op(
562562
num,
563563
other,
564-
|a, b| a * b,
564+
|a, b, _vm| a * b,
565565
vm
566566
)),
567-
remainder: atomic_func!(|num, other, vm| PyFloat::number_general_op(
568-
num, other, inner_mod, vm
569-
)),
570-
divmod: atomic_func!(|num, other, vm| PyFloat::number_general_op(
571-
num,
572-
other,
573-
inner_divmod,
574-
vm
575-
)),
576-
power: atomic_func!(|num, other, vm| PyFloat::number_general_op(
577-
num, other, float_pow, vm
578-
)),
567+
remainder: atomic_func!(|num, other, vm| PyFloat::number_op(num, other, inner_mod, vm)),
568+
divmod: atomic_func!(|num, other, vm| PyFloat::number_op(num, other, inner_divmod, vm)),
569+
power: atomic_func!(|num, other, vm| PyFloat::number_op(num, other, float_pow, vm)),
579570
negative: atomic_func!(|num, vm| {
580571
let value = PyFloat::number_downcast(num).value;
581572
(-value).to_pyresult(vm)
582573
}),
583-
positive: atomic_func!(|num, vm| PyFloat::number_float(num, vm).to_pyresult(vm)),
574+
positive: atomic_func!(
575+
|num, vm| PyFloat::number_downcast_exact(num, vm).to_pyresult(vm)
576+
),
584577
absolute: atomic_func!(|num, vm| {
585578
let value = PyFloat::number_downcast(num).value;
586579
value.abs().to_pyresult(vm)
@@ -590,26 +583,26 @@ impl AsNumber for PyFloat {
590583
let value = PyFloat::number_downcast(num).value;
591584
try_to_bigint(value, vm).map(|x| vm.ctx.new_int(x))
592585
}),
593-
float: atomic_func!(|num, vm| Ok(PyFloat::number_float(num, vm))),
586+
float: atomic_func!(|num, vm| Ok(PyFloat::number_downcast_exact(num, vm))),
594587
floor_divide: atomic_func!(|num, other, vm| {
595-
PyFloat::number_general_op(num, other, inner_floordiv, vm)
588+
PyFloat::number_op(num, other, inner_floordiv, vm)
596589
}),
597590
true_divide: atomic_func!(|num, other, vm| {
598-
PyFloat::number_general_op(num, other, inner_div, vm)
591+
PyFloat::number_op(num, other, inner_div, vm)
599592
}),
600593
..PyNumberMethods::NOT_IMPLEMENTED
601594
});
602595
&AS_NUMBER
603596
}
597+
598+
#[inline]
599+
fn clone_exact(zelf: &Py<Self>, vm: &VirtualMachine) -> PyRef<Self> {
600+
vm.ctx.new_float(zelf.value)
601+
}
604602
}
605603

606604
impl PyFloat {
607-
fn number_general_op<F, R>(
608-
number: PyNumber,
609-
other: &PyObject,
610-
op: F,
611-
vm: &VirtualMachine,
612-
) -> PyResult
605+
fn number_op<F, R>(number: PyNumber, other: &PyObject, op: F, vm: &VirtualMachine) -> PyResult
613606
where
614607
F: FnOnce(f64, f64, &VirtualMachine) -> R,
615608
R: ToPyResult,
@@ -620,26 +613,6 @@ impl PyFloat {
620613
Ok(vm.ctx.not_implemented())
621614
}
622615
}
623-
624-
fn number_float_op<F>(
625-
number: PyNumber,
626-
other: &PyObject,
627-
op: F,
628-
vm: &VirtualMachine,
629-
) -> PyResult
630-
where
631-
F: FnOnce(f64, f64) -> f64,
632-
{
633-
Self::number_general_op(number, other, |a, b, _vm| op(a, b), vm)
634-
}
635-
636-
fn number_float(number: PyNumber, vm: &VirtualMachine) -> PyRef<PyFloat> {
637-
if let Some(zelf) = number.obj.downcast_ref_if_exact::<Self>(vm) {
638-
zelf.to_owned()
639-
} else {
640-
vm.ctx.new_float(Self::number_downcast(number).value)
641-
}
642-
}
643616
}
644617

645618
// Retrieve inner float value:

0 commit comments

Comments
 (0)