Skip to content

Commit 44daeef

Browse files
qingshi163youknowone
authored andcommitted
fix static lifetime
1 parent c244575 commit 44daeef

File tree

3 files changed

+76
-100
lines changed

3 files changed

+76
-100
lines changed

vm/src/builtins/float.rs

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -560,55 +560,42 @@ macro_rules! atomic_func {
560560
}
561561

562562
impl AsNumber for PyFloat {
563-
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
564-
add: atomic_func!(|num, other, vm| Self::number_float_op(num, other, |a, b| a + b, vm)),
565-
subtract: atomic_func!(|num, other, vm| Self::number_float_op(
566-
num,
567-
other,
568-
|a, b| a - b,
569-
vm
570-
)),
571-
multiply: atomic_func!(|num, other, vm| Self::number_float_op(
572-
num,
573-
other,
574-
|a, b| a * b,
575-
vm
576-
)),
577-
remainder: atomic_func!(|num, other, vm| Self::number_general_op(
578-
num, other, inner_mod, vm
579-
)),
580-
divmod: atomic_func!(|num, other, vm| Self::number_general_op(
581-
num,
582-
other,
583-
inner_divmod,
584-
vm
585-
)),
586-
power: atomic_func!(|num, other, vm| Self::number_general_op(num, other, float_pow, vm)),
587-
negative: atomic_func!(|num, vm| {
588-
let value = Self::number_downcast(num).value;
589-
(-value).to_pyresult(vm)
590-
}),
591-
positive: atomic_func!(|num, vm| Self::number_float(num, vm).to_pyresult(vm)),
592-
absolute: atomic_func!(|num, vm| {
593-
let value = Self::number_downcast(num).value;
594-
value.abs().to_pyresult(vm)
595-
}),
596-
boolean: atomic_func!(|num, _vm| Ok(Self::number_downcast(num).value.is_zero())),
597-
int: atomic_func!(|num, vm| {
598-
let value = Self::number_downcast(num).value;
599-
try_to_bigint(value, vm).map(|x| vm.ctx.new_int(x))
600-
}),
601-
float: atomic_func!(|num, vm| Ok(Self::number_float(num, vm))),
602-
floor_divide: atomic_func!(|num, other, vm| {
603-
Self::number_general_op(num, other, inner_floordiv, vm)
604-
}),
605-
true_divide: atomic_func!(|num, other, vm| {
606-
Self::number_general_op(num, other, inner_div, vm)
607-
}),
608-
..PyNumberMethods::NOT_IMPLEMENTED
609-
};
563+
fn as_number() -> &'static PyNumberMethods {
564+
&AS_NUMBER
565+
}
610566
}
611567

