urlyy opened a new issue, #2726:
URL: https://github.com/apache/fory/issues/2726

   ### Feature Request
   
   currently, when users want to use EXT, because we need to handle `Any`, some 
`fixed code` has to be implemented manually. 
   Here is a demo, and `as_any` and `fory_default` are some fixed code. 
   ```rust
   #[derive(Debug, PartialEq, Default)]
   struct ExtItem {
       id: i32,
   }
   impl ForyDefault for ExtItem {
       fn fory_default() -> Self {
           Self::default()
       }
   }
   impl Serializer for ExtItem {
       fn fory_write_data(&self, context: &mut WriteContext, is_field: bool) {
           write_data(&self.id, context, is_field);
       }
       fn fory_read_data(context: &mut ReadContext, is_field: bool) -> 
Result<Self, Error> {
           Ok(Self {
               id: read_data(context, is_field)?,
           })
       }
       
       fn fory_type_id_dyn(&self, fory: &Fory) -> u32 {
           Self::fory_get_type_id(fory)
       }
       
       fn as_any(&self) -> &dyn std::any::Any {
           self
       }
   }
   ```
   
   Now I haven't find how to set default implementation for `as_any`  at 
runtime. And `impl ForyDefault` must be implemented manually. If all else 
cannot work, we can give users a helper macro.
   
   ### Is your feature request related to a problem? Please describe
   
   _No response_
   
   ### Describe the solution you'd like
   
   Here is a helper macro implementation.
   ```rust
   use proc_macro::TokenStream;
   use quote::quote;
   use syn::{parse_macro_input, ItemImpl};
   
   #[proc_macro_attribute]
   pub fn ext_helper(_attr: TokenStream, item: TokenStream) -> TokenStream {
       // 解析原来的 impl 块
       let input = parse_macro_input!(item as ItemImpl);
       let self_ty = &input.self_ty;
       let mut new_items = input.items.clone();
       // 内部插入函数 as_any
       let injected_inside = syn::parse_quote! {
           fn as_any(&self) -> &dyn std::any::Any {
               self
           }
       };
       new_items.push(injected_inside);
       // 生成原 impl + 内部注入函数
       let original_impl = ItemImpl {
           items: new_items,
           ..input.clone()
       };
   
       // 生成 impl ForyDefault for <Type>
       let fory_default_impl = quote! {
           impl ForyDefault for #self_ty {
               fn fory_default() -> Self {
                   Self::default()
               }
           }
       };
   
       let expanded = quote! {
           #original_impl
           #fory_default_impl
       };
   
       TokenStream::from(expanded)
   }
   ```
   
   and use it as:
   ```rust
   use add_extra_impl_macro::ext_helper;
   
   struct Foo;
   
   #[ext_helper]
   impl Foo {
       fn serialize(&self) {}
   }
   
   fn main() {
       
   }
   ```
   
   
   the expanded code is:
   ```rust
   use add_extra_impl_macro::ext_helper;
   struct Foo;
   impl Foo {
       fn serialize(&self) {}
       fn as_any(&self) -> &dyn std::any::Any {
           self
       }
   }
   impl ForyDefault for Foo {
       fn fory_default() -> Self {
           Self::default()
       }
   }
   fn main() {}
   ```
   
   ### Describe alternatives you've considered
   
   _No response_
   
   ### Additional context
   
   _No response_


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