This repository explores the possibility of calling C# code from Rust when the C# code is compiled into a native library using .NET Native AOT. The project serves as a proof of concept for integrating Rust with C#, leveraging native interop for high-performance applications.
The primary goal of this project is to demonstrate how Rust can call functions from a C# library that has been compiled into a native shared library using .NET Native AOT. This setup is particularly useful for scenarios where Rust needs to interact with C# logic while maintaining the performance benefits of native code execution.
- C# to Rust Interop: Successfully implemented. Rust can now call functions from a C# library compiled with .NET Native AOT.
- Rust to C# Interop: To be implemented. The next step is to enable calling Rust functions from C#.
NativeCSharp/
: This directory contains the C# project. The C# code is compiled into a native shared library (.so
for Linux,.dll
for Windows) using .NET Native AOT.Rust/
: This directory contains the Rust project, which dynamically loads the C# native library and calls its functions.
- Rust: Ensure that Rust is installed on your system. You can install it via rustup.
- .NET SDK: Install the .NET SDK for building the C# project. The SDK can be downloaded from Microsoft's official website.
- Platform-Specific Tools: Depending on your platform, ensure you have the necessary tools for compiling native code:
- On Windows: Visual Studio (Installer) with the C++ build tools.
- On Linux: GCC or Clang for compiling native code.
-
Build the C# Native Library:
- Navigate to the
NativeCSharp
directory. - Build the C# project using the appropriate configuration (
Debug
orRelease
). For example:ordotnet publish -c Release -r linux-x64 --self-contained
dotnet publish -c Release -r win-x64 --self-contained
Note that the rust build script always expects an release build.
- The output will be a native shared library located in
NativeCSharp/bin/Release/<platform>/publish/
.
- Navigate to the
-
Build the Rust Project:
- Navigate to the
Rust
directory. - Run the build script using Cargo:
cargo build
- The build script (
build.rs
) will automatically copy the compiled C# library into the appropriate target directory (target/debug
ortarget/release
).
- Navigate to the
To run the Rust project, use the following command:
cargo run
This will execute the Rust program, which loads the C# native library and calls its functions.
The next goal for this project is to enable calling Rust functions from C#. This will involve:
Exposing Rust functions as C ABI functions.
Using P/Invoke or similar techniques in C# to call Rust functions.
Testing and validating the integration.
Stay tuned for updates as this feature is developed.
Contributions are welcome! If you'd like to help implement Rust to C# interop or improve the existing functionality, feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.