为python<->rust接口自动生成ffi。
rustyp的Python项目详细描述
rustypy是一个代码生成器,用于在 rust和python文件。此外,它还为 从python到rust的接口,自动类型转换 处理原语和其他基本类型(如向量、散列映射 和元组)以及一个用于处理这些对象的api(来自rust和 Python)。
功能
- 在以rust为目标的python函数中生成绑定。
- 在python中生成针对rust函数的绑定。
- 提供rust和python之间的接口类型。
安装
要安装rustypy,只需使用pip:
pip install rustypy
rustypy需要python 3.5或更高版本,并且与rust stable一起工作。
以python为目标不信任包 cpython需要 初始化包。
用法
rustypy包含一个命令行接口来生成代码 如果有必要,你可以在你的构建链中盗用。
在rust中生成python绑定
您可以执行脚本编写:
$rustypy -h
或
$python -m rustypy -h
确保rustypy位于当前的python路径中。帮助命令 拥有所有成功生成绑定的信息。
它还包括动态生成绑定的函数。在python中 使用:
from rustypy.pywrapper import bind_py_pckg_funcs # default prefix is "rust_bind_" optional = ["my_bind_prefix_", "other_prefix_"] bind_py_pckg_funcs(prefixes=optional)
此函数将为来自的包生成绑定 从调用(因此必须在 一个父文件夹中的文件)。
更多信息:Python in Rust
在python中生成rust绑定
由于python的特性,这是动态完成的,因此没有文件 生成绑定,并用它们自己的绑定进行适当包装 来自python的可调用函数。
from rustypy.rswrapper import bind_rs_crate_funcs source_path = "/home/user/workspace/rs_test_lib" compiled_lib = "/home/user/workspace/rs_test_lib/target/debug/libtest_lib.so" # default prefix is "python_bind_" optional = ["my_bind_prefix_", "other_prefix_"] lib_binds = bind_rs_crate_funcs(source_path, compiled_lib, prefixes=optional) lib_binds.my_bind_prefix__ffi_function("Hello from Python!")
C语言中没有“模块”的概念 接口),因此不能像在纯 生锈。阅读the book中的rust ffi。
更多信息:Rust in Python。
生锈的板条箱documentation。