Generalize --rust-serialize to traits other than serde::Serialize #8180
andrew-otiv
started this conversation in
Ideas
Replies: 1 comment
-
Without this feature, a really hard workaround would to write a macro that ingests the entire file generated by flatc, adds trait declarations, etc.., and re-emits it: https://www.reddit.com/r/rust/comments/mre53r/derive_away_from_struct_definition/ At this point I would probably skip generating types from schema and just write the code manually. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
flatc has a --rust-serialize argument that implements serde::Serialize for the emitted types. It would be useful to generalize this to configurable macro invocations, on configurable types.
In my use case, the flatbuffer schema is itself generated from ~another schema that came out of a UML modeling tool, so for me having to add something to the flatbuffer schema would just shift the problem.
At the simplest, it could be an extra flag to rustc that looks like:
--emit-above-types "#[derive(SomeUserTrait)]" some_type some_other_type
Since you usually also need to add a "use" or "include" statement...
--emit-once-to-file-containing-types "pub use crate::some-user-trait;" some_type some_other_type
some_type would be the identifier in the flatc file, not the generated name since other flags might control the generated name. This tries to avoid breakage if other flags change the emitted symbol name.
The file-level emitted code would be de-duplicated (thus the "once" in the flag), since it will be common for one file to contain multiple types using the same trait.
The above API would let the user insert invalid rust code, but would work for derive macros as well as other macros, and invalid code will just be caught by the rust compiler anyway.
There is no "rust" prefix since the proposed API might still be useful for tweaking emitted code in other languages as well; it is just a hook for adding lines of code.
Beta Was this translation helpful? Give feedback.
All reactions