我有以下函数,它使用 PyO3 调用 python 函数并获得结果(在这种情况下,int
分配给 a i32
):
fn run_python<'a, T: FromPyObject<'a> + Clone>(func_name: &str) -> Result<T, ()> {
Python::with_gil(|py| {
let pyapi = match py.import("pyapi") {
Ok(v) => v,
Err(e) => { e.print_and_set_sys_last_vars(py); return Err(()) },
};
let locals = [("pyapi", pyapi)].into_py_dict(py);
let eval_result: PyResult<&PyAny> = py.eval("pyapi.{}(**kwargs)", None, Some(&locals));
let wrapped_obj: &PyAny = match eval_result {
Ok(v) => v,
Err(e) => { e.print_and_set_sys_last_vars(py); return Err(()) },
};
let unwrapped_result: PyResult<T> = wrapped_obj.extract();
match unwrapped_result {
Ok(v) => return Ok(v.clone()),
Err(e) => return Err(()),
};
})
}
当我尝试编译时,出现以下错误:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'p` due to conflicting requirements
--> src\bin\launch.rs:89:30
|
89 | let eval_result = py.eval("pyapi.{}(**kwargs)", None, Some(&locals));
| ^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 82:22...
--> src\bin\launch.rs:82:22
|
82 | Python::with_gil(|py| {
| ______________________^
83 | | let pyapi = match py.import("pyapi") {
84 | | Ok(v) => v,
85 | | Err(e) => { e.print_and_set_sys_last_vars(py); return Err(()) },
... |
101 | | };
102 | | })
| |_____^
note: ...so that the types are compatible
--> src\bin\launch.rs:89:30
|
89 | let eval_result = py.eval("pyapi.{}(**kwargs)", None, Some(&locals));
| ^^^^
= note: expected `pyo3::Python<'_>`
found `pyo3::Python<'_>`
note: but, the lifetime must be valid for the lifetime `'a` as defined on the function body at 81:15...
--> src\bin\launch.rs:81:15
|
81 | fn run_python<'a, T: FromPyObject<'a> + Clone>(func_name: &str) -> Result<T, ()> {
| ^^
note: ...so that the types are compatible
--> src\bin\launch.rs:96:57
|
96 | let unwrapped_result: PyResult<T> = wrapped_obj.extract();
| ^^^^^^^
= note: expected `pyo3::FromPyObject<'_>`
found `pyo3::FromPyObject<'a>`
我对 Rust 很陌生,可能正在做一些愚蠢的事情(很可能是 X/Y 问题)。我怎样才能得到一个py.eval
与python解释器没有生命周期的价值(这是我假设在这里发生的事情)?