假设我有一个像这样的 Rust 结构
struct X{...}
struct Y{
x:X
}
我希望能够编写X通过访问的python代码Y
y = Y()
y.x.some_method()
在 PyO3 中实现它的最佳方法是什么?目前我做了两个包装类
#[pyclass]
struct XWrapper{
x:X
}
#[pyclass]
struct YWrapper{
y:Y
}
#[pymethods]
impl YWrapper{
#[getter]
pub fn x(&self)->XWrapper{
XWrapper{x:self.y.clone()}
}
}
但是,这需要clone(). 我宁愿返回参考。当然我知道如果X是 a pyclass,那么我可以很容易地回到PyRef它。但问题是它X来自Y一个 Rust 库,我不能傻傻地添加#[pyclass]到它们中。