Skip to content

Commit e871a53

Browse files
committed
resolve conflict
1 parent 069c6c9 commit e871a53

File tree

10 files changed

+211
-130
lines changed

10 files changed

+211
-130
lines changed

vm/src/builtins/bytes.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::{
33
};
44
use crate::{
55
anystr::{self, AnyStr},
6+
atomic_func,
67
bytesinner::{
78
bytes_decode, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
89
ByteInnerSplitOptions, ByteInnerTranslateOptions, DecodeArgs, PyBytesInner,
@@ -608,14 +609,17 @@ impl AsSequence for PyBytes {
608609
}
609610

610611
impl AsNumber for PyBytes {
611-
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
612-
remainder: Some(|number, other, vm| {
613-
Self::number_downcast(number)
614-
.mod_(other.to_owned(), vm)
615-
.to_pyresult(vm)
616-
}),
617-
..PyNumberMethods::NOT_IMPLEMENTED
618-
};
612+
fn as_number() -> &'static PyNumberMethods {
613+
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
614+
remainder: atomic_func!(|number, other, vm| {
615+
PyBytes::number_downcast(number)
616+
.mod_(other.to_owned(), vm)
617+
.to_pyresult(vm)
618+
}),
619+
..PyNumberMethods::NOT_IMPLEMENTED
620+
};
621+
&AS_NUMBER
622+
}
619623
}
620624

621625
impl Hashable for PyBytes {

vm/src/builtins/complex.rs

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{float, PyStr, PyType, PyTypeRef};
22
use crate::{
3+
atomic_func,
34
class::PyClassImpl,
45
convert::{ToPyObject, ToPyResult},
56
function::{
@@ -421,30 +422,44 @@ impl Hashable for PyComplex {
421422
}
422423

423424
impl AsNumber for PyComplex {
424-
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
425-
add: Some(|number, other, vm| Self::number_complex_op(number, other, |a, b| a + b, vm)),
426-
subtract: Some(|number, other, vm| {
427-
Self::number_complex_op(number, other, |a, b| a - b, vm)
428-
}),
429-
multiply: Some(|number, other, vm| {
430-
Self::number_complex_op(number, other, |a, b| a * b, vm)
431-
}),
432-
power: Some(|number, other, vm| Self::number_general_op(number, other, inner_pow, vm)),
433-
negative: Some(|number, vm| {
434-
let value = Self::number_downcast(number).value;
435-
(-value).to_pyresult(vm)
436-
}),
437-
positive: Some(|number, vm| Self::number_complex(number, vm).to_pyresult(vm)),
438-
absolute: Some(|number, vm| {
439-
let value = Self::number_downcast(number).value;
440-
value.norm().to_pyresult(vm)
441-
}),
442-
boolean: Some(|number, _vm| Ok(Self::number_downcast(number).value.is_zero())),
443-
true_divide: Some(|number, other, vm| {
444-
Self::number_general_op(number, other, inner_div, vm)
445-
}),
446-
..PyNumberMethods::NOT_IMPLEMENTED
447-
};
425+
fn as_number() -> &'static PyNumberMethods {
426+
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
427+
add: atomic_func!(|number, other, vm| PyComplex::number_complex_op(
428+
number,
429+
other,
430+
|a, b| a + b,
431+
vm
432+
)),
433+
subtract: atomic_func!(|number, other, vm| {
434+
PyComplex::number_complex_op(number, other, |a, b| a - b, vm)
435+
}),
436+
multiply: atomic_func!(|number, other, vm| {
437+
PyComplex::number_complex_op(number, other, |a, b| a * b, vm)
438+
}),
439+
power: atomic_func!(|number, other, vm| PyComplex::number_general_op(
440+
number, other, inner_pow, vm
441+
)),
442+
negative: atomic_func!(|number, vm| {
443+
let value = PyComplex::number_downcast(number).value;
444+
(-value).to_pyresult(vm)
445+
}),
446+
positive: atomic_func!(
447+
|number, vm| PyComplex::number_complex(number, vm).to_pyresult(vm)
448+
),
449+
absolute: atomic_func!(|number, vm| {
450+
let value = PyComplex::number_downcast(number).value;
451+
value.norm().to_pyresult(vm)
452+
}),
453+
boolean: atomic_func!(|number, _vm| Ok(PyComplex::number_downcast(number)
454+
.value
455+
.is_zero())),
456+
true_divide: atomic_func!(|number, other, vm| {
457+
PyComplex::number_general_op(number, other, inner_div, vm)
458+
}),
459+
..PyNumberMethods::NOT_IMPLEMENTED
460+
};
461+
&AS_NUMBER
462+
}
448463
}
449464

