@@ -6,69 +6,33 @@ use std::collections::HashMap;
66use proc_macro2:: TokenStream ;
77use quote:: quote;
88
9- use crate :: core:: inline_fn:: { InputFn , OutputFn } ;
9+ use crate :: core:: inline_fn:: { InputFn , NO_OUTPUT , OutputFn } ;
1010use crate :: core:: type_binding:: { canon_type, TypeBinding } ;
1111
1212#[ derive( Default , Clone ) ]
1313pub struct TypeRegistry {
1414 pub ( crate ) types : HashMap < String , TypeBinding > ,
1515}
1616
17- pub struct TypePairBuilder {
18- registry : TypeRegistry ,
19- rust_type : String ,
20- }
21-
22- impl TypePairBuilder {
23- /// Add or replace the input conversion function for the current
24- /// Rust type pair.
25- pub fn input ( mut self , decode : InputFn ) -> Self {
26- self . registry
27- . add_input_conversion_function_mut ( & self . rust_type , decode) ;
28- self
29- }
30-
31- /// Add or replace the output conversion function for the current
32- /// Rust type pair.
33- pub fn output ( mut self , encode : OutputFn ) -> Self {
34- self . registry
35- . add_output_conversion_function_mut ( & self . rust_type , encode) ;
36- self
37- }
38-
39- /// Add or replace a new Rust/Wire type pair and continue chaining
40- /// conversions against that new pair.
41- pub fn type_pair (
42- self ,
43- rust_type : impl AsRef < str > ,
44- wire_type : impl AsRef < str > ,
45- ) -> TypePairBuilder {
46- self . finish ( ) . type_pair ( rust_type, wire_type)
47- }
48-
49- /// Return to the owning [`TypeRegistry`].
50- pub fn finish ( self ) -> TypeRegistry {
51- self . registry
52- }
53- }
54-
5517impl TypeRegistry {
5618 pub fn new ( ) -> Self {
5719 Self :: default ( )
5820 }
5921
60- /// Add or replace a Rust/Wire type pair.
22+ /// Add or replace a Rust/Wire type pair together with conversion
23+ /// functions used for wire-to-Rust (`input`) and Rust-to-wire (`output`).
6124 pub fn type_pair (
6225 mut self ,
6326 rust_type : impl AsRef < str > ,
6427 wire_type : impl AsRef < str > ,
65- ) -> TypePairBuilder {
66- let rust_type = rust_type. as_ref ( ) . to_owned ( ) ;
67- self . add_type_pair_mut ( & rust_type, wire_type) ;
68- TypePairBuilder {
69- registry : self ,
70- rust_type,
71- }
28+ input : InputFn ,
29+ output : OutputFn ,
30+ ) -> Self {
31+ let rust_type = rust_type. as_ref ( ) ;
32+ self . add_type_pair_mut ( rust_type, wire_type) ;
33+ self . add_input_conversion_function_mut ( rust_type, input) ;
34+ self . add_output_conversion_function_mut ( rust_type, output) ;
35+ self
7236 }
7337
7438 /// Add or replace the input conversion function for an already
@@ -165,30 +129,39 @@ impl TypeRegistry {
165129/// Kept here as a free function so the universal core has no opinion
166130/// about which primitives are pre-registered.
167131pub fn primitive_builtins ( ) -> TypeRegistry {
132+ let bool_input = InputFn :: new ( |input : & syn:: Ident | -> TokenStream {
133+ quote ! { #input != 0 }
134+ } ) ;
135+ let id_input = InputFn :: new ( |input : & syn:: Ident | -> TokenStream {
136+ quote ! { #input }
137+ } ) ;
138+ let duration_input = InputFn :: new ( |input : & syn:: Ident | -> TokenStream {
139+ quote ! { std:: time:: Duration :: from_millis( #input as u64 ) }
140+ } ) ;
141+
168142 TypeRegistry :: new ( )
169- . type_pair ( "bool" , "jni::sys::jboolean" )
170- . input (
171- InputFn :: new ( | input : & syn :: Ident | -> TokenStream {
172- quote ! { #input != 0 }
173- } ) ,
143+ . type_pair (
144+ "bool" ,
145+ "jni::sys::jboolean" ,
146+ bool_input ,
147+ NO_OUTPUT ,
174148 )
175- . type_pair ( "i64" , "jni::sys::jlong" )
176- . input (
177- InputFn :: new ( | input : & syn :: Ident | -> TokenStream {
178- quote ! { #input }
179- } ) ,
149+ . type_pair (
150+ "i64" ,
151+ "jni::sys::jlong" ,
152+ id_input . clone ( ) ,
153+ NO_OUTPUT ,
180154 )
181- . type_pair ( "f64" , "jni::sys::jdouble" )
182- . input (
183- InputFn :: new ( | input : & syn :: Ident | -> TokenStream {
184- quote ! { #input }
185- } ) ,
155+ . type_pair (
156+ "f64" ,
157+ "jni::sys::jdouble" ,
158+ id_input ,
159+ NO_OUTPUT ,
186160 )
187- . type_pair ( "Duration" , "jni::sys::jlong" )
188- . input (
189- InputFn :: new ( | input : & syn :: Ident | -> TokenStream {
190- quote ! { std :: time :: Duration :: from_millis ( #input as u64 ) }
191- } ) ,
161+ . type_pair (
162+ "Duration" ,
163+ "jni::sys::jlong" ,
164+ duration_input ,
165+ NO_OUTPUT ,
192166 )
193- . finish ( )
194167}
0 commit comments