1818use super :: context:: { ReadContext , WriteContext } ;
1919use crate :: error:: Error ;
2020use crate :: fory:: Fory ;
21- use crate :: serializer:: { Serializer , StructSerializer } ;
22- use crate :: types:: TypeId ;
23- use chrono:: { NaiveDate , NaiveDateTime } ;
21+ use crate :: serializer:: StructSerializer ;
2422use std:: { any:: Any , collections:: HashMap } ;
2523
2624pub struct Harness {
@@ -50,18 +48,18 @@ impl Harness {
5048
5149pub struct TypeInfo {
5250 type_def : Vec < u8 > ,
53- type_id : u32 ,
51+ type_id : i16 ,
5452}
5553
5654impl TypeInfo {
57- pub fn new < T : StructSerializer > ( fory : & Fory , type_id : u32 ) -> TypeInfo {
55+ pub fn new < T : StructSerializer > ( fory : & Fory , type_id : i16 ) -> TypeInfo {
5856 TypeInfo {
59- type_def : T :: type_def ( fory) ,
57+ type_def : T :: type_def ( fory, type_id ) ,
6058 type_id,
6159 }
6260 }
6361
64- pub fn get_type_id ( & self ) -> u32 {
62+ pub fn get_type_id ( & self ) -> i16 {
6563 self . type_id
6664 }
6765
@@ -70,56 +68,12 @@ impl TypeInfo {
7068 }
7169}
7270
71+ #[ derive( Default ) ]
7372pub struct TypeResolver {
74- serialize_map : HashMap < u32 , Harness > ,
75- type_id_map : HashMap < std:: any:: TypeId , u32 > ,
73+ serialize_map : HashMap < i16 , Harness > ,
74+ type_id_map : HashMap < std:: any:: TypeId , i16 > ,
7675 type_info_map : HashMap < std:: any:: TypeId , TypeInfo > ,
7776}
78- macro_rules! register_harness {
79- ( $ty: ty, $id: expr, $map: expr) => { {
80- fn serializer( this: & dyn std:: any:: Any , context: & mut WriteContext ) {
81- let this = this. downcast_ref:: <$ty>( ) ;
82- match this {
83- Some ( v) => <$ty>:: serialize( v, context) ,
84- None => todo!( "" ) ,
85- }
86- }
87-
88- fn deserializer( context: & mut ReadContext ) -> Result <Box <dyn std:: any:: Any >, Error > {
89- match <$ty>:: deserialize( context) {
90- Ok ( v) => Ok ( Box :: new( v) ) ,
91- Err ( e) => Err ( e) ,
92- }
93- }
94-
95- $map. insert( $id as u32 , Harness :: new( serializer, deserializer) ) ;
96- } } ;
97- }
98-
99- impl Default for TypeResolver {
100- fn default ( ) -> Self {
101- let mut serialize_map = HashMap :: new ( ) ;
102-
103- register_harness ! ( bool , TypeId :: BOOL , serialize_map) ;
104- register_harness ! ( i8 , TypeId :: INT8 , serialize_map) ;
105- register_harness ! ( i16 , TypeId :: INT16 , serialize_map) ;
106- register_harness ! ( i32 , TypeId :: INT32 , serialize_map) ;
107- register_harness ! ( i64 , TypeId :: INT64 , serialize_map) ;
108- register_harness ! ( f32 , TypeId :: FLOAT32 , serialize_map) ;
109- register_harness ! ( f64 , TypeId :: FLOAT64 , serialize_map) ;
110-
111- register_harness ! ( String , TypeId :: STRING , serialize_map) ;
112-
113- register_harness ! ( NaiveDate , TypeId :: LOCAL_DATE , serialize_map) ;
114- register_harness ! ( NaiveDateTime , TypeId :: TIMESTAMP , serialize_map) ;
115-
116- TypeResolver {
117- serialize_map,
118- type_id_map : HashMap :: new ( ) ,
119- type_info_map : HashMap :: new ( ) ,
120- }
121- }
122- }
12377
12478impl TypeResolver {
12579 pub fn get_type_info ( & self , type_id : std:: any:: TypeId ) -> & TypeInfo {
@@ -131,7 +85,7 @@ impl TypeResolver {
13185 } )
13286 }
13387
134- pub fn register < T : StructSerializer > ( & mut self , type_info : TypeInfo , id : u32 ) {
88+ pub fn register < T : StructSerializer > ( & mut self , type_info : TypeInfo , id : i16 ) {
13589 fn serializer < T2 : ' static + StructSerializer > ( this : & dyn Any , context : & mut WriteContext ) {
13690 let this = this. downcast_ref :: < T2 > ( ) ;
13791 match this {
@@ -161,7 +115,7 @@ impl TypeResolver {
161115 self . get_harness ( * self . type_id_map . get ( & type_id) . unwrap ( ) )
162116 }
163117
164- pub fn get_harness ( & self , id : u32 ) -> Option < & Harness > {
118+ pub fn get_harness ( & self , id : i16 ) -> Option < & Harness > {
165119 self . serialize_map . get ( & id)
166120 }
167121}
0 commit comments