Is there a way for me to use #[derive] on a struct

2019-07-03 22:18发布

I am working on a Rust program that uses serde-json, and I really like the #[derive(Serialize, Deserialize)] macros that it gives for use with custom structs and enums. The macros work just fine with my own types. However, I would like to be able to call the macros on types from other libraries that I am using.

I would implement the Serialize and Deserialize traits on those types myself, but the code for Deserialize is especially convoluted, and it would be a pain to write to for every single library type that I use in a struct.

标签: rust
1条回答
女痞
2楼-- · 2019-07-03 22:40

Is there a way for me to use #[derive] on a struct or enum from a library without editing the actual library's source code?

No, there is not.

See also:


For the specific case of Serde, you can use "remote deriving", but you have to provide a duplicate definition of the type, essentially rewriting the original structure.

Many crates provide a feature flag to enable optional functionality, so you may want to look to see if your crate has one for Serde. If it doesn't, you could submit such to the library.

查看更多
登录 后发表回答