Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Rust): Support polymorphism #1795

Merged
merged 4 commits into from
Aug 8, 2024

Conversation

theweipeng
Copy link
Member

@theweipeng theweipeng commented Aug 7, 2024

What does this PR do?

  1. Support polymorphism
    Add a class resolver which is used for managing the relationship between type_id and serializer. The serializer is generated by a generic function which will be called by dynamic dispatch. When a struct uses Box<dyn Any> as a field type and does not specify the specific type, we will use the class resolver to load the handler by type_id. In this situation, there is some performance overhead due to hash lookup, but it greatly improves convenience.
    Use as follow:
#[test]
fn any() {
    #[derive(Fury, Debug)]
    struct Animal {
        f3: String,
    }

    #[derive(Fury, Debug)]
    struct Person {
        f1: Box<dyn Any>,
    }

    let person = Person {
        f1: Box::new(Animal {
            f3: String::from("hello"),
        }),
    };

    let mut fury = Fury::default();
    fury.register::<Animal>(999);
    fury.register::<Person>(1000);
    let bin = fury.serialize(&person);
    let obj: Person = fury.deserialize(&bin).expect("");
    assert_eq!(true, obj.f1.is::<Animal>())
}
  1. Add a register function for user to register Struct and id
  2. Remove tag and hash which were generate by macro before and were removed in our protocol now.

TODO

  1. Internal types like String、Set and Map should be registered by fury by default and lookup by pattern match to avoid hash overhead.
  2. More unit testcases.
  3. Support Box<dyn customTrait>

@theweipeng theweipeng requested a review from chaokunyang as a code owner August 7, 2024 16:00
@theweipeng theweipeng requested review from chaokunyang and removed request for chaokunyang August 7, 2024 16:00
@chaokunyang chaokunyang requested a review from Xuanwo August 7, 2024 16:13
Copy link
Collaborator

@chaokunyang chaokunyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@theweipeng theweipeng merged commit e99b46f into apache:main Aug 8, 2024
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants