Repository: dtolnay/cxx (language: rust)

Issue:
[Question] How to use serde derive with Shared structs and enums
I have large enums and structs which I wish to share between C++ and Rust. My naive plan is to receive bincode serialized into`Vec<u8>` from wire and deserialize it on rust side and return it to C++ side

I tried following:-
```rust
#[cxx::bridge]
pub mod ffi {
    #[derive(Serialize, Deserialize)]
    pub enum RouteError {
        Failed,
        NotAvailable,
        NotFound,
       //long list ...
    }
}
```

But I'm getting following error:- 
```rust
error: unsupported derive
  --> common/src/lib.rs:12:14
   |
12 |     #[derive(Serialize, Deserialize)]
   |              ^^^^^^^^^

error: unsupported derive
  --> common/src/lib.rs:12:25
   |
12 |     #[derive(Serialize, Deserialize)]
   |                         ^^^^^^^^^^^
```


Discussion / hints:
https://github.com/dtolnay/cxx/issues/108 Implemented all standard derives. But this doesn't work for external ones like serde. I'm not sure if an exception should be added for serde or if there is a better way to achieve this?

Fix patch:
diff --git a/macro/src/derive.rs b/macro/src/derive.rs
index ea36e3e38..b011aabb9 100644
--- a/macro/src/derive.rs
+++ b/macro/src/derive.rs
@@ -21,6 +21,8 @@ pub fn expand_struct(strct: &Struct, actual_derives: &mut Option<TokenStream>) -
             Trait::Ord => expanded.extend(struct_ord(strct, span)),
             Trait::PartialEq => traits.push(quote_spanned!(span=> ::std::cmp::PartialEq)),
             Trait::PartialOrd => expanded.extend(struct_partial_ord(strct, span)),
+            Trait::Serialize => traits.push(quote_spanned!(span=> serde::Serialize)),
+            Trait::Deserialize => traits.push(quote_spanned!(span=> serde::Deserialize)),
         }
     }
 
@@ -66,6 +68,8 @@ pub fn expand_enum(enm: &Enum, actual_derives: &mut Option<TokenStream>) -> Toke
                 has_partial_eq = true;
             }
             Trait::PartialOrd => expanded.extend(enum_partial_ord(enm, span)),
+            Trait::Serialize => traits.push(quote_spanned!(span=> serde::Serialize)),
+            Trait::Deserialize => traits.push(quote_spanned!(span=> serde::Deserialize)),
         }
     }
 
diff --git a/syntax/derive.rs b/syntax/derive.rs
index 96b3eea75..7727fbc94 100644
--- a/syntax/derive.rs
+++ b/syntax/derive.rs
@@ -19,6 +19,8 @@ pub enum Trait {
     Ord,
     PartialEq,
     PartialOrd,
+    Serialize,
+    Deserialize,
 }
 
 impl Derive {
@@ -34,6 +36,8 @@ impl Derive {
             "Ord" => Trait::Ord,
             "PartialEq" => Trait::PartialEq,
             "PartialOrd" => Trait::PartialOrd,
+            "Serialize" => Trait::Serialize,
+            "Deserialize" => Trait::Deserialize,
             _ => return None,
         };
         let span = ident.span();
@@ -60,6 +64,8 @@ impl AsRef<str> for Trait {
             Trait::Ord => "Ord",
             Trait::PartialEq => "PartialEq",
             Trait::PartialOrd => "PartialOrd",
+            Trait::Serialize => "Serialize",
+            Trait::Deserialize => "Deserialize",
         }
     }
 }


Test patch: