├── .devcontainer ├── Dockerfile └── devcontainer.json ├── .gitignore ├── Cargo.toml ├── LICENSE ├── MonorepoWithCSharpRust.sln ├── README.md ├── csharp ├── MonorepoWithCSharpRust.App │ ├── MonorepoWithCSharpRust.App.csproj │ └── Program.cs ├── MonorepoWithCSharpRust.Math │ ├── FFI.cs │ └── MonorepoWithCSharpRust.Math.csproj └── MonorepoWithCSharpRust.Tests │ ├── FFITest.cs │ └── MonorepoWithCSharpRust.Tests.csproj └── rust └── math ├── Cargo.toml └── src └── lib.rs /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | # See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.217.4/containers/dotnet/.devcontainer/base.Dockerfile 2 | 3 | # [Choice] .NET version: 6.0, 5.0, 3.1, 6.0-bullseye, 5.0-bullseye, 3.1-bullseye, 6.0-focal, 5.0-focal, 3.1-focal 4 | ARG VARIANT="6.0-bullseye-slim" 5 | FROM mcr.microsoft.com/vscode/devcontainers/dotnet:0-${VARIANT} 6 | 7 | # [Choice] Node.js version: none, lts/*, 16, 14, 12, 10 8 | ARG NODE_VERSION="none" 9 | RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi 10 | 11 | # [Optional] Uncomment this section to install additional OS packages. 12 | # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ 13 | # && apt-get -y install --no-install-recommends 14 | 15 | # [Optional] Uncomment this line to install global node packages. 16 | # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: 2 | // https://github.com/microsoft/vscode-dev-containers/tree/v0.217.4/containers/dotnet 3 | { 4 | "name": "C# (.NET)", 5 | "build": { 6 | "dockerfile": "Dockerfile", 7 | "args": { 8 | // Update 'VARIANT' to pick a .NET Core version: 3.1, 5.0, 6.0 9 | // Append -bullseye or -focal to pin to an OS version. 10 | "VARIANT": "6.0", 11 | // Options 12 | "NODE_VERSION": "none" 13 | } 14 | }, 15 | 16 | // Set *default* container specific settings.json values on container create. 17 | "settings": {}, 18 | 19 | // Add the IDs of extensions you want installed when the container is created. 20 | "extensions": ["ms-dotnettools.csharp"], 21 | 22 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 23 | // "forwardPorts": [5000, 5001], 24 | 25 | // [Optional] To reuse of your local HTTPS dev cert: 26 | // 27 | // 1. Export it locally using this command: 28 | // * Windows PowerShell: 29 | // dotnet dev-certs https --trust; dotnet dev-certs https -ep "$env:USERPROFILE/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere" 30 | // * macOS/Linux terminal: 31 | // dotnet dev-certs https --trust; dotnet dev-certs https -ep "${HOME}/.aspnet/https/aspnetapp.pfx" -p "SecurePwdGoesHere" 32 | // 33 | // 2. Uncomment these 'remoteEnv' lines: 34 | // "remoteEnv": { 35 | // "ASPNETCORE_Kestrel__Certificates__Default__Password": "SecurePwdGoesHere", 36 | // "ASPNETCORE_Kestrel__Certificates__Default__Path": "/home/vscode/.aspnet/https/aspnetapp.pfx", 37 | // }, 38 | // 39 | // 3. Do one of the following depending on your scenario: 40 | // * When using GitHub Codespaces and/or Remote - Containers: 41 | // 1. Start the container 42 | // 2. Drag ~/.aspnet/https/aspnetapp.pfx into the root of the file explorer 43 | // 3. Open a terminal in VS Code and run "mkdir -p /home/vscode/.aspnet/https && mv aspnetapp.pfx /home/vscode/.aspnet/https" 44 | // 45 | // * If only using Remote - Containers with a local container, uncomment this line instead: 46 | // "mounts": [ "source=${env:HOME}${env:USERPROFILE}/.aspnet/https,target=/home/vscode/.aspnet/https,type=bind" ], 47 | 48 | // Use 'postCreateCommand' to run commands after the container is created. 49 | // "postCreateCommand": "dotnet restore", 50 | 51 | // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. 52 | "remoteUser": "vscode", 53 | "features": { 54 | "rust": "latest" 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | Cargo.lock 4 | ### https://raw.github.com/github/gitignore/499ae899e7b54e701e878759f73d9092302fd07a/VisualStudio.gitignore 5 | 6 | ## Ignore Visual Studio temporary files, build results, and 7 | ## files generated by popular Visual Studio add-ons. 8 | ## 9 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 10 | 11 | # User-specific files 12 | *.rsuser 13 | *.suo 14 | *.user 15 | *.userosscache 16 | *.sln.docstates 17 | 18 | # User-specific files (MonoDevelop/Xamarin Studio) 19 | *.userprefs 20 | 21 | # Mono auto generated files 22 | mono_crash.* 23 | 24 | # Build results 25 | [Dd]ebug/ 26 | [Dd]ebugPublic/ 27 | [Rr]elease/ 28 | [Rr]eleases/ 29 | x64/ 30 | x86/ 31 | [Aa][Rr][Mm]/ 32 | [Aa][Rr][Mm]64/ 33 | bld/ 34 | [Bb]in/ 35 | [Oo]bj/ 36 | [Ll]og/ 37 | 38 | # Visual Studio 2015/2017 cache/options directory 39 | .vs/ 40 | # Uncomment if you have tasks that create the project's static files in wwwroot 41 | #wwwroot/ 42 | 43 | # Visual Studio 2017 auto generated files 44 | Generated\ Files/ 45 | 46 | # MSTest test Results 47 | [Tt]est[Rr]esult*/ 48 | [Bb]uild[Ll]og.* 49 | 50 | # NUnit 51 | *.VisualState.xml 52 | TestResult.xml 53 | nunit-*.xml 54 | 55 | # Build Results of an ATL Project 56 | [Dd]ebugPS/ 57 | [Rr]eleasePS/ 58 | dlldata.c 59 | 60 | # Benchmark Results 61 | BenchmarkDotNet.Artifacts/ 62 | 63 | # .NET Core 64 | project.lock.json 65 | project.fragment.lock.json 66 | artifacts/ 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.vspscc 94 | *.vssscc 95 | .builds 96 | *.pidb 97 | *.svclog 98 | *.scc 99 | 100 | # Chutzpah Test files 101 | _Chutzpah* 102 | 103 | # Visual C++ cache files 104 | ipch/ 105 | *.aps 106 | *.ncb 107 | *.opendb 108 | *.opensdf 109 | *.sdf 110 | *.cachefile 111 | *.VC.db 112 | *.VC.VC.opendb 113 | 114 | # Visual Studio profiler 115 | *.psess 116 | *.vsp 117 | *.vspx 118 | *.sap 119 | 120 | # Visual Studio Trace Files 121 | *.e2e 122 | 123 | # TFS 2012 Local Workspace 124 | $tf/ 125 | 126 | # Guidance Automation Toolkit 127 | *.gpState 128 | 129 | # ReSharper is a .NET coding add-in 130 | _ReSharper*/ 131 | *.[Rr]e[Ss]harper 132 | *.DotSettings.user 133 | 134 | # JustCode is a .NET coding add-in 135 | .JustCode 136 | 137 | # TeamCity is a build add-in 138 | _TeamCity* 139 | 140 | # DotCover is a Code Coverage Tool 141 | *.dotCover 142 | 143 | # AxoCover is a Code Coverage Tool 144 | .axoCover/* 145 | !.axoCover/settings.json 146 | 147 | # Visual Studio code coverage results 148 | *.coverage 149 | *.coveragexml 150 | 151 | # NCrunch 152 | _NCrunch_* 153 | .*crunch*.local.xml 154 | nCrunchTemp_* 155 | 156 | # MightyMoose 157 | *.mm.* 158 | AutoTest.Net/ 159 | 160 | # Web workbench (sass) 161 | .sass-cache/ 162 | 163 | # Installshield output folder 164 | [Ee]xpress/ 165 | 166 | # DocProject is a documentation generator add-in 167 | DocProject/buildhelp/ 168 | DocProject/Help/*.HxT 169 | DocProject/Help/*.HxC 170 | DocProject/Help/*.hhc 171 | DocProject/Help/*.hhk 172 | DocProject/Help/*.hhp 173 | DocProject/Help/Html2 174 | DocProject/Help/html 175 | 176 | # Click-Once directory 177 | publish/ 178 | 179 | # Publish Web Output 180 | *.[Pp]ublish.xml 181 | *.azurePubxml 182 | # Note: Comment the next line if you want to checkin your web deploy settings, 183 | # but database connection strings (with potential passwords) will be unencrypted 184 | *.pubxml 185 | *.publishproj 186 | 187 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 188 | # checkin your Azure Web App publish settings, but sensitive information contained 189 | # in these scripts will be unencrypted 190 | PublishScripts/ 191 | 192 | # NuGet Packages 193 | *.nupkg 194 | # NuGet Symbol Packages 195 | *.snupkg 196 | # The packages folder can be ignored because of Package Restore 197 | **/[Pp]ackages/* 198 | # except build/, which is used as an MSBuild target. 199 | !**/[Pp]ackages/build/ 200 | # Uncomment if necessary however generally it will be regenerated when needed 201 | #!**/[Pp]ackages/repositories.config 202 | # NuGet v3's project.json files produces more ignorable files 203 | *.nuget.props 204 | *.nuget.targets 205 | 206 | # Microsoft Azure Build Output 207 | csx/ 208 | *.build.csdef 209 | 210 | # Microsoft Azure Emulator 211 | ecf/ 212 | rcf/ 213 | 214 | # Windows Store app package directories and files 215 | AppPackages/ 216 | BundleArtifacts/ 217 | Package.StoreAssociation.xml 218 | _pkginfo.txt 219 | *.appx 220 | *.appxbundle 221 | *.appxupload 222 | 223 | # Visual Studio cache files 224 | # files ending in .cache can be ignored 225 | *.[Cc]ache 226 | # but keep track of directories ending in .cache 227 | !?*.[Cc]ache/ 228 | 229 | # Others 230 | ClientBin/ 231 | ~$* 232 | *~ 233 | *.dbmdl 234 | *.dbproj.schemaview 235 | *.jfm 236 | *.pfx 237 | *.publishsettings 238 | orleans.codegen.cs 239 | 240 | # Including strong name files can present a security risk 241 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 242 | #*.snk 243 | 244 | # Since there are multiple workflows, uncomment next line to ignore bower_components 245 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 246 | #bower_components/ 247 | 248 | # RIA/Silverlight projects 249 | Generated_Code/ 250 | 251 | # Backup & report files from converting an old project file 252 | # to a newer Visual Studio version. Backup files are not needed, 253 | # because we have git ;-) 254 | _UpgradeReport_Files/ 255 | Backup*/ 256 | UpgradeLog*.XML 257 | UpgradeLog*.htm 258 | ServiceFabricBackup/ 259 | *.rptproj.bak 260 | 261 | # SQL Server files 262 | *.mdf 263 | *.ldf 264 | *.ndf 265 | 266 | # Business Intelligence projects 267 | *.rdl.data 268 | *.bim.layout 269 | *.bim_*.settings 270 | *.rptproj.rsuser 271 | *- [Bb]ackup.rdl 272 | *- [Bb]ackup ([0-9]).rdl 273 | *- [Bb]ackup ([0-9][0-9]).rdl 274 | 275 | # Microsoft Fakes 276 | FakesAssemblies/ 277 | 278 | # GhostDoc plugin setting file 279 | *.GhostDoc.xml 280 | 281 | # Node.js Tools for Visual Studio 282 | .ntvs_analysis.dat 283 | node_modules/ 284 | 285 | # Visual Studio 6 build log 286 | *.plg 287 | 288 | # Visual Studio 6 workspace options file 289 | *.opt 290 | 291 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 292 | *.vbw 293 | 294 | # Visual Studio LightSwitch build output 295 | **/*.HTMLClient/GeneratedArtifacts 296 | **/*.DesktopClient/GeneratedArtifacts 297 | **/*.DesktopClient/ModelManifest.xml 298 | **/*.Server/GeneratedArtifacts 299 | **/*.Server/ModelManifest.xml 300 | _Pvt_Extensions 301 | 302 | # Paket dependency manager 303 | .paket/paket.exe 304 | paket-files/ 305 | 306 | # FAKE - F# Make 307 | .fake/ 308 | 309 | # CodeRush personal settings 310 | .cr/personal 311 | 312 | # Python Tools for Visual Studio (PTVS) 313 | __pycache__/ 314 | *.pyc 315 | 316 | # Cake - Uncomment if you are using it 317 | # tools/** 318 | # !tools/packages.config 319 | 320 | # Tabs Studio 321 | *.tss 322 | 323 | # Telerik's JustMock configuration file 324 | *.jmconfig 325 | 326 | # BizTalk build output 327 | *.btp.cs 328 | *.btm.cs 329 | *.odx.cs 330 | *.xsd.cs 331 | 332 | # OpenCover UI analysis results 333 | OpenCover/ 334 | 335 | # Azure Stream Analytics local run output 336 | ASALocalRun/ 337 | 338 | # MSBuild Binary and Structured Log 339 | *.binlog 340 | 341 | # NVidia Nsight GPU debugger configuration file 342 | *.nvuser 343 | 344 | # MFractors (Xamarin productivity tool) working folder 345 | .mfractor/ 346 | 347 | # Local History for Visual Studio 348 | .localhistory/ 349 | 350 | # BeatPulse healthcheck temp database 351 | healthchecksdb 352 | 353 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 354 | MigrationBackup/ 355 | 356 | # Ionide (cross platform F# VS Code tools) working folder 357 | .ionide/ 358 | 359 | 360 | ### https://raw.github.com/github/gitignore/499ae899e7b54e701e878759f73d9092302fd07a/Global/VisualStudioCode.gitignore 361 | 362 | .vscode/* 363 | !.vscode/settings.json 364 | !.vscode/tasks.json 365 | !.vscode/launch.json 366 | !.vscode/extensions.json 367 | 368 | 369 | ### https://raw.github.com/github/gitignore/499ae899e7b54e701e878759f73d9092302fd07a/Global/Windows.gitignore 370 | 371 | # Windows thumbnail cache files 372 | Thumbs.db 373 | Thumbs.db:encryptable 374 | ehthumbs.db 375 | ehthumbs_vista.db 376 | 377 | # Dump file 378 | *.stackdump 379 | 380 | # Folder config file 381 | [Dd]esktop.ini 382 | 383 | # Recycle Bin used on file shares 384 | $RECYCLE.BIN/ 385 | 386 | # Windows Installer files 387 | *.cab 388 | *.msi 389 | *.msix 390 | *.msm 391 | *.msp 392 | 393 | # Windows shortcuts 394 | *.lnk 395 | 396 | 397 | ### https://raw.github.com/github/gitignore/499ae899e7b54e701e878759f73d9092302fd07a/Global/macOS.gitignore 398 | 399 | # General 400 | .DS_Store 401 | .AppleDouble 402 | .LSOverride 403 | 404 | # Icon must end with two \r 405 | Icon 406 | 407 | # Thumbnails 408 | ._* 409 | 410 | # Files that might appear in the root of a volume 411 | .DocumentRevisions-V100 412 | .fseventsd 413 | .Spotlight-V100 414 | .TemporaryItems 415 | .Trashes 416 | .VolumeIcon.icns 417 | .com.apple.timemachine.donotpresent 418 | 419 | # Directories potentially created on remote AFP share 420 | .AppleDB 421 | .AppleDesktop 422 | Network Trash Folder 423 | Temporary Items 424 | .apdisk 425 | 426 | 427 | ### https://raw.github.com/github/gitignore/499ae899e7b54e701e878759f73d9092302fd07a/Global/Linux.gitignore 428 | 429 | *~ 430 | 431 | # temporary files which can be created if a process still has a handle open of a deleted file 432 | .fuse_hidden* 433 | 434 | # KDE directory preferences 435 | .directory 436 | 437 | # Linux trash folder which might appear on any partition or disk 438 | .Trash-* 439 | 440 | # .nfs files are created when an open file is removed but is still being accessed 441 | .nfs* 442 | 443 | 444 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [workspace] 2 | members = [ 3 | "rust/math", 4 | ] 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Rei Fujimura 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /MonorepoWithCSharpRust.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30114.105 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "csharp", "csharp", "{2997C56B-CDC8-4FC1-9E91-63F6235B0817}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonorepoWithCSharpRust.Math", "csharp\MonorepoWithCSharpRust.Math\MonorepoWithCSharpRust.Math.csproj", "{EF378688-2994-42A3-9EE4-01585B72E3BF}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonorepoWithCSharpRust.App", "csharp\MonorepoWithCSharpRust.App\MonorepoWithCSharpRust.App.csproj", "{2F3BEE69-E8C6-47B1-B3D7-7FA2E5A7D1EB}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MonorepoWithCSharpRust.Tests", "csharp\MonorepoWithCSharpRust.Tests\MonorepoWithCSharpRust.Tests.csproj", "{464EC9B1-C02A-4CF4-9417-EDF38C91D691}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Release|Any CPU = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 23 | {EF378688-2994-42A3-9EE4-01585B72E3BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {EF378688-2994-42A3-9EE4-01585B72E3BF}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {EF378688-2994-42A3-9EE4-01585B72E3BF}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 | {EF378688-2994-42A3-9EE4-01585B72E3BF}.Release|Any CPU.Build.0 = Release|Any CPU 27 | {2F3BEE69-E8C6-47B1-B3D7-7FA2E5A7D1EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {2F3BEE69-E8C6-47B1-B3D7-7FA2E5A7D1EB}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {2F3BEE69-E8C6-47B1-B3D7-7FA2E5A7D1EB}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {2F3BEE69-E8C6-47B1-B3D7-7FA2E5A7D1EB}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {464EC9B1-C02A-4CF4-9417-EDF38C91D691}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {464EC9B1-C02A-4CF4-9417-EDF38C91D691}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {464EC9B1-C02A-4CF4-9417-EDF38C91D691}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {464EC9B1-C02A-4CF4-9417-EDF38C91D691}.Release|Any CPU.Build.0 = Release|Any CPU 35 | EndGlobalSection 36 | GlobalSection(NestedProjects) = preSolution 37 | {EF378688-2994-42A3-9EE4-01585B72E3BF} = {2997C56B-CDC8-4FC1-9E91-63F6235B0817} 38 | {2F3BEE69-E8C6-47B1-B3D7-7FA2E5A7D1EB} = {2997C56B-CDC8-4FC1-9E91-63F6235B0817} 39 | {464EC9B1-C02A-4CF4-9417-EDF38C91D691} = {2997C56B-CDC8-4FC1-9E91-63F6235B0817} 40 | EndGlobalSection 41 | EndGlobal 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust FFI and .NET Core P/Invoke demo 2 | 3 | Call Rust function from .NET using Rust FFI and .NET P/Invoke. 4 | 5 | ## Run Test 6 | 7 | When you run the "dotnet test" command in the "dotnet" directory, Rust library and .NET Standard library are automatically built and run test. 8 | 9 | ```sh 10 | dotnet test (-c Release) 11 | ``` 12 | 13 | ## Run Sample App 14 | 15 | ```sh 16 | dotnet run --project csharp/MonorepoWithCSharpRust.App 17 | ``` 18 | -------------------------------------------------------------------------------- /csharp/MonorepoWithCSharpRust.App/MonorepoWithCSharpRust.App.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Exe 9 | net6.0 10 | enable 11 | enable 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /csharp/MonorepoWithCSharpRust.App/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | var ans = MonorepoWithCSharpRust.Math.API.RustAdd(1, 2); 3 | Console.WriteLine($"1 + 2 = {ans}"); -------------------------------------------------------------------------------- /csharp/MonorepoWithCSharpRust.Math/FFI.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace MonorepoWithCSharpRust.Math; 5 | 6 | [StructLayout(LayoutKind.Sequential)] 7 | public struct RustVector2D 8 | { 9 | public double X; 10 | public double Y; 11 | } 12 | public static class API 13 | { 14 | 15 | [DllImport("monorepo_rust_math", EntryPoint = "rust_add")] 16 | public static extern Int32 RustAdd(Int32 a, Int32 b); 17 | 18 | [DllImport("monorepo_rust_math", EntryPoint = "rust_length")] 19 | public static extern double RustLength(RustVector2D vector); 20 | } 21 | -------------------------------------------------------------------------------- /csharp/MonorepoWithCSharpRust.Math/MonorepoWithCSharpRust.Math.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | ../../ 11 | 12 | 13 | 14 | 15 | 16 | 17 | PreserveNewest 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | PreserveNewest 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /csharp/MonorepoWithCSharpRust.Tests/FFITest.cs: -------------------------------------------------------------------------------- 1 | using Xunit; 2 | using MonorepoWithCSharpRust.Math; 3 | 4 | namespace MonorepoWithCSharpRust.Tests; 5 | 6 | public class FFITest 7 | { 8 | [Fact] 9 | public void RustAddTest() 10 | { 11 | Assert.Equal(2, MonorepoWithCSharpRust.Math.API.RustAdd(1, 1)); 12 | } 13 | 14 | [Fact] 15 | public void RustLengthTest() 16 | { 17 | var vec = new RustVector2D 18 | { 19 | X = 3, 20 | Y = 4 21 | }; 22 | Assert.Equal(5.0, MonorepoWithCSharpRust.Math.API.RustLength(vec)); 23 | Assert.Equal(3.0, vec.X); 24 | Assert.Equal(4.0, vec.Y); 25 | } 26 | } -------------------------------------------------------------------------------- /csharp/MonorepoWithCSharpRust.Tests/MonorepoWithCSharpRust.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | 7 | false 8 | 9 | 10 | 11 | 12 | 13 | 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | all 16 | 17 | 18 | runtime; build; native; contentfiles; analyzers; buildtransitive 19 | all 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /rust/math/Cargo.toml: -------------------------------------------------------------------------------- 1 | 2 | [package] 3 | name = "monorepo-rust-math" 4 | version = "0.1.0" 5 | authors = ["Rei Fujimura <25904196+rf8409@users.noreply.github.com>"] 6 | edition = "2018" 7 | 8 | [lib] 9 | name = "monorepo_rust_math" 10 | path = "src/lib.rs" 11 | crate-type = ["dylib"] 12 | 13 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 14 | 15 | [dependencies] 16 | -------------------------------------------------------------------------------- /rust/math/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub extern "C" fn rust_add(a: i32, b: i32) -> i32 { 3 | a + b 4 | } 5 | 6 | #[repr(C)] 7 | pub struct Vector2 { 8 | x: f64, 9 | y: f64, 10 | } 11 | 12 | #[no_mangle] 13 | pub extern "C" fn rust_length(vector: Vector2) -> f64 { 14 | (vector.x.powf(2.0) + vector.y.powf(2.0)).sqrt() 15 | } 16 | --------------------------------------------------------------------------------