rustpython_vm/function/
arithmetic.rs1use crate::{
2 convert::{ToPyObject, TryFromObject},
3 object::{AsObject, PyObjectRef, PyResult},
4 VirtualMachine,
5};
6
7#[derive(result_like::OptionLike)]
8pub enum PyArithmeticValue<T> {
9 Implemented(T),
10 NotImplemented,
11}
12
13impl PyArithmeticValue<PyObjectRef> {
14 pub fn from_object(vm: &VirtualMachine, obj: PyObjectRef) -> Self {
15 if obj.is(&vm.ctx.not_implemented) {
16 Self::NotImplemented
17 } else {
18 Self::Implemented(obj)
19 }
20 }
21}
22
23impl<T: TryFromObject> TryFromObject for PyArithmeticValue<T> {
24 fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
25 PyArithmeticValue::from_object(vm, obj)
26 .map(|x| T::try_from_object(vm, x))
27 .transpose()
28 }
29}
30
31impl<T> ToPyObject for PyArithmeticValue<T>
32where
33 T: ToPyObject,
34{
35 fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
36 match self {
37 PyArithmeticValue::Implemented(v) => v.to_pyobject(vm),
38 PyArithmeticValue::NotImplemented => vm.ctx.not_implemented(),
39 }
40 }
41}
42
43pub type PyComparisonValue = PyArithmeticValue<bool>;