@@ -9,91 +9,88 @@ use zenoh_flat::core::{
99use zenoh_flat:: jni:: { JniDecoderStruct , JniTryClosureBody } ;
1010use zenoh_flat:: kotlin:: { KotlinInterfaceGenerator , KotlinTypeMap } ;
1111
12- const OWNED_OBJECT : & str = "crate::owned_object::OwnedObject" ;
13-
14- fn decode_pure ( path : impl AsRef < str > ) -> InputFn {
15- let s = path. as_ref ( ) . to_string ( ) ;
16- InputFn :: new ( move |input : & syn:: Ident | -> TokenStream {
17- let p: syn:: Path = syn:: parse_str ( & s) . expect ( "invalid pure decode path" ) ;
18- quote ! { #p( #input) ? }
19- } )
12+ macro_rules! decode_pure {
13+ ( $path: path) => {
14+ InputFn :: new( |input: & syn:: Ident | -> TokenStream {
15+ quote! { $path( #input) ? }
16+ } )
17+ } ;
2018}
2119
22- fn decode_env_ref ( path : impl AsRef < str > ) -> InputFn {
23- let s = path . as_ref ( ) . to_string ( ) ;
24- InputFn :: new ( move |input : & syn:: Ident | -> TokenStream {
25- let p : syn :: Path = syn :: parse_str ( & s ) . expect ( "invalid env_ref decode path" ) ;
26- quote ! { #p ( & env , & #input ) ? }
27- } )
20+ macro_rules! decode_env_ref {
21+ ( $path : path ) => {
22+ InputFn :: new( |input: & syn:: Ident | -> TokenStream {
23+ quote! { $ path( & env , & #input ) ? }
24+ } )
25+ } ;
2826}
2927
30- fn decode_env_ref_mut ( path : impl AsRef < str > ) -> InputFn {
31- let s = path . as_ref ( ) . to_string ( ) ;
32- InputFn :: new ( move |input : & syn:: Ident | -> TokenStream {
33- let p : syn :: Path = syn :: parse_str ( & s ) . expect ( "invalid env_ref_mut decode path" ) ;
34- quote ! { #p ( & mut env , & #input ) ? }
35- } )
28+ macro_rules! decode_env_ref_mut {
29+ ( $path : path ) => {
30+ InputFn :: new( |input: & syn:: Ident | -> TokenStream {
31+ quote! { $path ( & mut env , & #input ) ? }
32+ } )
33+ } ;
3634}
3735
38- fn decode_option_env_ref ( path : impl AsRef < str > ) -> InputFn {
39- let s = path . as_ref ( ) . to_string ( ) ;
40- InputFn :: new ( move |input : & syn:: Ident | -> TokenStream {
41- let p : syn :: Path = syn :: parse_str ( & s ) . expect ( "invalid option decode path" ) ;
42- quote ! {
43- if ! #input. is_null ( ) {
44- Some ( #p ( & env , & #input ) ? )
45- } else {
46- None
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+ }
4745 }
48- }
49- } )
46+ } )
47+ } ;
5048}
5149
52- fn decode_option_env_ref_mut ( path : impl AsRef < str > ) -> InputFn {
53- let s = path . as_ref ( ) . to_string ( ) ;
54- InputFn :: new ( move |input : & syn:: Ident | -> TokenStream {
55- let p : syn :: Path = syn :: parse_str ( & s ) . expect ( "invalid option decode path" ) ;
56- quote ! {
57- if ! #input. is_null ( ) {
58- Some ( #p ( & mut env , & #input ) ? )
59- } else {
60- None
50+ macro_rules! decode_option_env_ref_mut {
51+ ( $path : path ) => {
52+ InputFn :: new( |input: & syn:: Ident | -> TokenStream {
53+ quote! {
54+ if !#input . is_null ( ) {
55+ Some ( $path ( & mut env , & #input) ? )
56+ } else {
57+ None
58+ }
6159 }
62- }
63- } )
60+ } )
61+ } ;
6462}
6563
66- fn decode_owned_raw ( owned_object : impl AsRef < str > ) -> InputFn {
67- let owned = owned_object . as_ref ( ) . to_string ( ) ;
68- InputFn :: new ( move |input : & syn:: Ident | -> TokenStream {
69- let p : syn :: Path = syn :: parse_str ( & owned ) . expect ( "invalid owned object path" ) ;
70- quote ! { #p :: from_raw ( #input ) }
71- } )
64+ macro_rules! decode_owned_raw {
65+ ( $owned_object : path ) => {
66+ InputFn :: new( |input: & syn:: Ident | -> TokenStream {
67+ quote! { $owned_object :: from_raw ( #input ) }
68+ } )
69+ } ;
7270}
7371
74- fn encode_wrapper ( path : impl AsRef < str > ) -> OutputFn {
75- let s = path . as_ref ( ) . to_string ( ) ;
76- OutputFn :: new ( move |output : Option < & syn:: Ident > | -> TokenStream {
77- let p : syn :: Path = syn :: parse_str ( & s ) . expect ( "invalid wrapper encode path" ) ;
78- match output {
79- Some ( output ) => quote ! { #p ( & mut env , #output ) ? } ,
80- None => quote ! { std :: ptr :: null_mut ( ) } ,
81- }
82- } )
72+ macro_rules! encode_wrapper {
73+ ( $path : path ) => {
74+ OutputFn :: new( |output: Option <& syn:: Ident >| -> TokenStream {
75+ match output {
76+ Some ( output) => quote! { $path ( & mut env , #output ) ? } ,
77+ None => quote! { std :: ptr :: null_mut ( ) } ,
78+ }
79+ } )
80+ } ;
8381}
8482
85- fn encode_arc_into_raw ( default_expr : impl AsRef < str > ) -> OutputFn {
86- let default = default_expr . as_ref ( ) . to_string ( ) ;
87- OutputFn :: new ( move |output : Option < & syn:: Ident > | -> TokenStream {
88- let default_expr : syn :: Expr =
89- syn :: parse_str ( & default ) . expect ( "invalid Arc encode default expr" ) ;
90- match output {
91- Some ( output ) => {
92- quote ! { std :: sync :: Arc :: into_raw ( std :: sync :: Arc :: new ( #output ) ) }
83+ macro_rules! encode_arc_into_raw {
84+ ( $default_expr : expr ) => {
85+ OutputFn :: new( |output: Option <& syn:: Ident >| -> TokenStream {
86+ match output {
87+ Some ( output ) => {
88+ quote! { std :: sync :: Arc :: into_raw ( std :: sync :: Arc :: new ( #output ) ) }
89+ }
90+ None => quote! { $default_expr } ,
9391 }
94- None => quote ! { #default_expr } ,
95- }
96- } )
92+ } )
93+ } ;
9794}
9895
9996/// Wire-side `TypeRegistry` shared across every JNI surface
@@ -104,89 +101,89 @@ fn shared_bindings() -> TypeRegistry {
104101 primitive_builtins ( )
105102 // Strings & byte arrays.
106103 . type_pair ( "String" , "jni::objects::JString" )
107- . input ( decode_env_ref_mut ( " crate::utils::decode_string" ) )
104+ . input ( decode_env_ref_mut ! ( crate :: utils:: decode_string) )
108105 . type_pair ( "Option<String>" , "jni::objects::JString" )
109- . input ( decode_option_env_ref_mut ( " crate::utils::decode_string" ) )
106+ . input ( decode_option_env_ref_mut ! ( crate :: utils:: decode_string) )
110107 . type_pair ( "Vec<u8>" , "jni::objects::JByteArray" )
111- . input ( decode_env_ref ( " crate::utils::decode_byte_array" ) )
108+ . input ( decode_env_ref ! ( crate :: utils:: decode_byte_array) )
112109 . type_pair ( "Option<Vec<u8>>" , "jni::objects::JByteArray" )
113- . input ( decode_option_env_ref ( " crate::utils::decode_byte_array" ) )
110+ . input ( decode_option_env_ref ! ( crate :: utils:: decode_byte_array) )
114111 // Callbacks.
115112 . type_pair ( "impl Fn(Sample) + Send + Sync + 'static" , "jni::objects::JObject" )
116- . input ( decode_env_ref_mut ( " crate::sample_callback::process_kotlin_sample_callback" ) )
113+ . input ( decode_env_ref_mut ! ( crate :: sample_callback:: process_kotlin_sample_callback) )
117114 . type_pair ( "impl Fn(Query) + Send + Sync + 'static" , "jni::objects::JObject" )
118- . input ( decode_env_ref_mut ( " crate::sample_callback::process_kotlin_query_callback" ) )
115+ . input ( decode_env_ref_mut ! ( crate :: sample_callback:: process_kotlin_query_callback) )
119116 . type_pair ( "impl Fn(Reply) + Send + Sync + 'static" , "jni::objects::JObject" )
120- . input ( decode_env_ref_mut ( " crate::sample_callback::process_kotlin_reply_callback" ) )
117+ . input ( decode_env_ref_mut ! ( crate :: sample_callback:: process_kotlin_reply_callback) )
121118 . type_pair ( "impl Fn() + Send + Sync + 'static" , "jni::objects::JObject" )
122- . input ( decode_env_ref_mut ( " crate::sample_callback::process_kotlin_on_close_callback" ) )
119+ . input ( decode_env_ref_mut ! ( crate :: sample_callback:: process_kotlin_on_close_callback) )
123120 // Java-enum-shaped types.
124121 . type_pair ( "CongestionControl" , "jni::sys::jint" )
125- . input ( decode_pure ( " crate::utils::decode_congestion_control" ) )
122+ . input ( decode_pure ! ( crate :: utils:: decode_congestion_control) )
126123 . type_pair ( "Priority" , "jni::sys::jint" )
127- . input ( decode_pure ( " crate::utils::decode_priority" ) )
124+ . input ( decode_pure ! ( crate :: utils:: decode_priority) )
128125 . type_pair ( "Reliability" , "jni::sys::jint" )
129- . input ( decode_pure ( " crate::utils::decode_reliability" ) )
126+ . input ( decode_pure ! ( crate :: utils:: decode_reliability) )
130127 . type_pair ( "QueryTarget" , "jni::sys::jint" )
131- . input ( decode_pure ( " crate::utils::decode_query_target" ) )
128+ . input ( decode_pure ! ( crate :: utils:: decode_query_target) )
132129 . type_pair ( "ConsolidationMode" , "jni::sys::jint" )
133- . input ( decode_pure ( " crate::utils::decode_consolidation" ) )
130+ . input ( decode_pure ! ( crate :: utils:: decode_consolidation) )
134131 . type_pair ( "ReplyKeyExpr" , "jni::sys::jint" )
135- . input ( decode_pure ( " crate::utils::decode_reply_key_expr" ) )
132+ . input ( decode_pure ! ( crate :: utils:: decode_reply_key_expr) )
136133 // KeyExpr by-value: JNI side passes the JNIKeyExpr holder object.
137134 . type_pair ( "KeyExpr<'static>" , "jni::objects::JObject" )
138- . input ( decode_env_ref_mut ( " crate::key_expr::decode_jni_key_expr" ) )
135+ . input ( decode_env_ref_mut ! ( crate :: key_expr:: decode_jni_key_expr) )
139136 // Encoding via JObject + custom decoder.
140137 . type_pair ( "Encoding" , "jni::objects::JObject" )
141- . input ( decode_env_ref_mut ( " crate::utils::decode_jni_encoding" ) )
138+ . input ( decode_env_ref_mut ! ( crate :: utils:: decode_jni_encoding) )
142139 . type_pair ( "Option<Encoding>" , "jni::objects::JObject" )
143- . input ( decode_option_env_ref_mut ( " crate::utils::decode_jni_encoding" ) )
140+ . input ( decode_option_env_ref_mut ! ( crate :: utils:: decode_jni_encoding) )
144141 // Borrows: opaque Arc handles received as `*const T`.
145142 . type_pair ( "&Session" , "*const Session" )
146- . input ( decode_owned_raw ( OWNED_OBJECT ) )
143+ . input ( decode_owned_raw ! ( crate :: owned_object :: OwnedObject ) )
147144 . type_pair ( "&Config" , "*const Config" )
148- . input ( decode_owned_raw ( OWNED_OBJECT ) )
145+ . input ( decode_owned_raw ! ( crate :: owned_object :: OwnedObject ) )
149146 // Returns: ZenohId / Vec<ZenohId> via custom encoders.
150147 . type_pair ( "ZResult<ZenohId>" , "jni::sys::jbyteArray" )
151- . output ( encode_wrapper ( " crate::zenoh_id::zenoh_id_to_byte_array" ) )
148+ . output ( encode_wrapper ! ( crate :: zenoh_id:: zenoh_id_to_byte_array) )
152149 . type_pair ( "ZResult<Vec<ZenohId>>" , "jni::sys::jobject" )
153- . output ( encode_wrapper ( " crate::zenoh_id::zenoh_ids_to_java_list" ) )
150+ . output ( encode_wrapper ! ( crate :: zenoh_id:: zenoh_ids_to_java_list) )
154151 // Returns: opaque Arc handles.
155152 . type_pair ( "ZResult<Session>" , "*const Session" )
156- . output ( encode_arc_into_raw ( " std::ptr::null()" ) )
153+ . output ( encode_arc_into_raw ! ( std:: ptr:: null( ) ) )
157154 . type_pair ( "ZResult<Publisher<'static>>" , "*const Publisher<'static>" )
158- . output ( encode_arc_into_raw ( " std::ptr::null()" ) )
155+ . output ( encode_arc_into_raw ! ( std:: ptr:: null( ) ) )
159156 . type_pair ( "ZResult<KeyExpr<'static>>" , "*const KeyExpr<'static>" )
160- . output ( encode_arc_into_raw ( " std::ptr::null()" ) )
157+ . output ( encode_arc_into_raw ! ( std:: ptr:: null( ) ) )
161158 . type_pair ( "ZResult<Subscriber<()>>" , "*const Subscriber<()>" )
162- . output ( encode_arc_into_raw ( " std::ptr::null()" ) )
159+ . output ( encode_arc_into_raw ! ( std:: ptr:: null( ) ) )
163160 . type_pair ( "ZResult<Querier<'static>>" , "*const Querier<'static>" )
164- . output ( encode_arc_into_raw ( " std::ptr::null()" ) )
161+ . output ( encode_arc_into_raw ! ( std:: ptr:: null( ) ) )
165162 . type_pair ( "ZResult<Queryable<()>>" , "*const Queryable<()>" )
166- . output ( encode_arc_into_raw ( " std::ptr::null()" ) )
163+ . output ( encode_arc_into_raw ! ( std:: ptr:: null( ) ) )
167164 . type_pair ( "ZResult<AdvancedSubscriber<()>>" , "*const AdvancedSubscriber<()>" )
168- . output ( encode_arc_into_raw ( " std::ptr::null()" ) )
165+ . output ( encode_arc_into_raw ! ( std:: ptr:: null( ) ) )
169166 . type_pair ( "ZResult<AdvancedPublisher<'static>>" , "*const AdvancedPublisher<'static>" )
170- . output ( encode_arc_into_raw ( " std::ptr::null()" ) )
167+ . output ( encode_arc_into_raw ! ( std:: ptr:: null( ) ) )
171168 // Unit returns: ZResult<()> with `()` wire type so the converter treats it as a no-return shape.
172169 . type_pair ( "ZResult<()>" , "()" )
173170 // Structs from ext.rs and nullable wrappers.
174171 . type_pair ( "HistoryConfig" , "jni::objects::JObject" )
175- . input ( decode_env_ref_mut ( " decode_HistoryConfig" ) )
172+ . input ( decode_env_ref_mut ! ( decode_HistoryConfig) )
176173 . type_pair ( "Option<HistoryConfig>" , "jni::objects::JObject" )
177- . input ( decode_option_env_ref_mut ( " decode_HistoryConfig" ) )
174+ . input ( decode_option_env_ref_mut ! ( decode_HistoryConfig) )
178175 . type_pair ( "RecoveryConfig" , "jni::objects::JObject" )
179- . input ( decode_env_ref_mut ( " decode_RecoveryConfig" ) )
176+ . input ( decode_env_ref_mut ! ( decode_RecoveryConfig) )
180177 . type_pair ( "Option<RecoveryConfig>" , "jni::objects::JObject" )
181- . input ( decode_option_env_ref_mut ( " decode_RecoveryConfig" ) )
178+ . input ( decode_option_env_ref_mut ! ( decode_RecoveryConfig) )
182179 . type_pair ( "CacheConfig" , "jni::objects::JObject" )
183- . input ( decode_env_ref_mut ( " decode_CacheConfig" ) )
180+ . input ( decode_env_ref_mut ! ( decode_CacheConfig) )
184181 . type_pair ( "Option<CacheConfig>" , "jni::objects::JObject" )
185- . input ( decode_option_env_ref_mut ( " decode_CacheConfig" ) )
182+ . input ( decode_option_env_ref_mut ! ( decode_CacheConfig) )
186183 . type_pair ( "MissDetectionConfig" , "jni::objects::JObject" )
187- . input ( decode_env_ref_mut ( " decode_MissDetectionConfig" ) )
184+ . input ( decode_env_ref_mut ! ( decode_MissDetectionConfig) )
188185 . type_pair ( "Option<MissDetectionConfig>" , "jni::objects::JObject" )
189- . input ( decode_option_env_ref_mut ( " decode_MissDetectionConfig" ) )
186+ . input ( decode_option_env_ref_mut ! ( decode_MissDetectionConfig) )
190187 . finish ( )
191188}
192189
0 commit comments