-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to call Rust code from Scala
- Loading branch information
Showing
9 changed files
with
378 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package hexacraft.rs | ||
|
||
import com.github.sbt.jni.syntax.NativeLoader | ||
|
||
object RustLib extends NativeLoader("hexacraft_rs") { | ||
@native def hello(): String | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "hexacraft-rs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[dependencies] | ||
jni = "0.21.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## JNI wrapper (calling Rust from Scala) | ||
|
||
This folder contains the glue code needed for calling Rust code from Scala. The code is compiled into a dynamic library which is then shipped as a single file inside the JAR file of this sbt project. | ||
|
||
### Header files | ||
|
||
The header files in the `jni` folder can be generated using the `sbt` command `javah`, and are only there to show what the API should look like. Since the header files are checked in to git it should be easy to see if something has changed since the last commit. | ||
|
||
### Useful sbt commands | ||
|
||
- `native / test` | ||
- `javah` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use jni::objects::JObject; | ||
use jni::sys::jstring; | ||
use jni::JNIEnv; | ||
|
||
#[no_mangle] | ||
pub extern "system" fn Java_hexacraft_rs_RustLib_00024_hello<'local>( | ||
env: JNIEnv<'local>, | ||
_object: JObject<'local>, | ||
) -> jstring { | ||
let s = env.new_string("Hello from Rust!").unwrap(); | ||
s.into_raw() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package hexacraft.rs | ||
|
||
import munit.FunSuite | ||
|
||
class RustLibTest extends FunSuite { | ||
test("hello") { | ||
assertEquals(RustLib.hello(), "Hello from Rust!") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters