Skip to content

Commit afb64c8

Browse files
Karatussyouknowone
authored andcommitted
Add AsNumber for PyUnion
1 parent 8f17468 commit afb64c8

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

vm/src/builtins/union.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::{
66
builtins::{PyFrozenSet, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType},
77
class::PyClassImpl,
88
common::hash,
9-
convert::ToPyObject,
9+
convert::{ToPyObject, ToPyResult},
1010
function::PyComparisonValue,
11-
protocol::PyMappingMethods,
12-
types::{AsMapping, Comparable, GetAttr, Hashable, PyComparisonOp},
11+
protocol::{PyMappingMethods, PyNumberMethods},
12+
types::{AsMapping, AsNumber, Comparable, GetAttr, Hashable, PyComparisonOp},
1313
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
1414
VirtualMachine,
1515
};
@@ -35,7 +35,7 @@ impl PyPayload for PyUnion {
3535
}
3636
}
3737

38-
#[pyclass(with(Hashable, Comparable, AsMapping), flags(BASETYPE))]
38+
#[pyclass(flags(BASETYPE), with(Hashable, Comparable, AsMapping, AsNumber))]
3939
impl PyUnion {
4040
pub fn new(args: PyTupleRef, vm: &VirtualMachine) -> Self {
4141
let parameters = make_parameters(&args, vm);
@@ -255,6 +255,18 @@ impl AsMapping for PyUnion {
255255
}
256256
}
257257

258+
impl AsNumber for PyUnion {
259+
fn as_number() -> &'static PyNumberMethods {
260+
static AS_NUMBER: Lazy<PyNumberMethods> = Lazy::new(|| PyNumberMethods {
261+
or: atomic_func!(|num, other, vm| {
262+
PyUnion::or(num.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
263+
}),
264+
..PyNumberMethods::NOT_IMPLEMENTED
265+
});
266+
&AS_NUMBER
267+
}
268+
}
269+
258270
impl Comparable for PyUnion {
259271
fn cmp(
260272
zelf: &crate::Py<Self>,

0 commit comments

Comments
 (0)