jiacai2050 commented on code in PR #1797: URL: https://github.com/apache/fury/pull/1797#discussion_r1713950350
########## rust/fury-core/src/raw/maybe_trait_object.rs: ########## @@ -0,0 +1,42 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::error::Error; +use mem::transmute; +use std::any::TypeId; +use std::mem; + +pub struct MaybeTraitObject { + ptr: *const u8, + type_id: TypeId, +} + +impl MaybeTraitObject { + pub fn new<T: 'static>(value: T) -> MaybeTraitObject { + let ptr = unsafe { transmute::<Box<T>, *const u8>(Box::new(value)) }; + let type_id = TypeId::of::<T>(); + MaybeTraitObject { ptr, type_id } + } + + pub fn to_trait_object<T: 'static>(self) -> Result<T, Error> { Review Comment: If user don't call this function, then `T` is leaked? If it's, we can impl Drop for MaybeTraitObject to release memory.. ########## rust/fury-core/src/resolver/class_resolver.rs: ########## @@ -18,99 +18,78 @@ use super::context::{ReadContext, WriteContext}; use crate::error::Error; use crate::fury::Fury; -use crate::serializer::StructSerializer; +use crate::raw::maybe_trait_object::MaybeTraitObject; +use crate::serializer::Serializer; use std::any::TypeId; use std::{any::Any, collections::HashMap}; -pub struct Harness { - serializer: fn(&dyn Any, &mut WriteContext), - deserializer: fn(&mut ReadContext) -> Result<Box<dyn Any>, Error>, -} - -impl Harness { - pub fn new( - serializer: fn(&dyn Any, &mut WriteContext), - deserializer: fn(&mut ReadContext) -> Result<Box<dyn Any>, Error>, - ) -> Harness { - Harness { - serializer, - deserializer, - } - } - - pub fn get_serializer(&self) -> fn(&dyn Any, &mut WriteContext) { - self.serializer - } - - pub fn get_deserializer(&self) -> fn(&mut ReadContext) -> Result<Box<dyn Any>, Error> { - self.deserializer - } -} +pub type TraitObjectDeserializer = fn(&mut ReadContext) -> Result<MaybeTraitObject, Error>; pub struct ClassInfo { type_def: Vec<u8>, - type_id: u32, + fury_type_id: u32, + rust_type_id: TypeId, + trait_object_serializer: fn(&dyn Any, &mut WriteContext), + trait_object_deserializer: HashMap<TypeId, TraitObjectDeserializer>, +} + +fn serialize<T: 'static + Serializer>(this: &dyn Any, context: &mut WriteContext) { + let this = this.downcast_ref::<T>().unwrap(); + T::serialize(this, context) } impl ClassInfo { - pub fn new<T: StructSerializer>(fury: &Fury, type_id: u32) -> ClassInfo { + pub fn new<T: Serializer>(fury: &Fury, type_id: u32) -> ClassInfo { ClassInfo { type_def: T::type_def(fury), - type_id, + fury_type_id: type_id, + rust_type_id: TypeId::of::<T>(), + trait_object_serializer: serialize::<T>, + trait_object_deserializer: T::get_trait_object_deserializer(), } } - pub fn get_type_id(&self) -> u32 { - self.type_id + pub fn get_rust_type_id(&self) -> TypeId { + self.rust_type_id + } + + pub fn get_fury_type_id(&self) -> u32 { + self.fury_type_id } pub fn get_type_def(&self) -> &Vec<u8> { &self.type_def } + + pub fn get_serializer(&self) -> fn(&dyn Any, &mut WriteContext) { + self.trait_object_serializer + } + + pub fn get_trait_object_deserializer<T: 'static>(&self) -> Option<&TraitObjectDeserializer> { + let type_id = TypeId::of::<T>(); + self.trait_object_deserializer.get(&type_id) + } } #[derive(Default)] pub struct ClassResolver { - serialize_map: HashMap<u32, Harness>, - type_id_map: HashMap<TypeId, u32>, + fury_type_id_map: HashMap<u32, TypeId>, class_info_map: HashMap<TypeId, ClassInfo>, } impl ClassResolver { - pub fn get_class_info(&self, type_id: TypeId) -> &ClassInfo { + pub fn get_class_info_by_rust_type(&self, type_id: TypeId) -> &ClassInfo { self.class_info_map.get(&type_id).unwrap() } - pub fn register<T: StructSerializer>(&mut self, class_info: ClassInfo, id: u32) { - fn serializer<T2: 'static + StructSerializer>(this: &dyn Any, context: &mut WriteContext) { - let this = this.downcast_ref::<T2>(); - match this { - Some(v) => { - T2::serialize(v, context); - } - None => todo!(), - } - } - - fn deserializer<T2: 'static + StructSerializer>( - context: &mut ReadContext, - ) -> Result<Box<dyn Any>, Error> { - match T2::deserialize(context) { - Ok(v) => Ok(Box::new(v)), - Err(e) => Err(e), - } - } - self.type_id_map.insert(TypeId::of::<T>(), id); - self.serialize_map - .insert(id, Harness::new(serializer::<T>, deserializer::<T>)); - self.class_info_map.insert(TypeId::of::<T>(), class_info); - } - - pub fn get_harness_by_type(&self, type_id: TypeId) -> Option<&Harness> { - self.get_harness(*self.type_id_map.get(&type_id).unwrap()) + pub fn get_class_info_by_fury_type(&self, type_id: u32) -> &ClassInfo { + let type_id = self.fury_type_id_map.get(&type_id).unwrap(); Review Comment: Too many unwrap here, how about return a Result? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
