use crate::{ convert::{ToPyObject, TryFromObject}, pyobject::{AsObject, PyObjectRef, PyResult}, VirtualMachine, }; #[derive(result_like::OptionLike)] pub enum PyArithmeticValue { Implemented(T), NotImplemented, } impl PyArithmeticValue { pub fn from_object(vm: &VirtualMachine, obj: PyObjectRef) -> Self { if obj.is(&vm.ctx.not_implemented) { Self::NotImplemented } else { Self::Implemented(obj) } } } impl TryFromObject for PyArithmeticValue { fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { PyArithmeticValue::from_object(vm, obj) .map(|x| T::try_from_object(vm, x)) .transpose() } } impl ToPyObject for PyArithmeticValue where T: ToPyObject, { fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef { match self { PyArithmeticValue::Implemented(v) => v.to_pyobject(vm), PyArithmeticValue::NotImplemented => vm.ctx.not_implemented(), } } } pub type PyComparisonValue = PyArithmeticValue;