Skip to content

Commit 3316698

Browse files
committed
bytes converters
1 parent 37641a9 commit 3316698

4 files changed

Lines changed: 56 additions & 35 deletions

File tree

prebindgen-ext/src/core/type_registry.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,23 @@ pub fn primitive_builtins() -> TypeRegistry {
155155
});
156156
let string_option_input = option_input(string_input.clone());
157157
let string_option_output = option_output(string_output.clone());
158+
let bytes_input = InputFn::new(|input: &syn::Ident| -> TokenStream {
159+
quote! {
160+
zenoh_flat::jni::decode_byte_array(&mut env, &#input)
161+
.map_err(|err| zerror!(err))?
162+
}
163+
});
164+
let bytes_option_input = option_input(bytes_input.clone());
165+
let bytes_output = OutputFn::new(|output: Option<&syn::Ident>| -> TokenStream {
166+
match output {
167+
Some(output) => quote! {
168+
zenoh_flat::jni::encode_byte_array(&mut env, #output)
169+
.map_err(|err| zerror!(err))?
170+
},
171+
None => quote! { zenoh_flat::jni::null_byte_array() },
172+
}
173+
});
174+
let bytes_option_output = option_output(bytes_output.clone());
158175

159176
TypeRegistry::new()
160177
// Strings
@@ -170,6 +187,18 @@ pub fn primitive_builtins() -> TypeRegistry {
170187
string_option_input,
171188
string_option_output,
172189
)
190+
.type_pair(
191+
"Vec<u8>",
192+
"jni::objects::JByteArray",
193+
bytes_input,
194+
bytes_output,
195+
)
196+
.type_pair(
197+
"Option<Vec<u8>>",
198+
"jni::objects::JByteArray",
199+
bytes_option_input,
200+
bytes_option_output,
201+
)
173202
// Primitives
174203
.type_pair(
175204
"bool",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! JNI byte-array conversion helpers for use in type converters and runtime code.
2+
3+
use jni::objects::{JByteArray, JObject};
4+
use jni::JNIEnv;
5+
6+
/// Converts a JNI `JByteArray` into a Rust `Vec<u8>`.
7+
pub fn decode_byte_array(env: &mut JNIEnv, payload: &JByteArray) -> Result<Vec<u8>, String> {
8+
env.convert_byte_array(payload)
9+
.map_err(|err| format!("Error while decoding JByteArray: {}", err))
10+
}
11+
12+
/// Converts a Rust byte slice into a JNI `JByteArray`.
13+
pub fn encode_byte_array<'local>(
14+
env: &mut JNIEnv<'local>,
15+
bytes: &[u8],
16+
) -> Result<JByteArray<'local>, String> {
17+
env.byte_array_from_slice(bytes)
18+
.map_err(|err| format!("Error while encoding JByteArray: {}", err))
19+
}
20+
21+
/// Returns a null JNI byte-array handle.
22+
pub fn null_byte_array() -> JByteArray<'static> {
23+
JByteArray::from(JObject::null())
24+
}

prebindgen-ext/src/jni/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
//! whose inner is a JNI-object-shaped wire type.
2121
2222
pub mod body_strategy;
23+
pub mod byte_array_helpers;
2324
pub mod inline_fn_helpers;
2425
pub mod jni_type;
2526
pub mod jni_type_helper;
@@ -28,5 +29,6 @@ pub mod string_helpers;
2829
pub mod struct_strategy;
2930

3031
pub use body_strategy::JniTryClosureBody;
32+
pub use byte_array_helpers::{decode_byte_array, encode_byte_array, null_byte_array};
3133
pub use string_helpers::{decode_string, encode_string, null_string};
3234
pub use struct_strategy::JniDecoderStruct;

zenoh-jni/build.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ macro_rules! decode_pure {
1717
};
1818
}
1919

20-
macro_rules! decode_env_ref {
21-
($path:path) => {
22-
InputFn::new(|input: &syn::Ident| -> TokenStream {
23-
quote! { $path(&env, &#input)? }
24-
})
25-
};
26-
}
27-
2820
macro_rules! decode_env_ref_mut {
2921
($path:path) => {
3022
InputFn::new(|input: &syn::Ident| -> TokenStream {
@@ -33,20 +25,6 @@ macro_rules! decode_env_ref_mut {
3325
};
3426
}
3527

36-
macro_rules! decode_option_env_ref {
37-
($path:path) => {
38-
InputFn::new(|input: &syn::Ident| -> TokenStream {
39-
quote! {
40-
if !#input.is_null() {
41-
Some($path(&env, &#input)?)
42-
} else {
43-
None
44-
}
45-
}
46-
})
47-
};
48-
}
49-
5028
macro_rules! decode_option_env_ref_mut {
5129
($path:path) => {
5230
InputFn::new(|input: &syn::Ident| -> TokenStream {
@@ -161,19 +139,7 @@ macro_rules! encode_cast {
161139
/// bindings — into the methods phase and the Kotlin generator.
162140
fn shared_bindings() -> TypeRegistry {
163141
primitive_builtins()
164-
// Strings & byte arrays (String converters now in primitive_builtins)
165-
.type_pair(
166-
"Vec<u8>",
167-
"jni::objects::JByteArray",
168-
decode_env_ref!(crate::utils::decode_byte_array),
169-
NO_OUTPUT,
170-
)
171-
.type_pair(
172-
"Option<Vec<u8>>",
173-
"jni::objects::JByteArray",
174-
decode_option_env_ref!(crate::utils::decode_byte_array),
175-
NO_OUTPUT,
176-
)
142+
// String and byte-array converters are in primitive_builtins.
177143
// Callbacks.
178144
.type_pair(
179145
"impl Fn(Sample) + Send + Sync + 'static",

0 commit comments

Comments
 (0)