chaokunyang commented on code in PR #2855:
URL: https://github.com/apache/fory/pull/2855#discussion_r2477862707


##########
rust/fory-derive/src/object/derive_enum.rs:
##########
@@ -44,18 +44,130 @@ pub fn gen_write(_data_enum: &DataEnum) -> TokenStream {
 }
 
 pub fn gen_write_data(data_enum: &DataEnum) -> TokenStream {
-    let variant_idents: Vec<_> = data_enum.variants.iter().map(|v| 
&v.ident).collect();
-    let variant_values: Vec<_> = (0..variant_idents.len()).map(|v| v as 
u32).collect();
-    quote! {
-        Ok(match self {
-            #(
-                Self::#variant_idents => {
-                    context.writer.write_varuint32(#variant_values);
+    let xlang_variant_branches: Vec<TokenStream> = data_enum
+        .variants
+        .iter()
+        .enumerate()
+        .map(|(idx, v)| {
+            let ident = &v.ident;
+            let tag_value = idx as u32;
+
+            match &v.fields {
+                Fields::Unit => {
+                    quote! {
+                        Self::#ident => {
+                            context.writer.write_varuint32(#tag_value);
+                        }
+                    }
+                }
+                Fields::Unnamed(_) => {
+                    quote! {
+                        Self::#ident(..) => {
+                            context.writer.write_varuint32(#tag_value);
+                        }
+                    }
                 }
-            )*
+                Fields::Named(_) => {
+                    quote! {
+                        Self::#ident { .. } => {
+                            context.writer.write_varuint32(#tag_value);
+                        }
+                    }
+                }
+            }
         })
+        .collect();
+
+    let normal_variant_branches: Vec<TokenStream> = 
data_enum.variants.iter().enumerate().map(|(idx, v)| {
+        let ident = &v.ident;
+        let tag_value = idx as u32;
+
+        match &v.fields {
+            Fields::Unit => {
+                quote! {
+                    Self::#ident => {
+                        context.writer.write_varuint32(#tag_value);
+                    }
+                }
+            }
+            Fields::Unnamed(fields_unnamed) => {
+                let field_idents: Vec<_> = (0..fields_unnamed.unnamed.len())
+                    .map(|i| syn::Ident::new(&format!("f{}", i), 
proc_macro2::Span::call_site()))
+                    .collect();
+
+                let write_fields: Vec<_> = 
fields_unnamed.unnamed.iter().zip(field_idents.iter()).map(|(f, ident)| {
+                    let ty = &f.ty;
+                    quote! {
+                        if <#ty>::fory_is_option() {

Review Comment:
   You can check whether a field is optional at compile time, and you shoulld 
handle all unamed fields like this:
   -  if it's `Option` or `RcWeak/ArcWeak/Rc/Arc`, use `fory_write/read`
   -  if it's not `Option`, use `fory_write_data/read_data` instead.
   Since we don't support Compatible mode in this PR, we can keep things simple



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