-
Notifications
You must be signed in to change notification settings - Fork 13
/
lib.rs
59 lines (50 loc) · 2.02 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use robusta_jni::bridge;
#[bridge]
mod jni {
use robusta_jni::convert::{
Field, IntoJavaValue, Signature, TryFromJavaValue, TryIntoJavaValue,
};
use robusta_jni::jni::errors::Error as JniError;
use robusta_jni::jni::errors::Result as JniResult;
use robusta_jni::jni::objects::AutoLocal;
use robusta_jni::jni::JNIEnv;
#[derive(Signature, TryIntoJavaValue, IntoJavaValue, TryFromJavaValue)]
#[package(com.example.robusta)]
pub struct HelloWorld<'env: 'borrow, 'borrow> {
#[instance]
raw: AutoLocal<'env, 'borrow>,
#[field]
foo: Field<'env, 'borrow, String>,
}
impl<'env: 'borrow, 'borrow> HelloWorld<'env, 'borrow> {
#[constructor]
pub extern "java" fn new(env: &'borrow JNIEnv<'env>) -> JniResult<Self> {}
pub extern "jni" fn special(mut input1: Vec<i32>, input2: i32) -> Vec<String> {
input1.push(input2);
input1.iter().map(ToString::to_string).collect()
}
pub extern "jni" fn nativeFun(self, env: &JNIEnv, static_call: bool) -> JniResult<i32> {
if static_call {
Ok(HelloWorld::staticJavaAdd(env, 1, 2))
} else {
let a = self.javaAdd(env, 0, 0)?;
Ok(a + self.javaAdd(env, 1, 2)?)
}
}
#[call_type(safe(
exception_class = "java.lang.IllegalArgumentException",
message = "something bad happened"
))]
pub extern "jni" fn catchMe(self, _env: &JNIEnv) -> JniResult<i32> {
Err(JniError::NullPtr("catch me if you can"))
}
pub extern "java" fn javaAdd(&self, _env: &JNIEnv, i: i32, u: i32) -> JniResult<i32> {}
#[call_type(unchecked)]
pub extern "java" fn staticJavaAdd(env: &JNIEnv, i: i32, u: i32) -> i32 {}
pub extern "jni" fn setStringHelloWorld(mut self) -> JniResult<()> {
println!("[rust]: self.foo: \"{}\"", self.foo.get()?);
self.foo.set("hello world".into())?;
Ok(())
}
}
}