tokio-rs / prost

PROST! a Protocol Buffers implementation for the Rust Language
Apache License 2.0
3.86k stars 500 forks source link

Support for adding derive annotations for traits outside prost? #1064

Closed andrew-otiv closed 3 months ago

andrew-otiv commented 4 months ago

Prost already can generate types supporting Clone and PartialEq, which is great. We'd like to also be able to derive other traits, like Default, or the various traits used by fuzzers like cargo fuzz. Rust's orphan rules make this hard in the context of code generation.

We currently only have ~50 code generated types, so we could write our own impls manually, but maybe it would possible to add trait derivations to the generated code via configuration? Theres already a mechanism for switching ~off the already supported traits; I am interested in going the other direction.

Related: https://github.com/tokio-rs/prost/pull/493

romac commented 4 months ago

Have you tried using https://docs.rs/prost-build/latest/prost_build/struct.Config.html#method.type_attribute?

andrew-otiv commented 4 months ago

That looks very promising, thanks! We're trying to set this up from bazel rules_rust; hopefully we can drive this through keyword arguments to https://bazelbuild.github.io/rules_rust/rust_proto.html#rust_prost_library.

caspermeijn commented 3 months ago

You can derive any trait you want using type_attribute as commented earlier. For example to derive Arbirary on every protobuf type:

    prost_build::Config::new()
        .type_attribute(".", "#[derive(arbitrary::Arbitrary)]")
        .compile_protos(&["src/types.proto"], &["src/"])?;

If this doesn't resolve your problem, please reopen the issue.

andrew-otiv commented 2 months ago

That indeed solved our need for derving traits on everything! Thanks!