Skip to content

Easier method of executing Rust code from C# #32

@SeanOMik

Description

@SeanOMik

Hello, I was wondering of an easier method to execute Rust code (unmanaged) from C# (managed). I already know of the return-string-from-managed example and was able to easily get it working, but it required a field and two methods to be created for a single unmanaged method:

// Required since only unsafe code can use function pointers.
private static unsafe void DoRustyThings() {
    DoRustyThingsPtr();
}

private static unsafe delegate*<void> DoRustyThingsPtr;

[UnmanagedCallersOnly]
public static unsafe void SetDoRustyThingsPtr(delegate*<void> doRustyThingsPtr) => DoRustyThingsPtr = doRustyThingsPtr;

Mono has a really simple way to call managed code from unmanaged code by adding an attribute:

[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static void DoRustyThings();

Is there anyway that running managed code from unmanaged code could become easier and lead to less boilerplate? If its not implemented just yet, I may be able to mess around with implementing it and make a PR, or just hack something up to get it working for me. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions