Skip to main content

Crate interoptopus_csharp

Crate interoptopus_csharp 

Source
Expand description

§interoptopus_csharp

C# backend for Interoptopus.

Generates idiomatic C# bindings from a Rust FFI library, including classes for services, delegates for callbacks, and most Interoptopus patterns.

§Usage

Add the crate as a dependency:

[dependencies]
interoptopus_csharp = "..."

Then write a test that builds an inventory and runs the backend:

use interoptopus::inventory::RustInventory;
use interoptopus::{ffi, function};
use interoptopus_csharp::RustLibrary;

#[ffi]
pub fn my_function(x: u32) -> u32 { x + 1 }

fn generate_bindings() -> Result<(), Box<dyn std::error::Error>> {
    let inventory = RustInventory::new()
        .register(function!(my_function))
        .validate();

    RustLibrary::builder(inventory)
        .dll_name("my_lib")
        .build()
        .process()?
        .write_buffers_to("bindings/")?;

    Ok(())
}

This produces an Interop.cs file in bindings/ with [DllImport("my_lib")] declarations and idiomatic C# wrappers for all registered items.

For multi-file output or custom namespaces, use a Dispatch:

use interoptopus_csharp::dispatch::Dispatch;
use interoptopus_csharp::lang::meta::FileEmission;
use interoptopus_csharp::output::Target;

let dispatch = Dispatch::custom(|item, _| match item.emission {
    FileEmission::Common => Target::new("Interop.Common.cs", "My.Company.Common"),
    FileEmission::Default => Target::new("Interop.cs", "My.Company"),
    FileEmission::CustomModule(_) => Target::new("Interop.cs", "My.Company"),
});

Modules§

config
Configuration options for the C# backend.
dispatch
Routes FFI items to output files based on customizable rules.
extensions
Extension point for customizing the C# code generation pipeline.
output
Output file descriptors for generated C# code.
pluginunstable-plugins
.NET runtime loader for ‘reverse interop’ plugins.

Structs§

DotnetLibraryunstable-plugins
Code generation pipeline for .NET plugins (reverse interop).
DotnetLibraryBuilderunstable-plugins
Builder for configuring and constructing a DotnetLibrary.
RustLibrary
The main entry point for C# code generation.
RustLibraryBuilder
Builder for configuring and constructing a RustLibrary.

Enums§

Error
Errors that can occur during C# code generation.