Skip to main content

Module pattern

Module pattern 

Source
Expand description

C#-specific backend patterns for transparent exception mapping.

The central type here is Try<T>, a transparent exception converter for plugins. When the C# backend encounters a function returning Try<T>, it automatically captures exceptions and converts them into an ExceptionError.

§Example

plugin!(MyPlugin {
    fn foo() -> Try<u32>;
});

plugin.foo().ok()?;

On the C# side the following plugin code is emitted that allows callees to just return a result. Should an exception happen it will be automatically caught and converted.

partial class Plugin : IPlugin
{
    public static uint Foo() { };
}

§Registering exceptions

You can tell the builder which C# exception to map (besides System.Exception) with DotnetLibraryBuilder::exception():

let output = DotnetLibrary::builder(inventory)
    .exception(Exception::new("System.IO.FileNotFoundException"))
    .build()
    .process()?;

§Try - Result conversion

Because Try<T> is effectively an ffi::Result it cannot be used with Rust’s ? operator directly. TryExtension converts it into a standard result for ergonomic error propagation:

let value = result.ok()?;

Structs§

Exception
A registered C# exception type for structured error mapping.
ExceptionError
Wire-level error type carried inside Try<T>.

Traits§

TryExtension
Converts a Try<T> into a standard Result<T, ExceptionError>.

Type Aliases§

Try
Return type that enables transparent C# exception mapping.