简体   繁体   English

接受实现宏的函数的通用 function 参数

[英]Generic function argument that accepts functions that implement a macro

I'm using pyo3 to add some rust to my python project (for performance), and wanted to create a function to make adding submodules easier.我正在使用 pyo3 将一些 rust 添加到我的 python 项目中(出于性能考虑),并希望创建一个 function 以更轻松地添加子模块。 My current code:我当前的代码:

fn register_submodule(name: &str, python: Python, parent: &PyModule, funcs: Vec<?>) -> PyResult<()> {
    let name = &(parent.name()?.to_owned() + name);
    let child = PyModule::new(python, name)?;

    for func in &funcs {
        child.add_function(wrap_pyfunction!(func, child)?)?;
    }

    py_run!(python, child, format!("import sys; sys.modules[{}] = child_module", name).as_str());
    parent.add_submodule(child)?;

    Ok(())
}

I want this function to be able to take an array or vector (or whatever) containing functions that implement the #[PyFunction] macro, take any arguments of any type and return a PyResult containing any type, so I can register these to the new module.我希望这个 function 能够采用包含实现#[PyFunction]宏的函数的数组或向量(或其他),采用任何类型的 arguments 并返回包含任何类型的 PyResult,所以我可以将这些注册到新的模块。 Is this possible?这可能吗? How would I go about doing it, or achieving a similar result?我将如何 go 做到这一点,或取得类似的结果? Any help would be much appreciated!任何帮助将非常感激!

There is no way to do it, either no way to pass vector with different functions.没有办法做到这一点,也没有办法传递具有不同功能的向量。 Because functions in Vector may be very different, may accept different type/count of arguments (how rust will know how to call/use that functions).因为Vector中的函数可能非常不同,可能会接受不同类型/计数的 arguments(rust 将如何知道如何调用/使用该函数)。

The only way to achieve this is with Enums .实现这一点的唯一方法是使用Enums

Create Enum with variants of all Python functions eg.使用所有 Python 函数的变体创建Enum ,例如。

enum PyFunctionVariant {
   F1(fn(param1: i32, param2: i32) -> PyResult),
   F2(fn(param1: &str, param2: &str) -> PyResult),
   // all your function definitions
}

Change register_submodule declaration to accept Vec of PyFunctionVariant .更改register_submodule声明以接受PyFunctionVariantVec

fn register_submodule(name: &str, python: Python, parent: &PyModule, funcs: Vec<PyFunctionVariant>) -> PyResult<()> {
   ...
}

You may also need to change this part with match , because now func is Enum not PyFunction.您可能还需要使用match更改此部分,因为现在funcEnum而不是 PyFunction。

for func in &funcs {
    child.add_function(wrap_pyfunction!(func, child)?)?; // Change Here
}

and use this way并使用这种方式

let functions = vec![
   PyFunctionVariant::F1(add_int),
   PyFunctionVariant::F2(concat_str),
   // etc...
];
register_submodule("module", python, parent, functions)

Edit: There is also another way, you can use Vec<Box<dyn Any>> for passing vector of functions.编辑:还有另一种方法,您可以使用Vec<Box<dyn Any>>来传递函数向量。 Read about it here在这里阅读

暂无
暂无

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

相关问题 将接受多个 arguments 作为参数的 function 传递给 function - Pass a function that accepts multiple arguments as an argument to a function 一个接受标量或 numpy 数组作为参数的 python 函数 - A python function that accepts as an argument either a scalar or a numpy array 我如何为接受文件路径作为参数的方法实现 python 单元测试? - how do i implement a python unit-test for a method which accepts filepath as argument? 从通用函数定义函数列表 - Define a list of functions from a generic function 将函数列表作为函数参数的 Numba/jit - Numba/jit with list of functions as function argument 如何将 1 个参数从 2 个函数传递到 1 个 function? - How to pass 1 argument each from 2 functions into 1 function? 如何以另一个函数作为可选参数来实现和调用一个函数(过滤函数) - How to implement and call a function with another function as an optional argument (filter function) 如何创建一个将每个1自变量作为一种类型或另一种类型接受的函数? - How to create a function which accepts each 1 argument as either one type or the other? 有没有办法为接受固定形状列表参数的 python function 使用“@jit”注释? - Is there a way to use `@jit` annotation for a python function that accepts a fixed shape list argument? 如何为接受默认参数的 function 编写单元测试 - 无需为默认情况编写单独的断言? - How to write unittest for function which accepts default argument - without writing separate assert for the default case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM