hao-runtime-macros
bind! generates Rust bindings to a .NET assembly, for use with
hao-runtime.
Bind an assembly you built — assembly = "path" is relative to the crate's Cargo.toml:
let runtime = coreclr?;
let calculator = new?;
assert_eq!;
Or bind an assembly of the installed .NET with framework = "...", which finds it in the
newest .NET at compile time (needs .NET 8+, no SDK) — here, the class library's StringBuilder:
use StringBuilder;
let runtime = coreclr?;
// `StringBuilder(string)` → `new_str` (overloads are named after their params).
let builder = new_str?;
builder.append_str?;
builder.append_char?; // `char` crosses the boundary as a `u16`
assert_eq!;
Options
| option | |
|---|---|
assembly = "path" |
the assembly, relative to the crate's Cargo.toml |
assembly_env = "VAR" |
read the path from an environment variable at compile time, e.g. one set by a build script |
framework = "Name" |
an assembly of the installed .NET, e.g. System.Private.CoreLib (CoreCLR only) |
types = ["Ns.Type", ...] |
only bind these types (+ for nested types) |
What gets generated
Each namespace becomes a module (MyLib.Shapes → my_lib::shapes) and each public,
non-generic class becomes a struct wrapping a hao_runtime::Object:
- constructors become
new - overloads after the first are named after their parameter types:
append,append_i32,append_str_i32_i32,new_str - a class whose base class is bound too derefs to it, so it has the base's methods
- static methods take
&Runtimeas their first argument - instance methods take
&self - every call returns
hao_runtime::Result, with .NET exceptions asError::Managed
| .NET | parameter | return |
|---|---|---|
bool, int, double, ... |
bool, i32, f64, ... |
same |
char |
u16 |
u16 |
string |
&str |
Option<String> |
| bound class | &Class |
Option<Class> |
other class / object |
&hao_runtime::Object |
Option<hao_runtime::Object> |
Methods using anything else are skipped: generic types and methods, structs, arrays, and ref/out
parameters.
How it works
The macro reads the assembly with hao and builds a small thunk assembly with
Module::new: one static wrapper per bound method that converts arguments, makes the call inside
a try/catch, and passes objects as GCHandles. Two variants are embedded in your binary, one
marked [UnmanagedCallersOnly] for CoreCLR and one for Mono's unmanaged thunks. At runtime the
library and the thunks are loaded on first use, so calls go straight through function pointers,
with no reflection.
The library's path is baked in at compile time and loaded from there.
The source follows those steps: options.rs parses the input, model.rs picks the types and
methods to bind and names them, ty.rs says how each kind of value crosses the native boundary,
thunks.rs builds the thunk assembly and codegen.rs the Rust code.
Documentation
bind! on docs.rs documents every option; the
generated code uses hao_runtime::bindings.
Examples
corelib: bindsStringBuilder,PathandEnvironmentfrom .NET's class library.regex: uses .NET'sRegex, with typed results, inherited methods and exceptions as errors.
Both bind assemblies of the installed .NET with framework = "...", so they need .NET 8+ but no
SDK. hao-runtime-tests binds a C# library built from source.