Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
theweipeng committed Aug 13, 2024
1 parent fd97ad0 commit b60587b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion rust/fury-core/src/fury.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Fury {
&self.class_resolver
}

pub fn register<T: 'static + Serializer>(&mut self, id: u32) -> &ClassInfo {
pub fn register<T: 'static + Serializer>(&mut self, id: u32) -> &mut ClassInfo {
self.class_resolver
.register::<T>(ClassInfo::new::<T>(self, id), id)
}
Expand Down
8 changes: 4 additions & 4 deletions rust/fury-core/src/resolver/class_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::context::{ReadContext, WriteContext};
use crate::error::Error;
use crate::fury::Fury;
use crate::raw::maybe_trait_object::MaybeTraitObject;
use crate::serializer::Serializer;
use crate::serializer::{Serializer, SS};
use std::any::TypeId;
use std::{any::Any, collections::HashMap};

Expand Down Expand Up @@ -49,7 +49,7 @@ impl ClassInfo {
}
}

pub fn associate<T: Any>(&mut self, type_id: TypeId, func: TraitObjectDeserializer) {
pub fn associate(&mut self, type_id: TypeId, func: TraitObjectDeserializer) {
self.trait_object_deserializer.insert(type_id, func);
}

Expand Down Expand Up @@ -91,10 +91,10 @@ impl ClassResolver {
self.class_info_map.get(type_id).unwrap()
}

pub fn register<T: Serializer>(&mut self, class_info: ClassInfo, id: u32) -> &ClassInfo {
pub fn register<T: Serializer>(&mut self, class_info: ClassInfo, id: u32) -> &mut ClassInfo {
let type_id = TypeId::of::<T>();
self.fury_type_id_map.insert(id, type_id);
self.class_info_map.insert(type_id, class_info);
self.class_info_map.get(&type_id).unwrap()
self.class_info_map.get_mut(&type_id).unwrap()
}
}
7 changes: 7 additions & 0 deletions rust/fury-core/src/serializer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::collections::HashMap;

use crate::error::Error;
use crate::fury::Fury;
use crate::raw::maybe_trait_object::MaybeTraitObject;
use crate::resolver::class_resolver::TraitObjectDeserializer;
use crate::resolver::context::{ReadContext, WriteContext};
use crate::types::RefFlag;
Expand Down Expand Up @@ -75,6 +76,12 @@ pub trait PolymorphicCast {
fn type_id(&self) -> TypeId;
}

pub trait SS {
fn deserialize_to_trait_object<T: Serializer + 'static>(context: &mut ReadContext) -> Result<MaybeTraitObject, Error>;

fn trait_object_type_id() -> TypeId;
}

impl<T: Serializer + 'static> PolymorphicCast for T {
fn as_any(&self) -> &dyn Any {
self
Expand Down
4 changes: 4 additions & 0 deletions rust/fury-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub fn impl_polymorph(_attr: TokenStream, item: proc_macro::TokenStream) -> Toke
let output = quote! {
#input

impl<T: #name> SS for T {

}

trait #shadow where Self: Animal + fury_core::serializer::Serializer {
fn deserialize_to_trait_object(context: &mut fury_core::resolver::context::ReadContext) -> Result<fury_core::raw::maybe_trait_object::MaybeTraitObject, fury_core::error::Error> {
match Self::deserialize(context) {
Expand Down
4 changes: 2 additions & 2 deletions rust/tests/tests/test_custom_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fn test_custom_trait_object_work() {
}),
};
fury.register::<Person>(501);
let mut t = fury.register::<Dog>(500);
t.associate::<dyn Animal>();
let mut t = fury.register::<Dog>(500)
.associate(<Dog as AnimalShadow>::trait_object_type_id(), <Dog as AnimalShadow>::deserialize_to_trait_object);
let bin = fury.serialize(&p);
let obj: Person = fury.deserialize(&bin).unwrap();
assert_eq!(obj.pet.get_name(), "puppy");
Expand Down

0 comments on commit b60587b

Please sign in to comment.