568+
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
569+
add: atomic_func!(|num, other, vm| PyFloat::number_float_op(num, other, |a, b| a + b, vm)),
570+
subtract: atomic_func!(|num, other, vm| PyFloat::number_float_op(num, other, |a, b| a - b, vm)),
571+
multiply: atomic_func!(|num, other, vm| PyFloat::number_float_op(num, other, |a, b| a * b, vm)),
572+
remainder: atomic_func!(|num, other, vm| PyFloat::number_general_op(num, other, inner_mod, vm)),
573+
divmod: atomic_func!(|num, other, vm| PyFloat::number_general_op(num, other, inner_divmod, vm)),
574+
power: atomic_func!(|num, other, vm| PyFloat::number_general_op(num, other, float_pow, vm)),
575+
negative: atomic_func!(|num, vm| {
576+
let value = PyFloat::number_downcast(num).value;
577+
(-value).to_pyresult(vm)
578+
}),
579+
positive: atomic_func!(|num, vm| PyFloat::number_float(num, vm).to_pyresult(vm)),
580+
absolute: atomic_func!(|num, vm| {
581+
let value = PyFloat::number_downcast(num).value;
582+
value.abs().to_pyresult(vm)
583+
}),
584+
boolean: atomic_func!(|num, _vm| Ok(PyFloat::number_downcast(num).value.is_zero())),
585+
int: atomic_func!(|num, vm| {
586+
let value = PyFloat::number_downcast(num).value;
587+
try_to_bigint(value, vm).map(|x| vm.ctx.new_int(x))
588+
}),
589+
float: atomic_func!(|num, vm| Ok(PyFloat::number_float(num, vm))),
590+
floor_divide: atomic_func!(|num, other, vm| {
591+
PyFloat::number_general_op(num, other, inner_floordiv, vm)
592+
}),
593+
true_divide: atomic_func!(|num, other, vm| {
594+
PyFloat::number_general_op(num, other, inner_div, vm)
595+
}),
596+
..PyNumberMethods::NOT_IMPLEMENTED
597+
};
598+
612599
impl PyFloat {
613600
fn number_general_op<F, R>(
614601
number: &PyNumber,

vm/src/builtins/int.rs

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -763,56 +763,43 @@ macro_rules! atomic_func {
763763
}
764764

765765
impl AsNumber for PyInt {
766-
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
767-
add: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a + b, vm)),
768-
subtract: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a - b, vm)),
769-
multiply: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a * b, vm)),
770-
remainder: atomic_func!(|num, other, vm| Self::number_general_op(
771-
num, other, inner_mod, vm
772-
)),
773-
divmod: atomic_func!(|num, other, vm| Self::number_general_op(
774-
num,
775-
other,
776-
inner_divmod,
777-
vm
778-
)),
779-
power: atomic_func!(|num, other, vm| Self::number_general_op(num, other, inner_pow, vm)),
780-
negative: atomic_func!(|num, vm| (&Self::number_downcast(num).value).neg().to_pyresult(vm)),
781-
positive: atomic_func!(|num, vm| Ok(Self::number_int(num, vm).into())),
782-
absolute: atomic_func!(|num, vm| Self::number_downcast(num).value.abs().to_pyresult(vm)),
783-
boolean: atomic_func!(|num, _vm| Ok(Self::number_downcast(num).value.is_zero())),
784-
invert: atomic_func!(|num, vm| (&Self::number_downcast(num).value).not().to_pyresult(vm)),
785-
lshift: atomic_func!(|num, other, vm| Self::number_general_op(
786-
num,
787-
other,
788-
inner_lshift,
789-
vm
790-
)),
791-
rshift: atomic_func!(|num, other, vm| Self::number_general_op(
792-
num,
793-
other,
794-
inner_rshift,
795-
vm
796-
)),
797-
and: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a & b, vm)),
798-
xor: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a ^ b, vm)),
799-
or: atomic_func!(|num, other, vm| Self::number_int_op(num, other, |a, b| a | b, vm)),
800-
int: atomic_func!(|num, other| Ok(Self::number_int(num, other))),
801-
float: atomic_func!(|num, vm| {
802-
let zelf = Self::number_downcast(num);
803-
try_to_float(&zelf.value, vm).map(|x| vm.ctx.new_float(x))
804-
}),
805-
floor_divide: atomic_func!(|num, other, vm| {
806-
Self::number_general_op(num, other, inner_floordiv, vm)
807-
}),
808-
true_divide: atomic_func!(|num, other, vm| {
809-
Self::number_general_op(num, other, inner_truediv, vm)
810-
}),
811-
index: atomic_func!(|num, vm| Ok(Self::number_int(num, vm))),
812-
..PyNumberMethods::NOT_IMPLEMENTED
813-
};
766+
fn as_number() -> &'static PyNumberMethods {
767+
&AS_NUMBER
768+
}
814769
}
815770

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+
816803
impl PyInt {
817804
fn number_general_op<F>(
818805
number: &PyNumber,

vm/src/types/slot.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -871,13 +871,15 @@ pub trait AsSequence: PyPayload {
871871

872872
#[pyimpl]
873873
pub trait AsNumber: PyPayload {
874-
const AS_NUMBER: PyNumberMethods;
874+
// const AS_NUMBER: PyNumberMethods;
875875

876-
#[inline]
877876
#[pyslot]
878-
fn as_number() -> &'static PyNumberMethods {
879-
&Self::AS_NUMBER
880-
}
877+
fn as_number() -> &'static PyNumberMethods;
878+
// #[inline]
879+
// #[pyslot]
880+
// fn as_number() -> &'static PyNumberMethods {
881+
// &Self::AS_NUMBER
882+
// }
881883

882884
fn number_downcast<'a>(number: &'a PyNumber) -> &'a Py<Self> {
883885
unsafe { number.obj.downcast_unchecked_ref() }

0 commit comments

Comments
 (0)