使用pyo3返回文档对象的向量时出错

2024-09-30 22:17:22 发布

您现在位置:Python中文网/ 问答频道 /正文

将文档对象的向量从rust返回到python失败

我在rust中有一个结构和方法实现,如下所示

use mongodb::{
    bson::{Bson, Document},
    error::Error,
    sync::Client,
};
use pyo3::prelude::*;

#[pyclass]
pub struct SampleStruct{
    docs: Vec<Document>,
}

#[pymethods]
impl SampleStruct{
    #[new]
    pub fn new() -> Self {...}

    pub fn a_method(&self, some_string: &str) -> Vec<&Document> {...}
}

上述操作失败,并显示以下消息

error[E0277]: the trait bound `std::vec::Vec<&bson::document::Document>: pyo3::callback::IntoPyCallbackOutput<_>` is not satisfied
   --> src/fail_demo.rs:55:1
    |
55  | #[pymethods]
    | ^^^^^^^^^^^^ the trait `pyo3::callback::IntoPyCallbackOutput<_>` is not implemented for `std::vec::Vec<&bson::document::Document>`
    | 
   ::: /home/demo/.cargo/registry/src/github.com-1ecc6299db9ec823/pyo3-0.11.1/src/callback.rs:169:8
    |
169 |     T: IntoPyCallbackOutput<U>,
    |        ----------------------- required by this bound in `pyo3::callback::convert`
    |
    = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)


Tags: insrcnewusecallbackerrorrustdocument