-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
81 lines (76 loc) · 2.48 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use jni::objects::{JClass, JObject};
use jni::sys::{jint, jobject};
use jni::JNIEnv;
use serde::{Deserialize, Serialize};
use serde_clj::{from_object, to_object, Decoder, Encoder};
use std::collections::HashMap;
use std::iter::repeat;
#[derive(Deserialize, Serialize, Clone, Debug)]
enum Vars {
Zero,
One(usize),
Two(String),
Three(HashMap<i32, String>),
Four { a: i32, b: bool, s: String },
}
#[derive(Deserialize, Serialize, Clone, Debug)]
struct Test {
value: Vec<i64>,
another_field: Option<String>,
a_string: String,
// This gets encoded as a 1 char string, since java's char type
// does not support all utf8 characters
a_char: char,
tuple: (i32, String),
opt_tuple: (i32, Option<String>),
enumerate: Vec<Vars>,
#[serde(with = "serde_bytes")]
bytes: Vec<u8>,
}
#[no_mangle]
pub extern "system" fn Java_Test_ser(env: JNIEnv, _: JClass, n: jint) -> jobject {
let enc = Encoder::new(env).unwrap();
let mut map: HashMap<i32, String> = HashMap::new();
map.insert(7, "test".into());
let test = Test {
value: vec![1, 2, 3],
another_field: None,
a_string: "test".into(),
a_char: '𩸽',
tuple: (4, "hey".into()),
opt_tuple: (2, None),
enumerate: vec![
Vars::Zero,
Vars::One(1),
Vars::Two("three".into()),
Vars::Three(map),
Vars::Four {
a: 1,
b: true,
s: "ok?".into(),
},
],
bytes: vec![0, 1, 2],
};
let vec = repeat(test).take(n as usize).collect::<Vec<_>>();
let output = to_object(&enc, &vec).expect("serialisation failed!");
output.into_inner()
}
#[no_mangle]
pub extern "system" fn Java_Test_de(env: JNIEnv, _: JClass, obj: JObject) {
let dec = Decoder::new(env).unwrap();
let out: Vec<Test> = from_object(&dec, obj).expect("deserialisation failed");
println!("{:?}", out);
}
#[no_mangle]
pub extern "system" fn Java_Test_roundtrip(env: JNIEnv, _: JClass, obj: JObject) -> jobject {
// making an encoder and a decoder uses a lot of local refs to
// cache class & method ids
env.ensure_local_capacity(64)
.expect("failed increasing capacity");
let dec = Decoder::new(env.clone()).unwrap();
let out: Vec<Test> = from_object(&dec, obj).expect("deserialisation failed");
let enc = Encoder::new(env).unwrap();
let output = to_object(&enc, &out).expect("serialisation failed!");
output.into_inner()
}