mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-02 19:39:49 +09:00
AsNumber::number_downcast_exact useful for actual numbers
This commit is contained in:
@@ -488,6 +488,10 @@ impl AsNumber for PyComplex {
|
||||
});
|
||||
&AS_NUMBER
|
||||
}
|
||||
|
||||
fn clone_exact(zelf: &Py<Self>, vm: &VirtualMachine) -> PyRef<Self> {
|
||||
vm.ctx.new_complex(zelf.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl PyComplex {
|
||||
@@ -502,14 +506,6 @@ impl PyComplex {
|
||||
Ok(vm.ctx.not_implemented())
|
||||
}
|
||||
}
|
||||
|
||||
fn number_downcast_exact(number: PyNumber, vm: &VirtualMachine) -> PyRef<PyComplex> {
|
||||
if let Some(zelf) = number.obj.downcast_ref_if_exact::<Self>(vm) {
|
||||
zelf.to_owned()
|
||||
} else {
|
||||
vm.ctx.new_complex(Self::number_downcast(number).value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromArgs)]
|
||||
|
||||
@@ -966,10 +966,24 @@ pub trait AsNumber: PyPayload {
|
||||
#[pyslot]
|
||||
fn as_number() -> &'static PyNumberMethods;
|
||||
|
||||
fn clone_exact(_zelf: &Py<Self>, _vm: &VirtualMachine) -> PyRef<Self> {
|
||||
// not all AsNumber requires this implementation.
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn number_downcast(num: PyNumber) -> &Py<Self> {
|
||||
unsafe { num.obj.downcast_unchecked_ref() }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn number_downcast_exact(number: PyNumber, vm: &VirtualMachine) -> PyRef<Self> {
|
||||
if let Some(zelf) = number.obj.downcast_ref_if_exact::<Self>(vm) {
|
||||
zelf.to_owned()
|
||||
} else {
|
||||
Self::clone_exact(Self::number_downcast(number), vm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pyclass]
|
||||
|
||||
Reference in New Issue
Block a user