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. 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. Is this possible? How would I go about doing it, or achieving a similar result?
Any help would be much appreciated!