简体   繁体   English

PyO3中的自定义结构向量

[英]Vector of custom struct in PyO3

I'm new to Rust and PyO3 (coming from Python) so this might be obvious to more experienced people. 我是Rust和PyO3(来自Python)的新手,所以这对于更有经验的人可能是显而易见的。

I declared a pyclass struct in PyO3. 我在PyO3中声明了pyclass结构。

#[pyclass]
struct Block {
    start: i32,
    stop: i32,
}

Then I use Block in a rust function that takes a vector of Block and outputs a vector of int (signature below) 然后我使用Block锈色函数,它的向量Block ,并且输出为int的矢量(下面签名)

#[pyfunction]
fn from_blocks(block_list: Vec<Block>) -> Vec<i32>

When I compile using nightly-x86_64-apple-darwin I get the following error: 当我使用nightly-x86_64-apple-darwin编译时,出现以下错误:

#[pyfunction]
^^^^^^^^^^^^^ the trait `pyo3::FromPyObject<'_>` is not implemented for `std::vec::Vec<Block>`

How do I solve this? 我该如何解决?

EDIT: Caio is right. 编辑: Caio是正确的。 I made a mistake in tracing back the error. 在追溯错误时我犯了一个错误。 Previously I wrote 以前我写过

Then I use Block in a rust function that takes a vector of int and outputs a vector of Block (signature below) 然后我在一个rust函数中使用Block,该函数接受一个int向量并输出一个Block向量(下面的签名)

#[pyfunction]
fn to_blocks(list: Vec<i32>) -> Vec<Block>

But the actual offending function is: 但是实际的冒犯功能是:

#[pyfunction]
fn from_blocks(block_list: Vec<Block>) -> Vec<i32>

I've updated the question to make it clearer. 我已经更新了问题,以使其更清楚。

FromPyObject is intended to be used by types that can be extracted from the Python world. FromPyObject适用于可以从Python世界中提取的类型。 That is why I think that you were trying to write fn to_blocks(list: Vec<Block>) -> Vec<i32> instead of fn to_blocks(list: Vec<i32>) -> Vec<Block> . 这就是为什么我认为您尝试编写fn to_blocks(list: Vec<Block>) -> Vec<i32>而不是fn to_blocks(list: Vec<i32>) -> Vec<Block> If that is the case , lets go down to the implementation chain. 如果真是这样,让我们​​进入实施链。

FromPyObject has a default implementation for any &T that implements PyTryFrom and PyTryFrom has a default implementation for any T that implements PyTypeInfo . FromPyObject实现PyTryFrom的任何&T具有默认实现,而PyTryFrom实现PyTypeInfo的任何T具有默认实现。 [pyclass] implements PyObjectAlloc or PyObjectWithFreeList according to the impl_class method and both traits have the PyTypeInfo trait bound. [pyclass]根据impl_class方法实现PyObjectAllocPyObjectWithFreeList ,并且两个特征都绑定了PyTypeInfo特征。 Therefore, your class/struct will work just fine with references, eg: 因此,您的类/结构可以很好地与引用配合使用,例如:

#[pyfunction]
fn to_blocks(list: Vec<&Block>) -> Vec<i32>

You can see in the official documentation this explanation in a summarized way. 您可以在官方文档中以概述的方式查看此说明。

FromPyObject is implemented by various types that can be extracted from a Python object reference . FromPyObject由可以从Python对象引用中提取的各种类型实现。

It looks like the pyfunction attribute generates code that requires that the return type implements the FromPyObject trait. 看起来pyfunction属性生成的代码要求返回类型实现FromPyObject特性。 While there's a blanket implementation of FromPyObject for Vec<T> where T: FromPyObject , it looks like the code generated for pyclass attribute does not include an implementation of FromPyObject for your Block type. 虽然有一个FromPyObject for Vec<T> where T: FromPyObjectFromPyObject for Vec<T> where T: FromPyObject的全面实现, FromPyObject for Vec<T> where T: FromPyObject ,但为pyclass属性生成的代码似乎不包含针对您的Block类型的FromPyObject的实现。

Since I am unfamiliar with PyO3 other than the few minutes I just looked at its API documentation to validate this answer, I'm not sure how you'd best get a FromPyObject implementation -- perhaps there's a derive for it? 因为除了几分钟我不熟悉PyO3,我只是看了一下它的API文档来验证此答案,所以我不确定您如何最好地获得FromPyObject实现-也许有一个derive方法?

Which version of PyO3 are you on? 您正在使用哪个版本的PyO3? Your code is working for me on 0.5.3 and 0.6.0-alpha.1 . 您的代码对我适用于0.5.30.6.0-alpha.1

Due to that I can't test this, but I would guess that you need to return a PyResult : 因此,我无法对此进行测试,但我想您需要返回一个PyResult

#[pyfunction]
fn to_blocks(list: Vec<i32>) -> PyResult<Vec<Block>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM