stepancheg / grpc-rust

Rust implementation of gRPC
MIT License
1.37k stars 124 forks source link

GRPC compiler generates object interface that is somewhat clunky to use ... #111

Closed przygienda closed 6 years ago

przygienda commented 6 years ago

so, I end up with a lot of

let mut o : Object;

o.set_prop1(x); o.set_prop2(x);

let mut o2 : Object2;

o2.set_prop(o);


it would be far elegant if the setter would have a signature like this

fn set_x(&mut Self) -> &mut Self ...

so we could do

let o2 = Object2::new(Object::new() .set_prop1(x) . set_prop2(y));

on larger APIs that adds up to quite a lot of cruft right now ...

stepancheg commented 6 years ago

The plan (for rust-protobuf) is actually to make all fields public, so object can be initialized like this:

let o2 = Object2 {
    prop1: x,
    prop2: y,
    .. Default::default()
}

There's one downside of that approach for syntax=proto2: this way it is slightly less efficient (in terms of memory) than C++ protobuf generated code where field present flags are stored as single field bitmask. So, because of that I'm not 100% sure we should do it.

However, in proto3 there's no field presence flags, and proto2 is going to be eventually replaced with proto3, so probably implementation of masks is probably not worth it.

Also, FYI, for proto3 there's a codegen flag to make fields public: expose_fields_all. This flag can be specified by including rustproto.proto. And there's a task (in my head) to expose these codegen flags via protoc-rust API when codegen is invoked programmatically.

przygienda commented 6 years ago

yes, I saw that pub fields stuff but the set_ stuff hides a lot of one_of_a and repeated things so it makes still for a much more elegant code IME ...

stepancheg commented 6 years ago

Closing as a duplicate of https://github.com/stepancheg/rust-protobuf/issues/178.