我有一个结构
#[pyclass]
pub struct DynMat {
...
}
我有这个功能
#[pyfunction]
#[text_signature = "(tensor/)"]
pub fn exp<'py>(py: Python<'py>, tensor_or_scalar: &'py PyAny) -> PyResult<&'py PyAny> {
// I need to return &PyAny because I might either return PyFloat or DynMat
if let Ok(scalar) = tensor_or_scalar.cast_as::<PyFloat>() {
let scalar: &PyAny = PyFloat::new(py, scalar.extract::<f64>()?.exp());
Ok(scalar)
} else if let Ok(tensor) = tensor_or_scalar.cast_as::<PyCell<DynMat>>() {
let mut tensor:PyRef<DynMat> = tensor.try_borrow()?;
let tensor:DynMat = tensor.exp()?;
// what now? How to return tensor
}
}
pyclass
问题是,我怎样才能从一个期望的函数返回一个标有PyResult<&'py PyAny>