-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of using JNI to call into Rust (#2690)
- Loading branch information
1 parent
d0b0553
commit bb73b64
Showing
8 changed files
with
187 additions
and
2 deletions.
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
21 changes: 21 additions & 0 deletions
21
examples/ffi/java_calling_rust/com/example/rustjni/BUILD.bazel
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,21 @@ | ||
load("@rules_java//java:defs.bzl", "java_library", "java_test") | ||
|
||
java_library( | ||
name = "rustjni", | ||
srcs = ["RustStringLength.java"], | ||
data = ["//ffi/java_calling_rust/rust-crate:rstrlen"], | ||
deps = [ | ||
"@bazel_tools//tools/java/runfiles", | ||
"@maven//:net_java_dev_jna_jna", | ||
], | ||
) | ||
|
||
java_test( | ||
name = "rustjni_test", | ||
srcs = ["RustJniTest.java"], | ||
test_class = "com.example.rustjni.RustJniTest", | ||
deps = [ | ||
":rustjni", | ||
"@maven//:org_hamcrest_hamcrest", | ||
], | ||
) |
15 changes: 15 additions & 0 deletions
15
examples/ffi/java_calling_rust/com/example/rustjni/RustJniTest.java
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,15 @@ | ||
package com.example.rustjni; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class RustJniTest { | ||
@Test | ||
public void testCallsJniToRust() throws Exception { | ||
final String s = "hello"; | ||
long result = RustStringLength.loadNativeLibrary().calculate_string_length_from_rust(s); | ||
assertThat(result, equalTo(5L)); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
examples/ffi/java_calling_rust/com/example/rustjni/RustStringLength.java
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,29 @@ | ||
package com.example.rustjni; | ||
|
||
import com.google.devtools.build.runfiles.AutoBazelRepository; | ||
import com.google.devtools.build.runfiles.Runfiles; | ||
|
||
import com.sun.jna.Library; | ||
import com.sun.jna.Native; | ||
|
||
import java.io.IOException; | ||
|
||
@AutoBazelRepository | ||
public interface RustStringLength extends Library { | ||
long calculate_string_length_from_rust(String s); | ||
|
||
static RustStringLength loadNativeLibrary() throws IOException { | ||
String prefix = "lib"; | ||
String extension = "so"; | ||
if ("Mac OS X".equals(System.getProperty("os.name"))) { | ||
extension = "dylib"; | ||
} else if (System.getProperty("os.name").contains("Windows")) { | ||
prefix = ""; | ||
extension = "dll"; | ||
} | ||
Runfiles.Preloaded runfiles = Runfiles.preload(); | ||
String dylibPath = runfiles.withSourceRepository(AutoBazelRepository_RustStringLength.NAME).rlocation("examples/ffi/java_calling_rust/rust-crate/" + prefix + "rstrlen." + extension); | ||
|
||
return Native.load(dylibPath, RustStringLength.class); | ||
} | ||
} |
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,8 @@ | ||
load("@rules_rust//rust:defs.bzl", "rust_shared_library") | ||
|
||
rust_shared_library( | ||
name = "rstrlen", | ||
srcs = ["rust_string_length.rs"], | ||
edition = "2021", | ||
visibility = ["//visibility:public"], | ||
) |
16 changes: 16 additions & 0 deletions
16
examples/ffi/java_calling_rust/rust-crate/rust_string_length.rs
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,16 @@ | ||
use std::ffi::CStr; | ||
use std::os::raw::c_char; | ||
|
||
/// Calculates the length of a string. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The argument must null-terminated. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn calculate_string_length_from_rust(s: *const c_char) -> i64 { | ||
let slice = unsafe { CStr::from_ptr(s).to_bytes() }; | ||
std::str::from_utf8(slice).map_or(-1, |s| { | ||
let l = s.len(); | ||
l.try_into().unwrap_or(-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,49 @@ | ||
{ | ||
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL", | ||
"__INPUT_ARTIFACTS_HASH": -152875169, | ||
"__RESOLVED_ARTIFACTS_HASH": 61617868, | ||
"artifacts": { | ||
"net.java.dev.jna:jna": { | ||
"shasums": { | ||
"jar": "34ed1e1f27fa896bca50dbc4e99cf3732967cec387a7a0d5e3486c09673fe8c6" | ||
}, | ||
"version": "5.14.0" | ||
}, | ||
"org.hamcrest:hamcrest": { | ||
"shasums": { | ||
"jar": "5e62846a89f05cd78cd9c1a553f340d002458380c320455dd1f8fc5497a8a1c1" | ||
}, | ||
"version": "2.2" | ||
} | ||
}, | ||
"dependencies": {}, | ||
"packages": { | ||
"net.java.dev.jna:jna": [ | ||
"com.sun.jna", | ||
"com.sun.jna.internal", | ||
"com.sun.jna.ptr", | ||
"com.sun.jna.win32" | ||
], | ||
"org.hamcrest:hamcrest": [ | ||
"org.hamcrest", | ||
"org.hamcrest.beans", | ||
"org.hamcrest.collection", | ||
"org.hamcrest.comparator", | ||
"org.hamcrest.core", | ||
"org.hamcrest.internal", | ||
"org.hamcrest.io", | ||
"org.hamcrest.number", | ||
"org.hamcrest.object", | ||
"org.hamcrest.text", | ||
"org.hamcrest.xml" | ||
] | ||
}, | ||
"repositories": { | ||
"https://repo1.maven.org/maven2/": [ | ||
"net.java.dev.jna:jna", | ||
"org.hamcrest:hamcrest" | ||
] | ||
}, | ||
"services": {}, | ||
"version": "2" | ||
} |