450465
impl PyComplex {

vm/src/builtins/float.rs

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{
22
try_bigint_to_f64, PyByteArray, PyBytes, PyInt, PyIntRef, PyStr, PyStrRef, PyType, PyTypeRef,
33
};
44
use crate::{
5+
atomic_func,
56
class::PyClassImpl,
67
common::{float_ops, hash},
78
convert::{ToPyObject, ToPyResult},
@@ -16,7 +17,6 @@ use crate::{
1617
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
1718
TryFromBorrowedObject, TryFromObject, VirtualMachine,
1819
};
19-
use crossbeam_utils::atomic::AtomicCell;
2020
use num_bigint::{BigInt, ToBigInt};
2121
use num_complex::Complex64;
2222
use num_rational::Ratio;
@@ -553,49 +553,66 @@ impl Hashable for PyFloat {
553553
}
554554
}
555555

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

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-
599616
impl PyFloat {
600617
fn number_general_op<F, R>(
601618
number: &PyNumber,

vm/src/builtins/int.rs

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{float, PyByteArray, PyBytes, PyStr, PyStrRef, PyType, PyTypeRef};
22
use crate::{
3+
atomic_func,
34
bytesinner::PyBytesInner,
45
class::PyClassImpl,
56
common::hash,
@@ -15,7 +16,6 @@ use crate::{
1516
TryFromBorrowedObject, VirtualMachine,
1617
};
1718
use bstr::ByteSlice;
18-
use crossbeam_utils::atomic::AtomicCell;
1919
use num_bigint::{BigInt, BigUint, Sign};
2020
use num_integer::Integer;
2121
use num_traits::{One, Pow, PrimInt, Signed, ToPrimitive, Zero};
@@ -756,50 +756,79 @@ impl Hashable for PyInt {
756756
}
757757
}
758758

759-
macro_rules! atomic_func {
760-
($x:expr) => {
761-
AtomicCell::new(Some($x))
762-
};
763-
}
764-
765759
impl AsNumber for PyInt {
766760
fn as_number() -> &'static PyNumberMethods {
761+
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
762+
add: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a + b, vm)),
763+
subtract: atomic_func!(|num, other, vm| PyInt::number_int_op(
764+
num,
765+
other,
766+
|a, b| a - b,
767+
vm
768+
)),
769+
multiply: atomic_func!(|num, other, vm| PyInt::number_int_op(
770+
num,
771+
other,
772+
|a, b| a * b,
773+
vm
774+
)),
775+
remainder: atomic_func!(|num, other, vm| PyInt::number_general_op(
776+
num, other, inner_mod, vm
777+
)),
778+
divmod: atomic_func!(|num, other, vm| PyInt::number_general_op(
779+
num,
780+
other,
781+
inner_divmod,
782+
vm
783+
)),
784+
power: atomic_func!(|num, other, vm| PyInt::number_general_op(
785+
num, other, inner_pow, vm
786+
)),
787+
negative: atomic_func!(|num, vm| (&PyInt::number_downcast(num).value)
788+
.neg()
789+
.to_pyresult(vm)),
790+
positive: atomic_func!(|num, vm| Ok(PyInt::number_int(num, vm).into())),
791+
absolute: atomic_func!(|num, vm| PyInt::number_downcast(num)
792+
.value
793+
.abs()
794+
.to_pyresult(vm)),
795+
boolean: atomic_func!(|num, _vm| Ok(PyInt::number_downcast(num).value.is_zero())),
796+
invert: atomic_func!(|num, vm| (&PyInt::number_downcast(num).value)
797+
.not()
798+
.to_pyresult(vm)),
799+
lshift: atomic_func!(|num, other, vm| PyInt::number_general_op(
800+
num,
801+
other,
802+
inner_lshift,
803+
vm
804+
)),
805+
rshift: atomic_func!(|num, other, vm| PyInt::number_general_op(
806+
num,
807+
other,
808+
inner_rshift,
809+
vm
810+
)),
811+
and: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a & b, vm)),
812+
xor: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a ^ b, vm)),
813+
or: atomic_func!(|num, other, vm| PyInt::number_int_op(num, other, |a, b| a | b, vm)),
814+
int: atomic_func!(|num, other| Ok(PyInt::number_int(num, other))),
815+
float: atomic_func!(|num, vm| {
816+
let zelf = PyInt::number_downcast(num);
817+
try_to_float(&zelf.value, vm).map(|x| vm.ctx.new_float(x))
818+
}),
819+
floor_divide: atomic_func!(|num, other, vm| {
820+
PyInt::number_general_op(num, other, inner_floordiv, vm)
821+
}),
822+
true_divide: atomic_func!(|num, other, vm| {
823+
PyInt::number_general_op(num, other, inner_truediv, vm)
824+
}),
825+
index: atomic_func!(|num, vm| Ok(PyInt::number_int(num, vm))),
826+
..PyNumberMethods::NOT_IMPLEMENTED
827+
};
767828
&AS_NUMBER
768829
}
769830
}
770831

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-
803832
impl PyInt {
804833
fn number_general_op<F>(
805834
number: &PyNumber,

vm/src/builtins/mappingproxy.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{PyDict, PyDictRef, PyGenericAlias, PyList, PyTuple, PyType, PyTypeRef};
22
use crate::{
3+
atomic_func,
34
class::PyClassImpl,
45
convert::ToPyObject,
56
function::{ArgMapping, OptionalArg, PyComparisonValue},
@@ -217,11 +218,18 @@ impl AsSequence for PyMappingProxy {
217218
}
218219

219220
impl AsNumber for PyMappingProxy {
220-
const AS_NUMBER: PyNumberMethods = PyNumberMethods {
221-
or: Some(|num, args, vm| Self::number_downcast(num).or(args.to_pyobject(vm), vm)),
222-
inplace_or: Some(|num, args, vm| Self::number_downcast(num).ior(args.to_pyobject(vm), vm)),
223-
..PyNumberMethods::NOT_IMPLEMENTED
224-
};
221+
fn as_number() -> &'static PyNumberMethods {
222+
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
223+
or: atomic_func!(|num, args, vm| {
224+
PyMappingProxy::number_downcast(num).or(args.to_pyobject(vm), vm)
225+
}),
226+
inplace_or: atomic_func!(|num, args, vm| {
227+
PyMappingProxy::number_downcast(num).ior(args.to_pyobject(vm), vm)
228+
}),
229+
..PyNumberMethods::NOT_IMPLEMENTED
230+
};
231+
&AS_NUMBER
232+
}
225233
}
226234

227235
impl Iterable for PyMappingProxy {

0 commit comments

Comments
 (0)