Skip to content

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
chaokunyang added a commit that referenced this pull request Oct 6, 2025
…#2704)

## Why?

<!-- Describe the purpose of this PR. -->

## What does this PR do?

suport dyn any trait object serialization for box/arc/rc:
- introduce `ForyDefault` trait to work around orphan rule, so we can
impl `default` for `Rc<dny Any>`
- rename `Fory` derive to `ForyObject` derive
- add serializer impl for `Box/Rc/Arc<any>`
- support shared reference for `Rc/Arc<any>`
- introduce `write_any_typeinfo/read_typeinfo` for dynamic type-meta
read/write

## Related issues

#2691
#1797 
#1795

## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fory/issues/new/choose) describing the
need to do so and update the document if necessary.

Delete section if not applicable.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?

## Benchmark

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.

Delete section if not applicable.
-->
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