#bindings #cli #bindings-generator #cli-tool #generate #codebase #dll

bin+lib csbinding_generator

这是一个从 Rust 代码库生成 C# 绑定的 CLI 工具。

1 个不稳定版本

0.5.0 2022年2月7日

215FFI

MIT 许可证

10KB
126

C# 绑定生成器

这是一个从 Rust 代码库生成 C# 绑定的 CLI 工具。

描述

安装

  1. 从 crates.io 获取 CLI:cargo install csbinding
  2. 运行可执行文件:csbinding -h

帮助输出

USAGE:
    csbinding.exe [OPTIONS] <SCAN_FOLDER>

ARGS:
    <SCAN_FOLDER>    The folders to start checking from there

OPTIONS:
    -d, --dllname <DLLNAME>        Using this value, you can generate code with the proper dll name
                                   from the start. You can always change it later
    -e, --excluded <EXCLUDED>      Folders/Files excluded from search
    -h, --help                     Print help information
    -n, --namespace <NAMESPACE>    If you want the functions and/or object inside a namespace, add
                                   it with this flag
    -o <OUTPUT_FOLDER>             [default: .]
    -V, --version                  Print version information

限制

底层有一些限制,主要是由我的解析库 csharp_binder 强制执行的。库还会尝试预测生成的绑定是否为空,并避免这种情况,目前没有保证。也许以后会有自定义 c# 生成。

我假设提供的文件夹中的所有 *.rs 文件都属于同一个 dll,因此只提供了一个 dll 名称参数。如果你的项目有多个 dll 文件,您将必须编辑绑定以使用正确的名称。

可以导出三种类型:枚举、结构和函数。但并非全部。枚举应该用 #[repr(u8)] 或任何其他更大的整数进行注释。结构必须用 #[repr(C)] 进行注释,以具有正确的内存表示。函数也应该有一个 pub extern "C" 注释,以便正确导出。

对于每个包含导出的 Rust 文件,都会生成一个新的 C# 文件。为绑定提供输出文件夹会更好。

警告

不要导出任何Rust特定类型,如静态数组、Vec、String或切片。也不要从std中导出任何内容。如果您想导出/导入列表,请使用指针和大小参数。

示例

在示例文件夹中有两个项目。一个是用Rust编写的,生成库;另一个是用C#编写的,使用这个库来工作。以下是一些示例指令,可以帮助您运行项目。请注意,这些指令是在Windows命令提示符下运行的,请根据您的终端环境进行调整。

# Create the bindings
csbindinglib -o examples\test_env -d print_string.dll examples\print_string

# Generate library
cd examples\print_string
cargo b --release

# Copy dll to c# project
cd ..\..
copy examples\print_string\target\release\print_string.dll examples\test_env

# Run test environment
cd examples\test_env
dotnet run

依赖项

约3MB
约62K SLoC