ariesdevil opened a new pull request, #3073:
URL: https://github.com/apache/fory/pull/3073

   <!--
   **Thanks for contributing to Apache Fory™.**
   
   **If this is your first time opening a PR on fory, you can refer to 
[CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).**
   
   Contribution Checklist
   
       - The **Apache Fory™** community has requirements on the naming of pr 
titles. You can also find instructions in 
[CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).
   
       - Apache Fory™ has a strong focus on performance. If the PR you submit 
will have an impact on performance, please benchmark it first and provide the 
benchmark result here.
   -->
   
   ## Why?
   
   When using `#[derive(ForyObject)]` on a struct that already has a manual 
`impl Default`, a compilation error occurs due to conflicting `Default` 
implementations. The `ForyObject` derive macro automatically generates `impl 
Default`, which conflicts with user-provided implementations.
   
   This is a common scenario when integrating Fory with existing codebases 
(e.g., OpenRaft) where types already have custom `Default` implementations with 
specific default values.
   
   ## What does this PR do?
   
   Adds a `#[fory(skip_default)]` attribute to the `ForyObject` derive macro 
that:
   
   1. Skips generating `impl std::default::Default` for the annotated type
   2. Only generates `impl ForyDefault` that delegates to the existing 
`Default::default()`
   
   ### Usage
   
   #[derive(ForyObject)]
   #[fory(skip_default)]  // Don't generate impl Default
   struct BasicNode {
       addr: String,
   }
   
   // User's own Default implementation is preserved
   impl Default for BasicNode {
       fn default() -> Self {
           Self { addr: "localhost".to_string() }
       }
   }
   
   ### Why not detect manual `impl Default` automatically?
   
   Rust's proc_macro system only has access to the token stream of the type 
being derived. It cannot see other `impl` blocks in the same file or elsewhere. 
The existing `has_existing_default()` function can only detect 
`#[derive(Default)]`, not manual `impl Default` blocks. This is a fundamental 
limitation of proc_macros, not a code issue.
   
   ## Related issues
   
   None
   
   ## Does this PR introduce any user-facing change?
   
   - [x] Does this PR introduce any public API change?
     - Adds new optional attribute `#[fory(skip_default)]` to the `ForyObject` 
derive macro
   - [ ] Does this PR introduce any binary protocol compatibility change?
   
   ## Benchmark
   
   This change only affects code generation at compile time and has no runtime 
performance impact.
   
   <!--
   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.
   -->
   


-- 
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]

Reply via email to