Skip to content

Commit 0bd49e8

Browse files
committed
Initial commit
0 parents  commit 0bd49e8

File tree

10 files changed

+614
-0
lines changed

10 files changed

+614
-0
lines changed

.gitignore

Lines changed: 443 additions & 0 deletions
Large diffs are not rendered by default.

Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "rust-ffi"
3+
version = "0.1.0"
4+
authors = ["Rei Fujimura <25904196+rf8409@users.noreply.github.com>"]
5+
edition = "2018"
6+
7+
[lib]
8+
name = "rust_ffi"
9+
path = "src/lib.rs"
10+
crate-type = ["dylib"]
11+
12+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
13+
14+
[dependencies]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
# In "dotnet" directory
11+
dotnet test (-c Release)
12+
```

dotnet/RustFFI.Tests/ModuleTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using Xunit;
3+
4+
namespace RustFFI.Tests
5+
{
6+
public class ModuleTests
7+
{
8+
[Fact]
9+
public void RustAddTest()
10+
{
11+
Assert.Equal(2, Module.RustAdd(1, 1));
12+
}
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
11+
<PackageReference Include="xunit" Version="2.4.0" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\RustFFI\RustFFI.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

dotnet/RustFFI.sln

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.26124.0
5+
MinimumVisualStudioVersion = 15.0.26124.0
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RustFFI", "RustFFI\RustFFI.csproj", "{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RustFFI.Tests", "RustFFI.Tests\RustFFI.Tests.csproj", "{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Debug|x64 = Debug|x64
14+
Debug|x86 = Debug|x86
15+
Release|Any CPU = Release|Any CPU
16+
Release|x64 = Release|x64
17+
Release|x86 = Release|x86
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
23+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Debug|x64.ActiveCfg = Debug|Any CPU
26+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Debug|x64.Build.0 = Debug|Any CPU
27+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Debug|x86.ActiveCfg = Debug|Any CPU
28+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Debug|x86.Build.0 = Debug|Any CPU
29+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Release|Any CPU.ActiveCfg = Release|Any CPU
30+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Release|x64.ActiveCfg = Release|Any CPU
32+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Release|x64.Build.0 = Release|Any CPU
33+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Release|x86.ActiveCfg = Release|Any CPU
34+
{80CE3B95-DF1C-4D96-A43A-CA1EA6CE0281}.Release|x86.Build.0 = Release|Any CPU
35+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Debug|x64.ActiveCfg = Debug|Any CPU
38+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Debug|x64.Build.0 = Debug|Any CPU
39+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Debug|x86.ActiveCfg = Debug|Any CPU
40+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Debug|x86.Build.0 = Debug|Any CPU
41+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Release|x64.ActiveCfg = Release|Any CPU
44+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Release|x64.Build.0 = Release|Any CPU
45+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Release|x86.ActiveCfg = Release|Any CPU
46+
{D9012CB2-5587-4E53-A7EA-E76E41B4B5F8}.Release|x86.Build.0 = Release|Any CPU
47+
EndGlobalSection
48+
EndGlobal

dotnet/RustFFI/Module.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace RustFFI
5+
{
6+
public static class Module
7+
{
8+
[DllImport("rust_ffi", EntryPoint = "rust_add")]
9+
public static extern Int32 RustAdd(Int32 a, Int32 b);
10+
}
11+
}

dotnet/RustFFI/RustFFI.csproj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<PropertyGroup>
8+
<RustRootDirectory>../../</RustRootDirectory>
9+
</PropertyGroup>
10+
11+
<Target Name="CargoBuildDebug" BeforeTargets="PreBuildEvent" Condition=" '$(Configuration)' == 'Debug' ">
12+
<Exec Command="cargo build" WorkingDirectory="$(RustRootDirectory)"/>
13+
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
14+
<Content Include="$(RustRootDirectory)/target/debug/*rust_ffi.*">
15+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
16+
</Content>
17+
</ItemGroup>
18+
</Target>
19+
<Target Name="CargoBuildRelease" BeforeTargets="PreBuildEvent" Condition=" '$(Configuration)' == 'Release' ">
20+
<Exec Command="cargo build --release" WorkingDirectory="$(RustRootDirectory)"/>
21+
<ItemGroup Condition=" '$(Configuration)' == 'Release' ">
22+
<Content Include="$(RustRootDirectory)/target/release/*rust_ffi.*">
23+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
24+
</Content>
25+
</ItemGroup>
26+
</Target>
27+
28+
</Project>

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[no_mangle]
2+
pub extern "C" fn rust_add(a: i32, b:i32) -> i32 {
3+
a + b
4+
}

0 commit comments

Comments
 (0)